Добавить X-Instance-Id, задержку drain при SIGTERM и отдельный запуск миграций

This commit is contained in:
2026-11-02 16:58:11 +03:00
parent 5b9f377783
commit 7fa8881eb3
21 changed files with 389 additions and 64 deletions
+22 -1
View File
@@ -33,6 +33,8 @@ type payload struct {
Count int `json:"count"`
}
const testInstance = "0123456789abcdef"
func newEngine(t *testing.T, logs *bytes.Buffer) *gin.Engine {
t.Helper()
return newEngineAt(t, logs, slog.LevelDebug)
@@ -44,7 +46,7 @@ func newEngineAt(t *testing.T, logs *bytes.Buffer, level slog.Level) *gin.Engine
logs = &bytes.Buffer{}
}
log := slog.New(slog.NewJSONHandler(logs, &slog.HandlerOptions{Level: level}))
e := httpx.NewEngine(log, 64)
e := httpx.NewEngine(log, 64, testInstance)
e.GET("/ok", func(c *gin.Context) { httpx.JSON(c, http.StatusOK, gin.H{"text": "<a&b>"}) })
e.GET("/panic", func(c *gin.Context) { panic("secret detail") })
e.GET("/fail", func(c *gin.Context) { httpx.Error(c, errors.New("db password leaked")) })
@@ -106,6 +108,25 @@ func assertError(t *testing.T, rec *httptest.ResponseRecorder, eb errorBody, sta
}
}
// The balancer spreads one client's requests over the replicas; the header
// is how a client tells which process answered, so no response may lack it.
func TestEveryResponseCarriesInstanceID(t *testing.T) {
e := newEngine(t, nil)
for _, r := range []struct{ method, path string }{
{http.MethodGet, "/ok"},
{http.MethodGet, "/missing"},
{http.MethodDelete, "/ok"},
{http.MethodGet, "/panic"},
{http.MethodGet, "/fail"},
{http.MethodPost, "/decode"},
} {
rec, _ := do(t, e, r.method, r.path, "", nil)
if got := rec.Header().Get("X-Instance-Id"); got != testInstance {
t.Errorf("%s %s (status %d): X-Instance-Id = %q, want %q", r.method, r.path, rec.Code, got, testInstance)
}
}
}
func TestUnknownRouteIsJSON404(t *testing.T) {
rec, eb := do(t, newEngine(t, nil), http.MethodGet, "/nope", "", nil)
assertError(t, rec, eb, http.StatusNotFound, "not_found")