Добавить 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 {