Отвязать жизненный цикл сервера от сборки приложения

This commit is contained in:
2026-10-06 14:50:12 +03:00
parent 6720108793
commit 46c00436a0
3 changed files with 28 additions and 12 deletions
+2 -1
View File
@@ -33,7 +33,8 @@ func main() {
defer stop()
log.Info("starting", "env", cfg.Env, "addr", cfg.Addr(), "shutdown_timeout", cfg.ShutdownTimeout.String())
if err := server.Run(ctx, log, ln, app.New(log, cfg.MaxBodyBytes), cfg.ShutdownTimeout); err != nil {
a := app.New(log, cfg.MaxBodyBytes)
if err := server.Run(ctx, log, ln, a.Engine, a.Health, cfg.ShutdownTimeout); err != nil {
log.Error("server stopped with error", "error", err.Error())
os.Exit(1)
}
+8 -5
View File
@@ -10,14 +10,17 @@ import (
"net"
"net/http"
"time"
"messenger/internal/app"
)
// Readiness is switched off when shutdown starts.
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, a *app.App, shutdownTimeout time.Duration) error {
func Run(ctx context.Context, log *slog.Logger, ln net.Listener, h http.Handler, ready Readiness, shutdownTimeout time.Duration) error {
srv := &http.Server{
Handler: a.Engine,
Handler: h,
ReadHeaderTimeout: 5 * time.Second,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
@@ -43,7 +46,7 @@ func Run(ctx context.Context, log *slog.Logger, ln net.Listener, a *app.App, shu
}
log.Info("shutdown started", "timeout", shutdownTimeout.String())
a.Health.SetReady(false)
ready.SetReady(false)
drain, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
if err := srv.Shutdown(drain); err != nil {
+18 -6
View File
@@ -9,20 +9,32 @@ import (
"testing"
"time"
"messenger/internal/app"
"github.com/gin-gonic/gin"
"messenger/internal/health"
"messenger/internal/platform/httpx"
"messenger/internal/server"
)
// newHandler builds the smallest handler with probes; the lifecycle does not
// depend on the business modules or their stores.
func newHandler(log *slog.Logger) (*gin.Engine, *health.Handler) {
e := httpx.NewEngine(log, 1024)
probes := health.New()
probes.Register(e)
return e, probes
}
func TestServesUntilContextCancelledThenStopsCleanly(t *testing.T) {
log := slog.New(slog.NewTextHandler(io.Discard, nil))
a := app.New(log, 1024)
h, probes := newHandler(log)
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
done := make(chan error, 1)
go func() { done <- server.Run(ctx, log, ln, a, 5*time.Second) }()
go func() { done <- server.Run(ctx, log, ln, h, probes, 5*time.Second) }()
base := "http://" + ln.Addr().String()
waitFor(t, base+"/health/ready", http.StatusOK)
@@ -43,14 +55,14 @@ func TestServesUntilContextCancelledThenStopsCleanly(t *testing.T) {
func TestReadinessDropsBeforeListenerCloses(t *testing.T) {
log := slog.New(slog.NewTextHandler(io.Discard, nil))
a := app.New(log, 1024)
h, probes := newHandler(log)
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
done := make(chan error, 1)
go func() { done <- server.Run(ctx, log, ln, a, 5*time.Second) }()
go func() { done <- server.Run(ctx, log, ln, h, probes, 5*time.Second) }()
base := "http://" + ln.Addr().String()
waitFor(t, base+"/health/ready", http.StatusOK)
@@ -67,7 +79,7 @@ func TestReadinessDropsBeforeListenerCloses(t *testing.T) {
cancel()
<-done
<-slow
if !a.Health.IsReady() {
if !probes.IsReady() {
return
}
t.Fatal("readiness must be switched off during shutdown")