check / check (push) Waiting to run
TestService_Get_ReturnsByItsDeadline failed on a busy host because its request, past its deadline, still waited for its miss count to be written, a database write with no deadline; in pixad that write waits for the one database connection every request shares. The hit, miss, upstream fetch and transform counts are now each written in a goroutine of their own, which the request waits for only until its deadline; a count not written by then is written after it returns. Shutdown waits for those writes within ShutdownTimeout and reports any left unfinished. The test phase also runs go test with -parallel 4: on a busy host, as many tests at once as there are CPUs wait so long to be scheduled that a timed request can fail. Model: opus-5-5
164 lines
4.1 KiB
Go
164 lines
4.1 KiB
Go
// Package server provides the HTTP server and lifecycle management.
|
|
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/getsentry/sentry-go"
|
|
"github.com/go-chi/chi/v5"
|
|
"go.uber.org/fx"
|
|
"sneak.berlin/go/pixa/internal/config"
|
|
"sneak.berlin/go/pixa/internal/globals"
|
|
"sneak.berlin/go/pixa/internal/handlers"
|
|
"sneak.berlin/go/pixa/internal/logger"
|
|
"sneak.berlin/go/pixa/internal/middleware"
|
|
)
|
|
|
|
// Shutdown configuration constants.
|
|
const (
|
|
ShutdownTimeout = 5 * time.Second
|
|
SentryFlushTimeout = 2 * time.Second
|
|
)
|
|
|
|
// errStillProcessing is returned by the server's stop hook when images are
|
|
// still being processed once ShutdownTimeout has passed.
|
|
var errStillProcessing = errors.New("images still being processed at shutdown")
|
|
|
|
// errStillWritingCounts is returned by the server's stop hook when counts
|
|
// are still being written to the database once ShutdownTimeout has passed.
|
|
var errStillWritingCounts = errors.New("counts still being written at shutdown")
|
|
|
|
// Params defines dependencies for Server.
|
|
type Params struct {
|
|
fx.In
|
|
|
|
Logger *logger.Logger
|
|
Globals *globals.Globals
|
|
Config *config.Config
|
|
Middleware *middleware.Middleware
|
|
Handlers *handlers.Handlers
|
|
Shutdowner fx.Shutdowner
|
|
}
|
|
|
|
// Server is the main HTTP server.
|
|
type Server struct {
|
|
log *slog.Logger
|
|
config *config.Config
|
|
globals *globals.Globals
|
|
mw *middleware.Middleware
|
|
h *handlers.Handlers
|
|
shutdowner fx.Shutdowner
|
|
startupTime time.Time
|
|
sentryEnabled bool
|
|
httpServer *http.Server
|
|
router *chi.Mux
|
|
}
|
|
|
|
// New creates a new Server instance. Its start hook starts Sentry and the
|
|
// HTTP server; its stop hook, which fx runs on SIGINT, SIGTERM or a
|
|
// shutdown request, shuts them down.
|
|
func New(lc fx.Lifecycle, params Params) (*Server, error) {
|
|
s := &Server{
|
|
log: params.Logger.Get(),
|
|
config: params.Config,
|
|
globals: params.Globals,
|
|
mw: params.Middleware,
|
|
h: params.Handlers,
|
|
shutdowner: params.Shutdowner,
|
|
}
|
|
|
|
lc.Append(fx.Hook{
|
|
OnStart: func(_ context.Context) error {
|
|
s.startupTime = time.Now()
|
|
|
|
err := s.enableSentry()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
s.SetupRoutes()
|
|
s.httpServer = s.newHTTPServer()
|
|
|
|
go s.serveUntilShutdown()
|
|
|
|
return nil
|
|
},
|
|
OnStop: s.cleanShutdown,
|
|
})
|
|
|
|
return s, nil
|
|
}
|
|
|
|
// MaintenanceMode returns whether maintenance mode is enabled.
|
|
func (s *Server) MaintenanceMode() bool {
|
|
return s.config.MaintenanceMode
|
|
}
|
|
|
|
func (s *Server) enableSentry() error {
|
|
s.sentryEnabled = false
|
|
|
|
if s.config.SentryDSN == "" {
|
|
return nil
|
|
}
|
|
|
|
err := sentry.Init(sentry.ClientOptions{
|
|
Dsn: s.config.SentryDSN,
|
|
Release: fmt.Sprintf("%s-%s", s.globals.Appname, s.globals.Version),
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("sentry init failure: %w", err)
|
|
}
|
|
|
|
s.log.Info("sentry error reporting activated")
|
|
s.sentryEnabled = true
|
|
|
|
return nil
|
|
}
|
|
|
|
// cleanShutdown stops the HTTP server, waits for the images still being
|
|
// processed and then for the counts still being written to the database,
|
|
// which closes after this hook, then flushes Sentry. The first three share
|
|
// ShutdownTimeout. It returns errStillProcessing and errStillWritingCounts
|
|
// for the images and counts still unfinished after that, as their work is
|
|
// abandoned.
|
|
func (s *Server) cleanShutdown(ctx context.Context) error {
|
|
s.log.Info("shutting down")
|
|
|
|
ctxShutdown, shutdownCancel := context.WithTimeout(ctx, ShutdownTimeout)
|
|
defer shutdownCancel()
|
|
|
|
err := s.httpServer.Shutdown(ctxShutdown)
|
|
if err != nil {
|
|
s.log.Error("server clean shutdown failed", "error", err)
|
|
}
|
|
|
|
stillProcessing := s.h.WaitForProcessing(ctxShutdown)
|
|
countsWritten := s.h.WaitForCountWrites(ctxShutdown)
|
|
|
|
if s.sentryEnabled {
|
|
sentry.Flush(SentryFlushTimeout)
|
|
}
|
|
|
|
var unfinished []error
|
|
|
|
if stillProcessing > 0 {
|
|
s.log.Error("images still being processed at shutdown",
|
|
"count", stillProcessing)
|
|
|
|
unfinished = append(unfinished, errStillProcessing)
|
|
}
|
|
|
|
if !countsWritten {
|
|
s.log.Error("counts still being written at shutdown")
|
|
|
|
unfinished = append(unfinished, errStillWritingCounts)
|
|
}
|
|
|
|
return errors.Join(unfinished...)
|
|
}
|