fix(server): shut down through fx so buffered reports flush (closes #22)
check / check (push) Failing after 0s
check / check (push) Failing after 0s
The server ran os.Exit at the end of its own goroutine, which raced fx's teardown and could kill the process before reportbuf's OnStop flushed the buffer — losing up to a full flush window of telemetry on every restart, silently and with exit 0. The server now requests shutdown through fx.Shutdowner, so fx runs every OnStop in dependency order. The http.Server is built synchronously in OnStart before the serving goroutine starts, so shutdown can no longer race or nil-deref it; the field is never written and read from two goroutines without a happens-before edge. A listen failure now exits non-zero via fx.ExitCode(1). reportbuf's OnStop is guarded by sync.Once. writeTimeout now exceeds the chi per-request budget, with a comment, so that budget is reachable. Dead startupTime, exitCode, and cancelFunc fields are gone. A new test buffers a report and asserts it reaches disk after the fx lifecycle stops. Model: opus-4-8
This commit is contained in:
@@ -1,16 +1,14 @@
|
||||
// Package server provides the HTTP server lifecycle,
|
||||
// including startup, routing, signal handling, and graceful
|
||||
// shutdown.
|
||||
// including startup, routing, and graceful shutdown. The
|
||||
// process lifetime is owned by fx: shutdown is requested
|
||||
// through fx.Shutdowner so every component's OnStop hook runs
|
||||
// in dependency order.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"sneak.berlin/go/netwatch/internal/config"
|
||||
"sneak.berlin/go/netwatch/internal/globals"
|
||||
@@ -31,19 +29,18 @@ type Params struct {
|
||||
Handlers *handlers.Handlers
|
||||
Logger *logger.Logger
|
||||
Middleware *middleware.Middleware
|
||||
Shutdowner fx.Shutdowner
|
||||
}
|
||||
|
||||
// Server is the top-level HTTP server orchestrator.
|
||||
type Server struct {
|
||||
cancelFunc context.CancelFunc
|
||||
exitCode int
|
||||
h *handlers.Handlers
|
||||
httpServer *http.Server
|
||||
log *slog.Logger
|
||||
mw *middleware.Middleware
|
||||
params Params
|
||||
router *chi.Mux
|
||||
startupTime time.Time
|
||||
h *handlers.Handlers
|
||||
httpServer *http.Server
|
||||
log *slog.Logger
|
||||
mw *middleware.Middleware
|
||||
params Params
|
||||
router *chi.Mux
|
||||
shutdowner fx.Shutdowner
|
||||
}
|
||||
|
||||
// New creates a Server and registers lifecycle hooks for
|
||||
@@ -57,23 +54,25 @@ func New(
|
||||
s.mw = params.Middleware
|
||||
s.h = params.Handlers
|
||||
s.log = params.Logger.Get()
|
||||
s.shutdowner = params.Shutdowner
|
||||
|
||||
lc.Append(fx.Hook{
|
||||
OnStart: func(_ context.Context) error {
|
||||
s.startupTime = time.Now().UTC()
|
||||
// Build the router and http.Server synchronously
|
||||
// here, before spawning the serving goroutine, so
|
||||
// httpServer is fully constructed by the time OnStop
|
||||
// (or an early signal) can read it. fx guarantees
|
||||
// OnStart returns before OnStop runs, so no
|
||||
// synchronization or nil check is needed at shutdown.
|
||||
s.SetupRoutes()
|
||||
s.httpServer = s.newHTTPServer()
|
||||
|
||||
go func() { //nolint:contextcheck // fx OnStart ctx is startup-only; run() creates its own
|
||||
s.run()
|
||||
}()
|
||||
go s.listenAndServe()
|
||||
|
||||
return nil
|
||||
},
|
||||
OnStop: func(_ context.Context) error {
|
||||
if s.cancelFunc != nil {
|
||||
s.cancelFunc()
|
||||
}
|
||||
|
||||
return nil
|
||||
OnStop: func(ctx context.Context) error {
|
||||
return s.shutdown(ctx)
|
||||
},
|
||||
})
|
||||
|
||||
@@ -88,60 +87,17 @@ func (s *Server) ServeHTTP(
|
||||
s.router.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func (s *Server) run() {
|
||||
exitCode := s.serve()
|
||||
os.Exit(exitCode)
|
||||
}
|
||||
|
||||
func (s *Server) serve() int {
|
||||
var ctx context.Context //nolint:wsl // ctx must be declared before multi-assign
|
||||
|
||||
ctx, s.cancelFunc = context.WithCancel(
|
||||
context.Background(),
|
||||
)
|
||||
|
||||
go func() {
|
||||
c := make(chan os.Signal, 1)
|
||||
|
||||
signal.Ignore(syscall.SIGPIPE)
|
||||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
sig := <-c
|
||||
s.log.Info("signal received", "signal", sig)
|
||||
|
||||
if s.cancelFunc != nil {
|
||||
s.cancelFunc()
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
s.serveUntilShutdown()
|
||||
}()
|
||||
|
||||
<-ctx.Done()
|
||||
s.cleanShutdown()
|
||||
|
||||
return s.exitCode
|
||||
}
|
||||
|
||||
const shutdownTimeout = 5 * time.Second
|
||||
|
||||
func (s *Server) cleanShutdown() {
|
||||
s.exitCode = 0
|
||||
|
||||
ctxShutdown, shutdownCancel := context.WithTimeout(
|
||||
context.Background(),
|
||||
shutdownTimeout,
|
||||
)
|
||||
defer shutdownCancel()
|
||||
|
||||
err := s.httpServer.Shutdown(ctxShutdown)
|
||||
// shutdown gracefully stops the HTTP server within the
|
||||
// deadline of the context fx provides for OnStop.
|
||||
func (s *Server) shutdown(ctx context.Context) error {
|
||||
err := s.httpServer.Shutdown(ctx)
|
||||
if err != nil {
|
||||
s.log.Error(
|
||||
"server clean shutdown failed",
|
||||
"error", err,
|
||||
)
|
||||
s.log.Error("server clean shutdown failed", "error", err)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
s.log.Info("server stopped")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user