This commit is contained in:
2026-10-22 18:41:56 +03:00
parent 665c37c0b8
commit 3695ab6dfa
32 changed files with 881 additions and 156 deletions
+25 -7
View File
@@ -4,7 +4,9 @@ import (
"context"
"log"
"net/http"
"os"
"os/signal"
"sync/atomic"
"syscall"
"time"
@@ -66,30 +68,46 @@ func main() {
chatCache := cache.New(redisClient, chatCacheTTL)
s := store.New(pool, limiter)
mux := httpapi.NewRouterWithCache(s, pool, sessionStore, chatCache)
instanceID := config.GetEnv("INSTANCE_ID", "")
if instanceID == "" {
instanceID, _ = os.Hostname()
}
var draining atomic.Bool
mux := httpapi.NewRouterWithCache(s, pool, sessionStore, chatCache, httpapi.Options{
InstanceID: instanceID,
Redis: redisClient,
Draining: &draining,
})
port := config.GetEnv("PORT", "8080")
srv := &http.Server{
Addr: ":" + port,
Handler: mux,
Addr: ":" + port,
Handler: mux,
ReadHeaderTimeout: 5 * time.Second,
ReadTimeout: 15 * time.Second,
WriteTimeout: 15 * time.Second,
IdleTimeout: 60 * time.Second,
}
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
defer stop()
go func() {
log.Printf("запуск на :%s", port)
log.Printf("запуск на :%s instance=%s", port, instanceID)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("сервер упал: %v", err)
}
}()
<-ctx.Done()
log.Println("Получен сигнал остановки, завершаю активные запросы...")
log.Println("Получен сигнал остановки: readiness -> 503, жду LB...")
draining.Store(true)
time.Sleep(time.Duration(config.ParseIntEnv("DRAIN_SECONDS", 4)) * time.Second)
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
shutdownCtx, cancel := context.WithTimeout(context.Background(), 12*time.Second)
defer cancel()
if err := srv.Shutdown(shutdownCtx); err != nil {
log.Printf("принудительное завершение: %v", err)
}