edge: bind the guard's mint function once, in New
Every guarded request took e.mintRef as a method value, an allocation for a function most requests never call (final review M1). New binds it once; the per-request guard state is now the struct alone. An Edge built without New falls back to the method value, so a block never calls nil.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+31
−1
internal/edge/edge.go+6 −1internal/edge/guard.go+12 −0internal/edge/guard_test.go+13 −0
CI failed — run details
diff --git a/internal/edge/edge.go b/internal/edge/edge.go| index 135782e3d..74e3942f8 100644 |
| --- a/internal/edge/edge.go |
| +++ b/internal/edge/edge.go |
| @@ -256,6 +256,10 @@ type Edge struct { |
| // each rule's own mode, and in catalogue version 1 that is only ever to |
| // watch. |
| guard guardMode |
| + // guardMint is mintRef, bound once in New: a method value taken per |
| + // request would allocate for every guarded request, and most never |
| + // mint a code. |
| + guardMint func(reqrec.Tier, string) string |
| // issuer, when set, gates certificate ISSUANCE on this box holding the |
| // deployment's housekeeper lease (certsync.go). It arrives through |
| // WithIssuerGate because internal/host imports this package and never the |
| @@ -430,6 +434,7 @@ func New(reg *registry.Registry, act Activator) (*Edge, error) { |
| lbHealth: lbHealthFromEnv(os.Getenv, act), rateMode: rateModeFromEnv(), rateLimiter: ratelimit.NewLimiter(time.Now, 100_000)} |
| e.geo = geo.NewSwappable() |
| e.geoLookup = e.geo.Lookup |
| + e.guardMint = e.mintRef |
| // Generations (zero-downtime spec §7.2), before the startup Reload so |
| // its first table build already proxies through them. |
| if gs, ok := act.(GenSource); ok && gs.GenerationsOn() { |
| @@ -1736,7 +1741,7 @@ func (e *Edge) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| // checked, and is recorded, by the hub that forwarded it, so |
| // checking it again here would answer or count it twice. |
| if e.guard != guardOff && !isDoor && !hopDoorFrom(r.Context()) && !fromTunnel(r.Context()) { |
| - out.guard = &guardState{mode: e.guard, mint: e.mintRef} |
| + out.guard = e.newGuardState() |
| } |
| defer func() { |
| if m := e.usageMeter.Load(); m != nil { |
diff --git a/internal/edge/guard.go b/internal/edge/guard.go| index 40f6eef0b..dd2ee7f73 100644 |
| --- a/internal/edge/guard.go |
| +++ b/internal/edge/guard.go |
| @@ -83,6 +83,18 @@ type guardState struct { |
| verdict *guardVerdict |
| } |
| +// newGuardState is the guard's state for one request. It costs the struct |
| +// and nothing else: the mint function was bound in New. An Edge built |
| +// without New has none bound and pays for the method value instead, so a |
| +// block never calls a nil func. |
| +func (e *Edge) newGuardState() *guardState { |
| + mint := e.guardMint |
| + if mint == nil { |
| + mint = e.mintRef |
| + } |
| + return &guardState{mode: e.guard, mint: mint} |
| +} |
| + |
| // guardDecide notes hit as r's verdict and, when the mode blocks the rule, |
| // answers r with the block page and reports true: the caller must then stop |
| // serving. It does nothing when the guard is not running for r, or when a |
diff --git a/internal/edge/guard_test.go b/internal/edge/guard_test.go| index 9ddeb347b..bbe424cb9 100644 |
| --- a/internal/edge/guard_test.go |
| +++ b/internal/edge/guard_test.go |
| @@ -417,3 +417,16 @@ func TestGuardSkipsDoorCalls(t *testing.T) { |
| blocked(t, tc.name, g, serve(g, tc.req(g))) |
| } |
| } |
| + |
| +// Review M1: the guard's per-request state is the struct alone. The mint |
| +// function is bound once in New, not taken as a method value per request. |
| +func TestGuardStateAllocs(t *testing.T) { |
| + g := guardEdge(t, false, false, http.StatusOK, "app") |
| + var keep *guardState |
| + if n := testing.AllocsPerRun(100, func() { keep = g.e.newGuardState() }); n != 1 { |
| + t.Fatalf("newGuardState allocates %v times, want 1 (the struct)", n) |
| + } |
| + if keep.mint == nil { |
| + t.Fatal("no mint function bound") |
| + } |
| +} |