Добавить 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
+46 -3
View File
@@ -19,7 +19,7 @@ import (
// newHandler builds the smallest handler with probes; the lifecycle does not
// depend on the business modules or their stores.
func newHandler(log *slog.Logger) (*gin.Engine, *health.Handler) {
e := httpx.NewEngine(log, 1024)
e := httpx.NewEngine(log, 1024, "test")
probes := health.New()
probes.Register(e)
return e, probes
@@ -34,7 +34,7 @@ func TestServesUntilContextCancelledThenStopsCleanly(t *testing.T) {
}
ctx, cancel := context.WithCancel(context.Background())
done := make(chan error, 1)
go func() { done <- server.Run(ctx, log, ln, h, probes, 5*time.Second) }()
go func() { done <- server.Run(ctx, log, ln, h, probes, 0, 5*time.Second) }()
base := "http://" + ln.Addr().String()
waitFor(t, base+"/health/ready", http.StatusOK)
@@ -62,7 +62,7 @@ func TestReadinessDropsBeforeListenerCloses(t *testing.T) {
}
ctx, cancel := context.WithCancel(context.Background())
done := make(chan error, 1)
go func() { done <- server.Run(ctx, log, ln, h, probes, 5*time.Second) }()
go func() { done <- server.Run(ctx, log, ln, h, probes, 0, 5*time.Second) }()
base := "http://" + ln.Addr().String()
waitFor(t, base+"/health/ready", http.StatusOK)
@@ -85,6 +85,49 @@ func TestReadinessDropsBeforeListenerCloses(t *testing.T) {
t.Fatal("readiness must be switched off during shutdown")
}
// During the drain delay the balancer is still learning that the replica is
// going away: readiness already fails, yet new connections are accepted and
// served. Only after the delay does the listener close.
func TestDrainKeepsServingWhileNotReady(t *testing.T) {
log := slog.New(slog.NewTextHandler(io.Discard, nil))
h, probes := newHandler(log)
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
done := make(chan error, 1)
const drain = 700 * time.Millisecond
go func() { done <- server.Run(ctx, log, ln, h, probes, drain, 5*time.Second) }()
base := "http://" + ln.Addr().String()
waitFor(t, base+"/health/ready", http.StatusOK)
cancel()
waitFor(t, base+"/health/ready", http.StatusServiceUnavailable)
// A fresh connection, as the balancer opens for a new request.
fresh := &http.Client{Transport: &http.Transport{DisableKeepAlives: true}}
resp, err := fresh.Get(base + "/health/live")
if err != nil {
t.Fatalf("new connection during the drain delay: %v", err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("liveness during drain = %d, want 200", resp.StatusCode)
}
select {
case err := <-done:
if err != nil {
t.Fatalf("Run returned error: %v", err)
}
case <-time.After(10 * time.Second):
t.Fatal("Run did not stop after the drain delay")
}
if _, err := fresh.Get(base + "/health/live"); err == nil {
t.Fatal("listener must be closed after the drain delay")
}
}
func waitFor(t *testing.T, url string, status int) {
t.Helper()
deadline := time.Now().Add(5 * time.Second)