diff --git a/docs/services/rate-limiting.md b/docs/services/rate-limiting.md index 9b2030b6..655d5d31 100644 --- a/docs/services/rate-limiting.md +++ b/docs/services/rate-limiting.md @@ -58,7 +58,7 @@ Keys are `rl:`. The prefix is `cache.prefix`, or by default `app.na When Redis does not answer, each attempt waits at most `cooler.AttemptTimeout` (500 ms) and then counts in a per-instance `surf.MemoryStore`, as if the memory store were selected, until Redis answers again. A warning is logged at most once a minute. Redis being down at start-up is a warning too, not a failed start; a missing or malformed `redis.url` fails the start with an error that names the key and never shows the URL. -Only `serve` connects to Redis. `route:list` builds the router with the memory store, and so does `surf.Assemble` in tests, so neither needs a running Redis. +Only `serve` connects to Redis. `route:list` builds the router with the memory store, and so does `surf.Assemble` in tests, so neither needs a running Redis. That memory store is the router's own: `surf.Router.Close` stops its background sweep, so a test that builds many routers closes them and does not pile up sweep goroutines. A store published on the app (such as cooler's Redis store) is never closed by the router; `serve` closes the per-instance fallback store it created on shutdown. ## Client IP and trusted proxies diff --git a/modules/surf/README.md b/modules/surf/README.md index d38c6e72..e04e71b0 100644 --- a/modules/surf/README.md +++ b/modules/surf/README.md @@ -29,7 +29,7 @@ Use cases: - Raw groups (`surf.Router.GroupRaw`) for routes that must not be wrapped in house middleware, such as webhooks or file streams: house middleware is refused there, the default body limit is skipped and a panic returns a bare 500. - Built-in middleware names: `throttle:` or `throttle:,`, `body.limit:`, `cors:`, `turnstile:open` and `turnstile:closed` (Cloudflare Turnstile through `bouncer.Turnstile`, built from `turnstile.secret`, with the client IP from `surf.ClientIP` and refusals written by a published `bouncer.TurnstileWriter` when there is one), `locale.from-principal`, plus `backend` (the admin guard) when the admin is enabled. - Named CORS profiles: `cors:` applies `http.cors_profiles.` (read by `surf.LoadCORSProfiles`, validated like `http.cors`) to a route or route group. The profile wraps every other layer of the route, whatever its position in the list or the group nesting, so every response carries its headers: guard refusals (401, 403), throttle (429), Turnstile (422, 503), the body limit (413) and recovered panics (500) included. A handler's own `Vary` is merged with the profile's `Vary: Origin`, not replacing it. surf registers an `OPTIONS` route for each such path that answers the preflight from the profile, since ServeMux would answer it with 405 before the middleware runs. An unknown profile, two profiles on one path and a profiled path that `http.cors.paths` also matches fail boot. -- Fixed-window rate limiting (`surf.FixedWindowLimiter`): named buckets from plugins that implement `surf.BucketProvider`, or inline limits keyed by the signed-in user, or by client IP for guests. Rejected requests get a 429 with `Retry-After` and `X-RateLimit-*` headers. The in-process `surf.MemoryStore` sits behind the `surf.Store` interface; `surf.BuildRouter` uses a `surf.Store` published on the app instead when there is one, which is how `serve` shares counters across instances through [cooler](../cooler/README.md)'s Redis store (`http.rate_limit.store: redis`). +- Fixed-window rate limiting (`surf.FixedWindowLimiter`): named buckets from plugins that implement `surf.BucketProvider`, or inline limits keyed by the signed-in user, or by client IP for guests. Rejected requests get a 429 with `Retry-After` and `X-RateLimit-*` headers. The in-process `surf.MemoryStore` sits behind the `surf.Store` interface; `surf.BuildRouter` uses a `surf.Store` published on the app instead when there is one, which is how `serve` shares counters across instances through [cooler](../cooler/README.md)'s Redis store (`http.rate_limit.store: redis`). When nothing is published, the router creates the fallback `surf.MemoryStore` itself, owns it and stops its background sweep on `surf.Router.Close`; a published store is never closed by the router. - Client IP resolution for limiter keys (`surf.ClientIP`) that only trusts `X-Forwarded-For` hops when the direct peer is inside a configured trusted proxy range (`surf.TrustedProxies`). - Every non-raw route runs inside JSON panic recovery (an opaque 500 via [wire](../wire/README.md)), gets the request locale from a published `surf.LocaleResolver` when one exists or from the `Accept-Language` header otherwise (see [towel](../towel/README.md)), and a request body cap. Responses are buffered until the handler returns, so a panic never leaves a half-written body. - Branded error pages for browsers: a top-level browser navigation (`Sec-Fetch-Mode: navigate` when the header is sent, otherwise GET or HEAD; no `X-Requested-With`; an `Accept` header that names `text/html` or `application/xhtml+xml` with a higher q than any JSON type; not `/api`, `/api/...`, `/_/...` or the admin prefix) that hits a framework-generated error gets a self-contained SummerCMS HTML page with the same status. Covered: unmatched routes (404), wrong methods (405, `Allow` kept), failed `Where`/`WhereIn` constraints and overlap-family misses (404, 405), the uploads route while its bucket or database is unpublished (404), and recovered panics in normal and raw groups (500; the panic value is never shown). Every other request keeps the exact stdlib or [wire](../wire/README.md) body. Method, path and `X-Request-Id` appear on the page only when `app.debug` is true or outside the production environment. The page sends `Cache-Control: no-store`, `X-Content-Type-Options: nosniff`, `Vary: Accept, Sec-Fetch-Mode, X-Requested-With` and a `Content-Security-Policy` that allows no script. An application or plugin replaces the page by publishing a `surf.ErrorPageRenderer`; `http.error_pages.enabled: false` turns the feature off. Handlers' own error bodies and middleware refusals are not touched. @@ -131,6 +131,7 @@ The generated application `main` wires surf in with `surf.ServeCommand` and `sur | `surf.Router.Handler` | Returns the compiled middleware chain of one registered route by method and pattern, for an in-process dispatch that must run the same guards, throttles, body limit, locale and recovery as an HTTP request. False for an unknown route or before `surf.BuildRouter` compiled the routes. | | `surf.Router.MountStream` | Registers one streaming route (raw, unbuffered, `http.Flusher` passed through) under the bound plugin with the given middleware names; a route another plugin registered for the method and path fails naming it. | | `surf.MCPBuildTag` | `summercms_mcp`: the build tag that links the MCP module; without it `mcp.enabled` true fails boot. | +| `surf.Router.Close` | Releases what the router created itself (the fallback `surf.MemoryStore` of `surf.BuildRouter`), never a `surf.Store` published on the app; nil-safe and idempotent, and the compiled handler keeps serving. `surf.BuildRouter` closes the router on every error return; `serve` and `route:list` close theirs on exit. | | `surf.Router.AdminPrefix` | The admin mount path and true when `surf.BuildRouter` mounted the cabana admin; false when no plugin registers an admin controller. | | `surf.RouteInfo` | Method, pattern, owning plugin, middleware and raw flag of one route. | | `surf.Use` | Builds a middleware name list for a group. | @@ -140,6 +141,7 @@ The generated application `main` wires surf in with `surf.ServeCommand` and `sur | `surf.BucketProvider` | Implemented by plugins that declare named buckets. | | `surf.FixedWindowLimiter` | The rate limiter behind the `throttle` middleware; `surf.NewFixedWindowLimiter` creates one. | | `surf.Store` | Atomic fixed-window admission; `surf.NewMemoryStore` is the in-process implementation. | +| `surf.MemoryStore` | The in-process `surf.Store`. `surf.MemoryStore.Close` stops its background sweep (nil-safe, idempotent); `surf.MemoryStore.Attempt` keeps counting after Close. | | `surf.ClientIP` | Resolves the client IP, honouring trusted proxies. | | `surf.TrustedProxies` | Parses `http.trusted_proxies` into CIDR prefixes. | | `surf.CORSConfig` | CORS settings; `surf.LoadCORSConfig` reads them from config. | @@ -218,4 +220,4 @@ The `serve` command also opens the database through [lagoon](../lagoon/README.md go test ./modules/surf/... ``` -The tests use `net/http/httptest` and in-memory stores. `TestRateLimitAcrossInstancesSmoke` starts a Redis container and `TestServeLifecycle` a Postgres container (it runs `serve`, sends one request and stops it) through testcontainers-go; both need Docker, and `go test -short ./modules/surf/...` skips them. `TestServeSummarySmoke` and `TestShutdownReportSmoke` check the summary and report builders without a database. +The tests use `net/http/httptest` and in-memory stores. `TestRateLimitAcrossInstancesSmoke` starts a Redis container and `TestServeLifecycle` a Postgres container (it runs `serve`, sends one request and stops it) through testcontainers-go; both need Docker, and `go test -short ./modules/surf/...` skips them. `TestServeSummarySmoke` and `TestShutdownReportSmoke` check the summary and report builders without a database. The close tests (`close_test.go`) check that `surf.MemoryStore.Close` stops the sweep, that `surf.Router.Close` stops the fallback store but never a published one, and that closing 20 routers brings the goroutine count back to its baseline. diff --git a/modules/surf/close_test.go b/modules/surf/close_test.go new file mode 100644 index 00000000..34c80871 --- /dev/null +++ b/modules/surf/close_test.go @@ -0,0 +1,149 @@ +package surf + +import ( + "runtime" + "testing" + "time" +) + +// waitClosed reports whether ch closes within d. +func waitClosed(ch <-chan struct{}, d time.Duration) bool { + select { + case <-ch: + return true + case <-time.After(d): + return false + } +} + +// waitGoroutines polls until runtime.NumGoroutine is at most limit or d +// passes, and returns the last count. +func waitGoroutines(limit int, d time.Duration) int { + deadline := time.Now().Add(d) + n := runtime.NumGoroutine() + for n > limit && time.Now().Before(deadline) { + time.Sleep(10 * time.Millisecond) + n = runtime.NumGoroutine() + } + return n +} + +func routerStore(t *testing.T, r *Router) *MemoryStore { + t.Helper() + if r == nil || r.limiter == nil { + t.Fatal("router has no limiter") + } + ms, ok := r.limiter.store.(*MemoryStore) + if !ok { + t.Fatalf("limiter store = %T, want *MemoryStore", r.limiter.store) + } + return ms +} + +func TestMemoryStoreClose(t *testing.T) { + s := NewMemoryStore(time.Millisecond) + if err := s.Close(); err != nil { + t.Fatalf("Close: %v", err) + } + if !waitClosed(s.done, 2*time.Second) { + t.Fatal("sweep loop still running after Close") + } + if err := s.Close(); err != nil { + t.Fatalf("second Close: %v", err) + } + if err := (*MemoryStore)(nil).Close(); err != nil { + t.Fatalf("nil Close: %v", err) + } + if err := NewMemoryStore(0).Close(); err != nil { + t.Fatalf("no-sweep Close: %v", err) + } + if err := (&MemoryStore{}).Close(); err != nil { + t.Fatalf("zero-value Close: %v", err) + } + if ok, n, _ := s.Attempt("k", 2, time.Minute); !ok || n != 1 { + t.Fatalf("Attempt after Close = %v %d, want true 1", ok, n) + } + if ok, n, _ := s.Attempt("k", 2, time.Minute); !ok || n != 2 { + t.Fatalf("second Attempt after Close = %v %d, want true 2", ok, n) + } + if ok, _, _ := s.Attempt("k", 2, time.Minute); ok { + t.Fatal("third Attempt after Close admitted past max") + } +} + +func TestRouterCloseOwnedStore(t *testing.T) { + for name, build := range map[string]func() (*Router, error){ + "nil app": func() (*Router, error) { return BuildRouter(nil, nil) }, + "config app": func() (*Router, error) { + return BuildRouter(transportApp(t, "development", map[string]string{"http.yaml": transportBodyLimits}), nil) + }, + } { + t.Run(name, func(t *testing.T) { + r, err := build() + if err != nil { + t.Fatal(err) + } + ms := routerStore(t, r) + if err := r.Close(); err != nil { + t.Fatalf("Close: %v", err) + } + if !waitClosed(ms.done, 2*time.Second) { + t.Fatal("fallback store still sweeping after Router.Close") + } + if err := r.Close(); err != nil { + t.Fatalf("second Close: %v", err) + } + }) + } + if err := (*Router)(nil).Close(); err != nil { + t.Fatalf("nil router Close: %v", err) + } + if err := New(nil).Close(); err != nil { + t.Fatalf("New(nil).Close: %v", err) + } +} + +func TestRouterCloseKeepsPublishedStore(t *testing.T) { + app := transportApp(t, "development", map[string]string{"http.yaml": transportBodyLimits}) + published := NewMemoryStore(time.Millisecond) + t.Cleanup(func() { _ = published.Close() }) + if err := app.Publish[Store](published); err != nil { + t.Fatal(err) + } + r, err := BuildRouter(app, nil) + if err != nil { + t.Fatal(err) + } + if got := routerStore(t, r); got != published { + t.Fatal("BuildRouter did not use the published store") + } + if err := r.Close(); err != nil { + t.Fatalf("Close: %v", err) + } + if waitClosed(published.done, 50*time.Millisecond) { + t.Fatal("Router.Close closed a store published on the app") + } +} + +func TestRouterCloseReleasesGoroutines(t *testing.T) { + baseline := runtime.NumGoroutine() + routers := make([]*Router, 0, 20) + for range 20 { + r, err := BuildRouter(nil, nil) + if err != nil { + t.Fatal(err) + } + routers = append(routers, r) + } + if n := runtime.NumGoroutine(); n < baseline+20 { + t.Fatalf("20 routers raised goroutines from %d to %d, want at least +20", baseline, n) + } + for _, r := range routers { + if err := r.Close(); err != nil { + t.Fatal(err) + } + } + if n := waitGoroutines(baseline+2, 2*time.Second); n > baseline+2 { + t.Fatalf("goroutines = %d after closing 20 routers, want at most %d", n, baseline+2) + } +} diff --git a/modules/surf/limiter_store.go b/modules/surf/limiter_store.go index a77ddc54..d68fd026 100644 --- a/modules/surf/limiter_store.go +++ b/modules/surf/limiter_store.go @@ -17,12 +17,17 @@ type counterEntry struct { resetAt time.Time } -// MemoryStore is an in-process, mutex-guarded Store. +// MemoryStore is an in-process, mutex-guarded Store. Close stops its +// background sweep. type MemoryStore struct { - mu sync.Mutex - entries map[string]*counterEntry - sweep time.Duration - stop chan struct{} + mu sync.Mutex + entries map[string]*counterEntry + sweep time.Duration + stop chan struct{} + closeOnce sync.Once + // done is closed when the sweep loop has returned (at once when there + // is no sweep goroutine). + done chan struct{} } // NewMemoryStore returns an in-process, mutex-guarded Store. sweep controls @@ -34,14 +39,31 @@ func NewMemoryStore(sweep time.Duration) *MemoryStore { entries: make(map[string]*counterEntry), sweep: sweep, stop: make(chan struct{}), + done: make(chan struct{}), } if sweep > 0 { go s.loop() + } else { + close(s.done) } return s } +// Close stops the background sweep goroutine. It only stops the sweep: +// Attempt keeps working after Close (expiry is checked lazily), so a handler +// still serving after Close is safe. Close is safe on a nil store, on a +// store without a sweep and when called more than once; it always returns +// nil. +func (s *MemoryStore) Close() error { + if s == nil || s.stop == nil { + return nil + } + s.closeOnce.Do(func() { close(s.stop) }) + return nil +} + func (s *MemoryStore) loop() { + defer close(s.done) ticker := time.NewTicker(s.sweep) defer ticker.Stop() for { diff --git a/modules/surf/routelist_command.go b/modules/surf/routelist_command.go index a048769a..5493b9be 100644 --- a/modules/surf/routelist_command.go +++ b/modules/surf/routelist_command.go @@ -45,6 +45,7 @@ scripts.`, if err != nil { return err } + defer r.Close() infos := r.Routes() if in != nil { if bonfire.FlagBool(in, jsonFlagName) { diff --git a/modules/surf/router.go b/modules/surf/router.go index bd3aeed4..ffb7cd01 100644 --- a/modules/surf/router.go +++ b/modules/surf/router.go @@ -8,6 +8,7 @@ import ( "math" "net/http" "strings" + "sync" "time" "git.golem15.com/golem15/summercms/modules/backpack" @@ -85,6 +86,39 @@ type Router struct { // pages writes the HTML error page for browser navigations; nil keeps // every error response as it was (errorpage.go). pages *errorPages + // closers release what the router created itself (the fallback + // MemoryStore, the MCP endpoint it mounted); Close runs them once, in + // reverse order. + closers []func() error + closeOnce sync.Once + closeErr error +} + +// own registers fn to run on Close. +func (r *Router) own(fn func() error) { + r.closers = append(r.closers, fn) +} + +// Close releases what the router created itself: the in-process MemoryStore +// BuildRouter falls back to and, with MCP enabled, the background work of +// the endpoint it mounted. It never closes a surf.Store or anything else +// published on the app; that belongs to its publisher. The compiled handler +// keeps serving after Close. Close is safe on a nil router and when called +// more than once; later calls return the first call's result. +func (r *Router) Close() error { + if r == nil { + return nil + } + r.closeOnce.Do(func() { + var errs []error + for i := len(r.closers) - 1; i >= 0; i-- { + if err := r.closers[i](); err != nil { + errs = append(errs, err) + } + } + r.closeErr = errors.Join(errs...) + }) + return r.closeErr } var ( @@ -543,9 +577,22 @@ func Assemble(app *backpack.App, plugins []party.Plugin) (http.Handler, error) { } // BuildRouter registers plugin middleware and routes without compiling ServeMux, -// so callers (route:list) can inspect Routes() after a successful boot. +// so callers (route:list) can inspect Routes() after a successful boot. A +// caller that also serves uses AssembleRouter, which builds once. The caller +// owns the returned router and calls Router.Close when done with it; on an +// error the router is already closed. func BuildRouter(app *backpack.App, plugins []party.Plugin) (*Router, error) { r := New(corsOrigins(app)) + if err := r.build(app, plugins); err != nil { + _ = r.Close() + return nil, err + } + return r, nil +} + +// build is BuildRouter's body: everything it creates that needs releasing is +// registered with own, so BuildRouter can close the router on any error. +func (r *Router) build(app *backpack.App, plugins []party.Plugin) error { r.app = app r.pages = newErrorPages(app) trusted := TrustedProxies(nil) @@ -561,24 +608,28 @@ func BuildRouter(app *backpack.App, plugins []party.Plugin) (*Router, error) { } } if store == nil { - // longest bucket decay is 1 minute; sweep at 2x - store = NewMemoryStore(2 * time.Minute) + // longest bucket decay is 1 minute; sweep at 2x. The router made + // it, so the router closes it; a published store is its + // publisher's. + ms := NewMemoryStore(2 * time.Minute) + r.own(ms.Close) + store = ms } lim := NewFixedWindowLimiter(store, trusted) r.limiter = lim if err := r.RegisterMiddlewareFactory("surf", "throttle", func(param string) pact.Middleware { return lim.Middleware(param) }); err != nil { - return nil, err + return err } if err := r.RegisterMiddlewareFactory("surf", "body.limit", func(param string) pact.Middleware { // Limit is applied outermost-inside-recovery in wrap(); the factory only occupies the name. return func(next http.Handler) http.Handler { return next } }); err != nil { - return nil, err + return err } if err := r.RegisterMiddleware("surf", "locale.from-principal", LocaleFromPrincipal); err != nil { - return nil, err + return err } r.corsProfiles = map[string]*corsPolicy{} if err := r.RegisterMiddlewareFactory("surf", "cors", func(param string) pact.Middleware { @@ -586,7 +637,7 @@ func BuildRouter(app *backpack.App, plugins []party.Plugin) (*Router, error) { // itself, outermost, and never calls this factory for it. return r.corsProfiles[param].profileMiddleware }); err != nil { - return nil, err + return err } turnstile, turnstileField := buildTurnstile(app) if err := r.RegisterMiddlewareFactory("surf", "turnstile", func(param string) pact.Middleware { @@ -600,17 +651,17 @@ func BuildRouter(app *backpack.App, plugins []party.Plugin) (*Router, error) { clientIP := func(req *http.Request) string { return ClientIP(req, trusted) } return turnstile.Middleware(param == "closed", turnstileField, clientIP, write) }); err != nil { - return nil, err + return err } if app != nil { corsCfg, err := LoadCORSConfig(app.Config) if err != nil { - return nil, err + return err } r.corsCfg = corsCfg profiles, err := LoadCORSProfiles(app.Config) if err != nil { - return nil, err + return err } for name, pc := range profiles { r.corsProfiles[name] = newCORSPolicy(pc) @@ -618,11 +669,11 @@ func BuildRouter(app *backpack.App, plugins []party.Plugin) (*Router, error) { if app.Config != nil { d, err := requiredBytes(app, "http.body_limits.default_bytes") if err != nil { - return nil, err + return err } u, err := requiredBytes(app, "http.body_limits.upload_bytes") if err != nil { - return nil, err + return err } r.defaultBytes, r.uploadBytes = d, u } @@ -631,7 +682,7 @@ func BuildRouter(app *backpack.App, plugins []party.Plugin) (*Router, error) { if hm, ok := p.(pact.HasMiddleware); ok { for name, fn := range hm.Middlewares() { if err := r.RegisterMiddleware(p.ID(), name, fn); err != nil { - return nil, err + return err } } } @@ -640,7 +691,7 @@ func BuildRouter(app *backpack.App, plugins []party.Plugin) (*Router, error) { if hf, ok := p.(pact.HasMiddlewareFactories); ok { for name, fn := range hf.MiddlewareFactories() { if err := r.RegisterMiddlewareFactory(p.ID(), name, fn); err != nil { - return nil, err + return err } } } @@ -649,7 +700,7 @@ func BuildRouter(app *backpack.App, plugins []party.Plugin) (*Router, error) { if hh, ok := p.(pact.HasHouseMiddleware); ok { for name, fn := range hh.HouseMiddlewares() { if err := r.RegisterHouseMiddleware(p.ID(), name, fn); err != nil { - return nil, err + return err } } } @@ -658,7 +709,7 @@ func BuildRouter(app *backpack.App, plugins []party.Plugin) (*Router, error) { if bp, ok := p.(BucketProvider); ok { for name, b := range bp.Buckets() { if err := lim.RegisterBucket(p.ID(), name, b); err != nil { - return nil, err + return err } } } @@ -667,28 +718,28 @@ func BuildRouter(app *backpack.App, plugins []party.Plugin) (*Router, error) { r.BindPlugin(p.ID()) if hr, ok := p.(pact.HasRoutes); ok { if err := hr.Routes(r); err != nil { - return nil, fmt.Errorf("surf: routes %s: %w", p.ID(), err) + return fmt.Errorf("surf: routes %s: %w", p.ID(), err) } } } if err := mountUploads(r, app); err != nil { - return nil, err + return err } if err := mountAPIDocs(r, app, plugins); err != nil { - return nil, err + return err } admin, err := cabana.Activate(app, plugins) if err != nil { - return nil, err + return err } if admin != nil { if err := r.RegisterMiddleware("summercms.cabana", "backend", admin.Middleware); err != nil { - return nil, err + return err } r.BindPlugin("summercms.cabana") admin.Mount(r) if err := r.checkAdminPrefix(admin.Prefix); err != nil { - return nil, err + return err } r.adminPrefix = admin.Prefix r.pages.setAdminPrefix(admin.Prefix) @@ -698,20 +749,20 @@ func BuildRouter(app *backpack.App, plugins []party.Plugin) (*Router, error) { // route and the admin (D-30: nothing when off; D-33: tag required). if app != nil && app.Config != nil && app.Config.Bool("mcp.enabled") { if mountPier == nil { - return nil, errors.New("surf: mcp.enabled is true but this binary was built without the summercms_mcp tag (summer.yaml build.tags)") + return errors.New("surf: mcp.enabled is true but this binary was built without the summercms_mcp tag (summer.yaml build.tags)") } if err := mountPier(r, app, plugins); err != nil { - return nil, err + return err } } if err := r.compileHandlers(); err != nil { - return nil, err + return err } warnTurnstileWithoutSecret(app, turnstile, r.routes) if err := r.addCORSPreflights(); err != nil { - return nil, err + return err } - return r, nil + return nil } // buildTurnstile returns the verifier behind turnstile:open|closed and the diff --git a/modules/surf/serve.go b/modules/surf/serve.go index 08713511..fff349ed 100644 --- a/modules/surf/serve.go +++ b/modules/surf/serve.go @@ -80,15 +80,17 @@ clean stop and 1 when draining fails.`, return err } defer bucket.Close() - coolerHandle, err := installCooler(ctx, app) + coolerHandle, fallback, err := installCooler(ctx, app) if err != nil { return err } defer coolerHandle.Close() + defer fallback.Close() r, err := BuildRouter(app, plugins) if err != nil { return err } + defer r.Close() h, err := r.compile() if err != nil { return err @@ -227,26 +229,32 @@ func publishUploads(ctx context.Context, app *backpack.App) (*blob.Bucket, error // publishes its Redis-backed Store (falling back to a per-instance // MemoryStore while Redis does not answer) so BuildRouter's limiter shares // counters across instances. Only serve calls it: route:list never dials Redis. -func installCooler(ctx context.Context, app *backpack.App) (*cooler.Handle, error) { +// It also returns that fallback MemoryStore (nil when Redis is off), which +// serve created and so closes on shutdown; Close is nil-safe. +func installCooler(ctx context.Context, app *backpack.App) (*cooler.Handle, *MemoryStore, error) { h, err := cooler.Install(ctx, app) if err != nil { - return nil, err + return nil, nil, err } + var fallback *MemoryStore if h.RateLimitRedis() { // longest bucket decay is 1 minute; sweep at 2x (as BuildRouter) - store := h.RateLimitStore(NewMemoryStore(2 * time.Minute)) + fallback = NewMemoryStore(2 * time.Minute) + store := h.RateLimitStore(fallback) if err := app.Publish[Store](store); err != nil { + _ = fallback.Close() _ = h.Close() - return nil, fmt.Errorf("surf: %w", err) + return nil, nil, fmt.Errorf("surf: %w", err) } // cabana looks this up for the mail test-send limit (and any other // fixed-window admission outside BuildRouter's throttle middleware). if err := app.Publish[cooler.Attempter](store); err != nil { + _ = fallback.Close() _ = h.Close() - return nil, fmt.Errorf("surf: %w", err) + return nil, nil, fmt.Errorf("surf: %w", err) } } - return h, nil + return h, fallback, nil } // adminCreateCommand is the command the startup hints name for creating the diff --git a/modules/surf/store_lookup_test.go b/modules/surf/store_lookup_test.go index b9184756..8ef3fea3 100644 --- a/modules/surf/store_lookup_test.go +++ b/modules/surf/store_lookup_test.go @@ -119,11 +119,11 @@ func TestRateLimitAcrossInstancesSmoke(t *testing.T) { instance := func() http.Handler { app := redisStoreApp(t, url) - hd, err := installCooler(ctx, app) + hd, fallback, err := installCooler(ctx, app) if err != nil { t.Fatal(err) } - t.Cleanup(func() { _ = hd.Close() }) + t.Cleanup(func() { _ = fallback.Close(); _ = hd.Close() }) p := assemblePlugin{id: "golem15.demo", path: "/ping", use: []string{"throttle:2,1"}} h, err := Assemble(app, []party.Plugin{p}) if err != nil { @@ -273,11 +273,11 @@ func TestInstallCoolerPublishesAttempter(t *testing.T) { t.Fatal(err) } app := backpack.New(cfg) - hd, err := installCooler(context.Background(), app) + hd, fallback, err := installCooler(context.Background(), app) if err != nil { t.Fatal(err) } - t.Cleanup(func() { _ = hd.Close() }) + t.Cleanup(func() { _ = fallback.Close(); _ = hd.Close() }) if _, ok := app.Lookup[Store](); ok { t.Fatal("memory store must not publish surf.Store") } @@ -293,11 +293,11 @@ func TestInstallCoolerPublishesAttempter(t *testing.T) { } t.Cleanup(mr.Close) app := redisStoreApp(t, "redis://"+mr.Addr()+"/0") - hd, err := installCooler(context.Background(), app) + hd, fallback, err := installCooler(context.Background(), app) if err != nil { t.Fatal(err) } - t.Cleanup(func() { _ = hd.Close() }) + t.Cleanup(func() { _ = fallback.Close(); _ = hd.Close() }) store, ok := app.Lookup[Store]() if !ok { t.Fatal("redis path must publish surf.Store")