Добавить X-Instance-Id, задержку drain при SIGTERM и отдельный запуск миграций
This commit is contained in:
+9
-2
@@ -25,6 +25,8 @@ import (
|
||||
type App struct {
|
||||
Engine *gin.Engine
|
||||
Health *health.Handler
|
||||
// Chats is exposed for the background cleanup of idempotency keys.
|
||||
Chats *chat.Service
|
||||
}
|
||||
|
||||
// Deps are the shared resources the modules are built on; their lifetime is
|
||||
@@ -37,6 +39,11 @@ type Deps struct {
|
||||
MaxBodyBytes int64
|
||||
// RequestTimeout bounds every API request; zero means the default.
|
||||
RequestTimeout time.Duration
|
||||
// InstanceID is sent as X-Instance-Id on every response.
|
||||
InstanceID string
|
||||
// IdempotencyTTL is how long a message Idempotency-Key is remembered;
|
||||
// zero means the default.
|
||||
IdempotencyTTL time.Duration
|
||||
}
|
||||
|
||||
// defaultRequestTimeout stays below the server's 10s write timeout, so a
|
||||
@@ -47,7 +54,7 @@ func New(d Deps) *App {
|
||||
users := user.NewService(userstore.NewUsers(d.DB))
|
||||
chats := chat.NewService(chatstore.NewChats(d.DB), chatstore.NewMessages(d.DB), users)
|
||||
|
||||
engine := httpx.NewEngine(d.Log, d.MaxBodyBytes)
|
||||
engine := httpx.NewEngine(d.Log, d.MaxBodyBytes, d.InstanceID)
|
||||
probes := health.New(
|
||||
health.Check{Name: "postgres", Probe: d.DB.Ping},
|
||||
health.Check{Name: "valkey", Probe: func(ctx context.Context) error { return d.Sessions.Ping(ctx).Err() }},
|
||||
@@ -66,5 +73,5 @@ func New(d Deps) *App {
|
||||
user.NewHandler(users).Register(public, protected)
|
||||
chat.NewHandler(chats).Register(protected)
|
||||
|
||||
return &App{Engine: engine, Health: probes}
|
||||
return &App{Engine: engine, Health: probes, Chats: chats}
|
||||
}
|
||||
|
||||
@@ -50,6 +50,9 @@ func TestMain(m *testing.M) {
|
||||
|
||||
const testPassword = "correct-horse-battery"
|
||||
|
||||
// testInstance is the alias of the in-process application under test.
|
||||
const testInstance = "00000000000000a1"
|
||||
|
||||
// errorBody mirrors the contract error envelope.
|
||||
type errorBody struct {
|
||||
Error struct {
|
||||
@@ -103,6 +106,7 @@ func newTesterWithTTL(t *testing.T, ttl time.Duration) *tester {
|
||||
Sessions: kv.Client,
|
||||
SessionTTL: ttl,
|
||||
MaxBodyBytes: 64 * 1024,
|
||||
InstanceID: testInstance,
|
||||
})}
|
||||
}
|
||||
|
||||
@@ -135,6 +139,9 @@ func (ts *tester) do(method, path, token string, body any, headers map[string]st
|
||||
if res.requestID == "" {
|
||||
ts.t.Fatalf("%s %s: missing X-Request-Id", method, path)
|
||||
}
|
||||
if got := rec.Header().Get("X-Instance-Id"); got != testInstance {
|
||||
ts.t.Fatalf("%s %s: X-Instance-Id = %q, want %q", method, path, got, testInstance)
|
||||
}
|
||||
if res.status >= 400 {
|
||||
var eb errorBody
|
||||
if err := json.Unmarshal(res.raw, &eb); err != nil {
|
||||
|
||||
@@ -146,11 +146,14 @@ func TestLogoutRevokesOnlyTheCurrentToken(t *testing.T) {
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/users/me", other.Token, nil, nil), http.StatusOK, "")
|
||||
}
|
||||
|
||||
// The TTL leaves room for a loaded machine: with one second the first request
|
||||
// after login sometimes arrived too late under -race with all packages
|
||||
// running at once.
|
||||
func TestSessionExpiresAfterTTL(t *testing.T) {
|
||||
ts := newTesterWithTTL(t, time.Second)
|
||||
ts := newTesterWithTTL(t, 3*time.Second)
|
||||
alice := ts.user("alice", "A")
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil), http.StatusOK, "")
|
||||
time.Sleep(1200 * time.Millisecond)
|
||||
time.Sleep(3200 * time.Millisecond)
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil), http.StatusUnauthorized, "unauthorized")
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ func newIsolatedTester(t *testing.T, requestTimeout time.Duration) (*tester, *te
|
||||
SessionTTL: time.Hour,
|
||||
MaxBodyBytes: 64 * 1024,
|
||||
RequestTimeout: requestTimeout,
|
||||
InstanceID: testInstance,
|
||||
})
|
||||
return &tester{t: t, a: a}, pg, vk
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user