Добавить генератор нагрузки, сбор метрик реплик и сценарии экспериментов для отказов, rolling update и трека 1

This commit is contained in:
2026-11-02 17:44:59 +03:00
parent c5cdcb3377
commit b2973405f3
30 changed files with 2067 additions and 18 deletions
+38
View File
@@ -3,6 +3,7 @@ package main
import (
"context"
"expvar"
"fmt"
"log/slog"
"net"
@@ -13,6 +14,8 @@ import (
"syscall"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"messenger/internal/app"
"messenger/internal/chat"
"messenger/internal/platform/config"
@@ -80,10 +83,45 @@ func run(ctx context.Context, log *slog.Logger, cfg config.Config, alias string)
var background sync.WaitGroup
defer background.Wait() // before the deferred pool.Close
background.Go(func() { chat.RunKeyCleanup(ctx, log, a.Chats, keyCleanupEvery) })
if cfg.DebugAddr != "" {
publishDebugVars(alias, pool)
debugLn, err := net.Listen("tcp", cfg.DebugAddr)
if err != nil {
return fmt.Errorf("listen on %s: %w", cfg.DebugAddr, err)
}
background.Go(func() {
if err := server.RunDebug(ctx, log, debugLn, expvar.Handler()); err != nil {
log.Error("debug listener stopped", "error", err.Error())
}
})
}
return server.Run(ctx, log, ln, a.Engine, a.Health, cfg.DrainDelay, cfg.ShutdownTimeout)
}
// publishDebugVars exposes, next to the runtime's memstats, what the scaling
// study needs from a replica: who it is, how many CPUs Go uses inside the
// container's limit, and how often requests had to wait for a pooled
// database connection.
func publishDebugVars(alias string, pool *pgxpool.Pool) {
expvar.NewString("instance").Set(alias)
expvar.Publish("gomaxprocs", expvar.Func(func() any { return runtime.GOMAXPROCS(0) }))
expvar.Publish("pgxpool", expvar.Func(func() any {
s := pool.Stat()
return map[string]any{
"acquire_count": s.AcquireCount(),
"acquire_duration_ms": s.AcquireDuration().Milliseconds(),
"acquired_conns": s.AcquiredConns(),
"idle_conns": s.IdleConns(),
"total_conns": s.TotalConns(),
"max_conns": s.MaxConns(),
"empty_acquire_count": s.EmptyAcquireCount(),
"empty_acquire_wait_ms": s.EmptyAcquireWaitTime().Milliseconds(),
"canceled_acquire_count": s.CanceledAcquireCount(),
}
}))
}
func newLogger(cfg config.Config) *slog.Logger {
opts := &slog.HandlerOptions{Level: cfg.LogLevel}
if cfg.Env == "dev" {