Добавить X-Instance-Id, задержку drain при SIGTERM и отдельный запуск миграций
This commit is contained in:
+22
-17
@@ -8,10 +8,12 @@ import (
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"syscall"
|
||||
|
||||
"messenger/internal/app"
|
||||
"messenger/internal/platform/config"
|
||||
"messenger/internal/platform/ident"
|
||||
"messenger/internal/platform/postgres"
|
||||
"messenger/internal/platform/valkey"
|
||||
"messenger/internal/server"
|
||||
@@ -23,30 +25,29 @@ func main() {
|
||||
fmt.Fprintln(os.Stderr, "configuration error:", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
log := newLogger(cfg)
|
||||
// Every log line carries the alias that this process sends as
|
||||
// X-Instance-Id, so a response can be matched to its replica's log.
|
||||
alias := ident.NewAlias()
|
||||
log := newLogger(cfg).With("instance", alias)
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
|
||||
defer stop()
|
||||
|
||||
if err := run(ctx, log, cfg); err != nil {
|
||||
if err := run(ctx, log, cfg, alias); err != nil {
|
||||
log.Error("server stopped with error", "error", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// run connects the dependencies before it listens, so the port accepts
|
||||
// connections only once the schema is in place.
|
||||
func run(ctx context.Context, log *slog.Logger, cfg config.Config) error {
|
||||
pool, err := postgres.NewPool(ctx, cfg.DatabaseURL)
|
||||
// connections only once they answer. The schema is applied beforehand by the
|
||||
// separate migrate command, once per deployment rather than by every replica.
|
||||
func run(ctx context.Context, log *slog.Logger, cfg config.Config, alias string) error {
|
||||
pool, err := postgres.NewPool(ctx, cfg.DatabaseURL, cfg.DBMaxConns)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
applied, err := postgres.Migrate(ctx, pool)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Info("schema ready", "applied_migrations", applied)
|
||||
sessions, err := valkey.NewClient(ctx, cfg.SessionStoreURL)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -58,15 +59,19 @@ func run(ctx context.Context, log *slog.Logger, cfg config.Config) error {
|
||||
return fmt.Errorf("listen on %s: %w", cfg.Addr(), err)
|
||||
}
|
||||
log.Info("starting", "env", cfg.Env, "addr", cfg.Addr(),
|
||||
"shutdown_timeout", cfg.ShutdownTimeout.String(), "session_ttl", cfg.SessionTTL.String())
|
||||
"gomaxprocs", runtime.GOMAXPROCS(0), "db_max_conns", cfg.DBMaxConns,
|
||||
"drain_delay", cfg.DrainDelay.String(), "shutdown_timeout", cfg.ShutdownTimeout.String(),
|
||||
"session_ttl", cfg.SessionTTL.String(), "idempotency_ttl", cfg.IdempotencyTTL.String())
|
||||
a := app.New(app.Deps{
|
||||
Log: log,
|
||||
DB: pool,
|
||||
Sessions: sessions,
|
||||
SessionTTL: cfg.SessionTTL,
|
||||
MaxBodyBytes: cfg.MaxBodyBytes,
|
||||
Log: log,
|
||||
DB: pool,
|
||||
Sessions: sessions,
|
||||
SessionTTL: cfg.SessionTTL,
|
||||
MaxBodyBytes: cfg.MaxBodyBytes,
|
||||
InstanceID: alias,
|
||||
IdempotencyTTL: cfg.IdempotencyTTL,
|
||||
})
|
||||
return server.Run(ctx, log, ln, a.Engine, a.Health, cfg.ShutdownTimeout)
|
||||
return server.Run(ctx, log, ln, a.Engine, a.Health, cfg.DrainDelay, cfg.ShutdownTimeout)
|
||||
}
|
||||
|
||||
func newLogger(cfg config.Config) *slog.Logger {
|
||||
|
||||
Reference in New Issue
Block a user