Добавить 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
+14 -5
View File
@@ -1,6 +1,7 @@
// Package server runs the HTTP listener with the lifecycle the lab
// requires: readiness drops first, the listener stops accepting, in-flight
// requests drain within a bounded timeout, then the process exits.
// requires: readiness drops first, the process keeps serving for a drain
// delay while the load balancer notices, then the listener stops accepting,
// in-flight requests drain within a bounded timeout and the process exits.
package server
import (
@@ -17,8 +18,15 @@ type Readiness interface {
SetReady(ready bool)
}
// Run serves until ctx is cancelled, then shuts down within shutdownTimeout.
func Run(ctx context.Context, log *slog.Logger, ln net.Listener, h http.Handler, ready Readiness, shutdownTimeout time.Duration) error {
// Run serves until ctx is cancelled, keeps serving for drainDelay with
// readiness off, then shuts down within shutdownTimeout.
//
// The balancer learns about a stopping replica only from its failing
// readiness checks. Closing the listener at once would let it send requests
// to a closed port, or over a kept-alive connection the server is closing,
// until the checks catch up. drainDelay must therefore exceed the balancer's
// detection time.
func Run(ctx context.Context, log *slog.Logger, ln net.Listener, h http.Handler, ready Readiness, drainDelay, shutdownTimeout time.Duration) error {
srv := &http.Server{
Handler: h,
ReadHeaderTimeout: 5 * time.Second,
@@ -45,8 +53,9 @@ func Run(ctx context.Context, log *slog.Logger, ln net.Listener, h http.Handler,
case <-ctx.Done():
}
log.Info("shutdown started", "timeout", shutdownTimeout.String())
log.Info("shutdown started", "drain_delay", drainDelay.String(), "timeout", shutdownTimeout.String())
ready.SetReady(false)
time.Sleep(drainDelay)
drain, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
if err := srv.Shutdown(drain); err != nil {
+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)