forked from mtusi2026/mtusi2026lab3
lab3 na4
This commit is contained in:
+25
-7
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user