edge: wake the cert warm when its host set grows (#386)
A new custom domain waited for the issuer's next hourly warm pass: only the issuer orders, steering keeps visitors on other boxes, and nothing woke the warm. carlos.international waited 19:24 -> 19:54 on the flagship. The warm now runs a pass on a wake that brings a host it has never seen (not warmed, not backing off, not already on the DNS schedule); any other wake is dropped and the hourly timer is untouched. Wakes come from the edge reload hook (now additive, so the wildcard keeps its slot) and a new home-map hook on ServedHosts changes (domains homed on other boxes). A new host that fails the DNS gate is re-checked every minute for its first 10 minutes of misses, every 5 until an hour, then hourly as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Human
Prompt
Can we see why certs are taking 20 mins to be assigned? Yes
From this commit's own message. The prompt ledger has no entries for it — which is what a squashed, rebased or amended commit looks like, and what every commit looks like in a repo without a ledger.
Summary
Cert warm wake on new hosts plus a fast DNS-miss schedule; reload hook becomes a fan-out; home map gains a ServedHosts wake.
Agent notes
Mutation-checked: each of nudge, the hasNewHost filter, the DNS retry, the fan-out and the served wake has a test that fails without it. No production-wiring test of RunWithActivatorReady's two registration lines.
4 files changed,
+607
−21
internal/edge/certwarm.go+154 −13internal/edge/certwarm_wake_test.go+370 −0internal/edge/edge.go+34 −7internal/edge/homemap.go+49 −1
CI failed — run details
diff --git a/internal/edge/certwarm.go b/internal/edge/certwarm.go| index 3e4700a74..a16f28ef6 100644 |
| --- a/internal/edge/certwarm.go |
| +++ b/internal/edge/certwarm.go |
| @@ -63,6 +63,16 @@ const ( |
| warmOrdersPerPass = 10 |
| // warmCappedDelay is how soon to come back with capped work remaining. |
| warmCappedDelay = time.Minute |
| + // The DNS-miss schedule for a NEW host (#386). A customer usually |
| + // finishes their DNS a few minutes after attaching, so the first ten |
| + // minutes of misses are re-checked every minute and the rest of the first |
| + // hour every five. After an hour the host waits for the hourly pass like |
| + // everything else, so an attach whose owner never points it costs the |
| + // issuer nothing past its first hour. |
| + warmDNSRetryFast = time.Minute |
| + warmDNSFastFor = 10 * time.Minute |
| + warmDNSRetrySlow = 5 * time.Minute |
| + warmDNSSlowFor = time.Hour |
| ) |
| // certWarm proactively orders and renews per-host certificates. |
| @@ -113,6 +123,19 @@ type certWarm struct { |
| // so a first order can be told from a renewal re-arm for the order cap |
| // and for logging. |
| warmed map[string]bool |
| + // dnsMissSince is when each never-warmed host FIRST failed the DNS gate, |
| + // for the DNS-miss schedule. Cleared when the host passes the gate and |
| + // pruned with the set. |
| + dnsMissSince map[string]time.Time |
| + |
| + // wake asks the loop for an early pass (nudge). Buffered one deep: a |
| + // wake already pending covers any that arrive behind it. |
| + wake chan struct{} |
| + // now is the clock; a field so the DNS-miss schedule is testable. |
| + now func() time.Time |
| + // passHook, when set, runs at the start of every pass the loop runs. |
| + // Tests only. |
| + passHook func() |
| lastStandDownLog time.Time |
| // lastSweep and lastExpirySweep pace the cert sweep (certsweep.go). |
| @@ -126,13 +149,16 @@ type certWarm struct { |
| func newCertWarm(hosts func() []string, getCert func(*tls.ClientHelloInfo) (*tls.Certificate, error), pointsHere func(string) bool, issuer IssuerGate, store release.Store) *certWarm { |
| return &certWarm{ |
| - hosts: hosts, |
| - getCert: getCert, |
| - pointsHere: pointsHere, |
| - issuer: issuer, |
| - store: store, |
| - nextAttempt: map[string]time.Time{}, |
| - warmed: map[string]bool{}, |
| + hosts: hosts, |
| + getCert: getCert, |
| + pointsHere: pointsHere, |
| + issuer: issuer, |
| + store: store, |
| + nextAttempt: map[string]time.Time{}, |
| + warmed: map[string]bool{}, |
| + dnsMissSince: map[string]time.Time{}, |
| + wake: make(chan struct{}, 1), |
| + now: time.Now, |
| } |
| } |
| @@ -145,10 +171,102 @@ func (c *certWarm) issuerGate() (context.Context, bool) { |
| return c.issuer() |
| } |
| -func (c *certWarm) run() { |
| +func (c *certWarm) run() { c.loop(nil) } |
| + |
| +// loop runs a pass, then waits for whichever comes first: the delay that pass |
| +// asked for, or a wake that brings a host the warm has never seen (#386). |
| +// |
| +// Without the wake a custom domain attached between passes waited up to an |
| +// hour for its certificate: only the issuer orders, latency steering keeps |
| +// visitors on the other boxes, so no first handshake ever reached the order |
| +// path either. A wake carrying nothing new is dropped and leaves the timer |
| +// alone, because wakes come from every route reload and every home-map change |
| +// in the fleet, and a pass costs a records LIST and a re-arm of every host. |
| +// |
| +// stop ends the loop; nil runs it for the life of the process. |
| +func (c *certWarm) loop(stop <-chan struct{}) { |
| + timer := time.NewTimer(c.runPass()) |
| + defer timer.Stop() |
| for { |
| - time.Sleep(c.pass()) |
| + select { |
| + case <-stop: |
| + return |
| + case <-timer.C: |
| + timer.Reset(c.runPass()) |
| + case <-c.wake: |
| + if !c.hasNewHost() { |
| + continue |
| + } |
| + timer.Stop() |
| + timer.Reset(c.runPass()) |
| + } |
| + } |
| +} |
| + |
| +func (c *certWarm) runPass() time.Duration { |
| + if c.passHook != nil { |
| + c.passHook() |
| + } |
| + return c.pass() |
| +} |
| + |
| +// nudge asks for an early pass. It never blocks: it is called from the |
| +// edge's reload path and the home map's refresh. Safe on a nil warm, which is |
| +// what a deployment without one has. |
| +func (c *certWarm) nudge() { |
| + if c == nil { |
| + return |
| + } |
| + select { |
| + case c.wake <- struct{}{}: |
| + default: |
| + } |
| +} |
| + |
| +// hasNewHost reports whether a pass now could place an order nobody has |
| +// scheduled yet: this box is the issuer, and the set holds a host that has |
| +// never been warmed, is not backing off, and is not already on the DNS-miss |
| +// schedule. Everything else in the set has its own next pass already. |
| +func (c *certWarm) hasNewHost() bool { |
| + if _, ok := c.issuerGate(); !ok { |
| + return false |
| + } |
| + now := c.now() |
| + hosts := c.hosts() |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + for _, h := range hosts { |
| + if c.warmed[h] { |
| + continue |
| + } |
| + if until, ok := c.nextAttempt[h]; ok && now.Before(until) { |
| + continue |
| + } |
| + if _, ok := c.dnsMissSince[h]; ok { |
| + continue |
| + } |
| + return true |
| } |
| + return false |
| +} |
| + |
| +// dnsMissed records that a never-warmed host failed the DNS gate and returns |
| +// how soon to check it again, 0 meaning the ordinary hourly pass. |
| +func (c *certWarm) dnsMissed(host string, now time.Time) time.Duration { |
| + c.mu.Lock() |
| + first, ok := c.dnsMissSince[host] |
| + if !ok { |
| + first = now |
| + c.dnsMissSince[host] = now |
| + } |
| + c.mu.Unlock() |
| + switch missing := now.Sub(first); { |
| + case missing < warmDNSFastFor: |
| + return warmDNSRetryFast |
| + case missing < warmDNSSlowFor: |
| + return warmDNSRetrySlow |
| + } |
| + return 0 |
| } |
| // pass warms every host in the set once and returns how long to wait. |
| @@ -166,6 +284,9 @@ func (c *certWarm) pass() time.Duration { |
| return warmInterval |
| } |
| if len(hosts) == 0 { |
| + c.mu.Lock() |
| + clear(c.dnsMissSince) |
| + c.mu.Unlock() |
| c.sweep(ctx, time.Now()) |
| return warmInterval |
| } |
| @@ -181,8 +302,11 @@ func (c *certWarm) pass() time.Duration { |
| inSet[h] = true |
| } |
| orders, capped := 0, false |
| + // dnsRetry is the soonest DNS-miss re-check any new host asked for, 0 |
| + // when none did. |
| + var dnsRetry time.Duration |
| for _, h := range hosts { |
| - now := time.Now() |
| + now := c.now() |
| c.mu.Lock() |
| until, backing := c.nextAttempt[h] |
| seen := c.warmed[h] |
| @@ -209,8 +333,14 @@ func (c *certWarm) pass() time.Duration { |
| continue |
| } |
| if !c.pointsHere(h) { |
| + if d := c.dnsMissed(h, now); d > 0 && (dnsRetry == 0 || d < dnsRetry) { |
| + dnsRetry = d |
| + } |
| continue |
| } |
| + c.mu.Lock() |
| + delete(c.dnsMissSince, h) |
| + c.mu.Unlock() |
| orders++ |
| } |
| if err := c.warmOne(h); err != nil { |
| @@ -235,14 +365,25 @@ func (c *certWarm) pass() time.Duration { |
| } |
| } |
| c.prune(ctx, records, inSet) |
| + c.mu.Lock() |
| + for h := range c.dnsMissSince { |
| + if !inSet[h] { |
| + delete(c.dnsMissSince, h) |
| + } |
| + } |
| + c.mu.Unlock() |
| // Housekeeping last: it is the least urgent thing this pass does, and |
| // running it after the orders means a slow bucket delays litter removal |
| // rather than certificate issuance (certsweep.go). |
| c.sweep(ctx, time.Now()) |
| + delay := warmInterval |
| if capped { |
| - return warmCappedDelay |
| + delay = warmCappedDelay |
| + } |
| + if dnsRetry > 0 && dnsRetry < delay { |
| + delay = dnsRetry |
| } |
| - return warmInterval |
| + return delay |
| } |
| // warmOne asks autocert for host's certificate, which mints it when absent |
| @@ -270,7 +411,7 @@ func (c *certWarm) fail(ctx context.Context, host string, err error, prior certw |
| } else { |
| log.Printf("edge: cert warm for %s: %v — retrying in %s", host, err, backoff) |
| } |
| - now := time.Now() |
| + now := c.now() |
| next := now.Add(backoff) |
| c.mu.Lock() |
| c.nextAttempt[host] = next |
diff --git a/internal/edge/certwarm_wake_test.go b/internal/edge/certwarm_wake_test.go| new file mode 100644 |
| index 000000000..c5f942590 |
| --- /dev/null |
| +++ b/internal/edge/certwarm_wake_test.go |
| @@ -0,0 +1,370 @@ |
| +package edge |
| + |
| +import ( |
| + "context" |
| + "crypto/tls" |
| + "sync" |
| + "sync/atomic" |
| + "testing" |
| + "time" |
| +) |
| + |
| +// #386: a newly attached custom domain waited for the issuer's next hourly |
| +// pass (20+ minutes on the flagship), because nothing woke the warm. These |
| +// pin the wake and the fast DNS-miss retry that replace that wait. |
| + |
| +// fakeClock is a settable clock for the DNS-miss schedule. |
| +type fakeClock struct { |
| + mu sync.Mutex |
| + t time.Time |
| +} |
| + |
| +func (f *fakeClock) now() time.Time { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + return f.t |
| +} |
| + |
| +func (f *fakeClock) advance(d time.Duration) { |
| + f.mu.Lock() |
| + f.t = f.t.Add(d) |
| + f.mu.Unlock() |
| +} |
| + |
| +// A new host whose DNS does not point here yet is checked again within |
| +// minutes, not at the next hourly pass. The customer usually finishes their |
| +// DNS a few minutes after attaching, and every minute after that is a minute |
| +// their domain is down for no reason. The schedule slows as the miss ages, |
| +// and a host that has missed for an hour goes back to the hourly pass, so an |
| +// abandoned attach does not keep the issuer busy. |
| +func TestWarmRetriesANewHostsDNSMissWithinMinutes(t *testing.T) { |
| + clock := &fakeClock{t: time.Unix(1_800_000_000, 0)} |
| + w := newCertWarm( |
| + func() []string { return []string{"www.customer.test"} }, |
| + func(*tls.ClientHelloInfo) (*tls.Certificate, error) { return &tls.Certificate{}, nil }, |
| + func(string) bool { return false }, |
| + nil, |
| + nil, |
| + ) |
| + w.now = clock.now |
| + if d := w.pass(); d != warmDNSRetryFast { |
| + t.Fatalf("first miss: delay = %s, want %s", d, warmDNSRetryFast) |
| + } |
| + clock.advance(warmDNSFastFor + time.Second) |
| + if d := w.pass(); d != warmDNSRetrySlow { |
| + t.Fatalf("after %s of misses: delay = %s, want %s", warmDNSFastFor, d, warmDNSRetrySlow) |
| + } |
| + clock.advance(warmDNSSlowFor) |
| + if d := w.pass(); d != warmInterval { |
| + t.Fatalf("after an hour of misses: delay = %s, want the hourly %s", d, warmInterval) |
| + } |
| +} |
| + |
| +// The miss clock starts at the FIRST miss, not at every pass: a host that |
| +// keeps missing must age into the slower schedule. |
| +func TestWarmDNSMissClockStartsAtTheFirstMiss(t *testing.T) { |
| + clock := &fakeClock{t: time.Unix(1_800_000_000, 0)} |
| + w := newCertWarm( |
| + func() []string { return []string{"www.customer.test"} }, |
| + func(*tls.ClientHelloInfo) (*tls.Certificate, error) { return &tls.Certificate{}, nil }, |
| + func(string) bool { return false }, |
| + nil, |
| + nil, |
| + ) |
| + w.now = clock.now |
| + for i := 0; i < 11; i++ { |
| + w.pass() |
| + clock.advance(time.Minute) |
| + } |
| + if d := w.pass(); d != warmDNSRetrySlow { |
| + t.Fatalf("after 11 one-minute passes: delay = %s, want %s", d, warmDNSRetrySlow) |
| + } |
| +} |
| + |
| +// Once the name points here and the order goes out, its miss history is |
| +// gone: a later miss (the host leaves and comes back) starts a fresh fast |
| +// schedule rather than inheriting an old one. |
| +func TestWarmDNSMissClockClearsWhenTheHostPoints(t *testing.T) { |
| + clock := &fakeClock{t: time.Unix(1_800_000_000, 0)} |
| + pointing := false |
| + w := newCertWarm( |
| + func() []string { return []string{"www.customer.test"} }, |
| + func(*tls.ClientHelloInfo) (*tls.Certificate, error) { return &tls.Certificate{}, nil }, |
| + func(string) bool { return pointing }, |
| + nil, |
| + nil, |
| + ) |
| + w.now = clock.now |
| + w.pass() |
| + pointing = true |
| + clock.advance(time.Minute) |
| + if d := w.pass(); d != warmInterval { |
| + t.Fatalf("ordered pass: delay = %s, want %s", d, warmInterval) |
| + } |
| + w.mu.Lock() |
| + defer w.mu.Unlock() |
| + if len(w.dnsMissSince) != 0 { |
| + t.Fatalf("miss history survived the order: %v", w.dnsMissSince) |
| + } |
| +} |
| + |
| +// A host that leaves the set takes its miss history with it. |
| +func TestWarmDNSMissHistoryIsPrunedWithTheSet(t *testing.T) { |
| + hosts := []string{"gone.customer.test"} |
| + w := newCertWarm( |
| + func() []string { return hosts }, |
| + func(*tls.ClientHelloInfo) (*tls.Certificate, error) { return &tls.Certificate{}, nil }, |
| + func(string) bool { return false }, |
| + nil, |
| + nil, |
| + ) |
| + w.pass() |
| + hosts = []string{"other.customer.test"} |
| + w.pass() |
| + w.mu.Lock() |
| + defer w.mu.Unlock() |
| + if _, ok := w.dnsMissSince["gone.customer.test"]; ok { |
| + t.Fatal("a host that left the set kept its miss history") |
| + } |
| +} |
| + |
| +// The fast retry is for NEW orders only. A warmed host's re-arm ignores DNS |
| +// (TestWarmRenewalReArmIgnoresTheDNSGate), so its resolver being sick must not |
| +// shorten the pass either. |
| +func TestWarmDNSRetryDoesNotApplyToWarmedHosts(t *testing.T) { |
| + pointing := true |
| + w := newCertWarm( |
| + func() []string { return []string{"www.customer.test"} }, |
| + func(*tls.ClientHelloInfo) (*tls.Certificate, error) { return &tls.Certificate{}, nil }, |
| + func(string) bool { return pointing }, |
| + nil, |
| + nil, |
| + ) |
| + w.pass() |
| + pointing = false |
| + if d := w.pass(); d != warmInterval { |
| + t.Fatalf("delay = %s, want %s — a warmed host is not DNS-gated", d, warmInterval) |
| + } |
| +} |
| + |
| +// startWarm runs the warm's loop until the test ends. |
| +func startWarm(t *testing.T, w *certWarm) { |
| + t.Helper() |
| + stop := make(chan struct{}) |
| + done := make(chan struct{}) |
| + go func() { |
| + defer close(done) |
| + w.loop(stop) |
| + }() |
| + t.Cleanup(func() { |
| + close(stop) |
| + <-done |
| + }) |
| +} |
| + |
| +// waitCount polls an atomic counter until it reaches want. |
| +func waitCount(t *testing.T, n *atomic.Int32, want int32, what string) { |
| + t.Helper() |
| + deadline := time.Now().Add(2 * time.Second) |
| + for n.Load() < want { |
| + if time.Now().After(deadline) { |
| + t.Fatalf("%s = %d; want %d", what, n.Load(), want) |
| + } |
| + time.Sleep(time.Millisecond) |
| + } |
| +} |
| + |
| +// THE fix. A host that appears in the set between hourly passes is ordered |
| +// when the warm is woken, not an hour later. |
| +func TestWarmWakeOrdersANewHostAtOnce(t *testing.T) { |
| + var mu sync.Mutex |
| + hosts := []string{} |
| + var orders atomic.Int32 |
| + w := newCertWarm( |
| + func() []string { |
| + mu.Lock() |
| + defer mu.Unlock() |
| + return append([]string(nil), hosts...) |
| + }, |
| + func(h *tls.ClientHelloInfo) (*tls.Certificate, error) { |
| + if h.ServerName == "new.customer.test" { |
| + orders.Add(1) |
| + } |
| + return &tls.Certificate{}, nil |
| + }, |
| + func(string) bool { return true }, |
| + nil, |
| + nil, |
| + ) |
| + var passes atomic.Int32 |
| + w.passHook = func() { passes.Add(1) } |
| + startWarm(t, w) |
| + waitCount(t, &passes, 1, "passes") // the boot pass, over an empty set |
| + mu.Lock() |
| + hosts = []string{"new.customer.test"} |
| + mu.Unlock() |
| + w.nudge() |
| + waitCount(t, &orders, 1, "orders for the new host") |
| +} |
| + |
| +// A wake with nothing new in the set runs no pass. Reloads happen on every |
| +// deploy anywhere in the fleet; each one must not cost a records LIST and a |
| +// re-arm of every host. |
| +func TestWarmWakeWithNothingNewRunsNoPass(t *testing.T) { |
| + w := newCertWarm( |
| + func() []string { return []string{"www.customer.test"} }, |
| + func(*tls.ClientHelloInfo) (*tls.Certificate, error) { return &tls.Certificate{}, nil }, |
| + func(string) bool { return true }, |
| + nil, |
| + nil, |
| + ) |
| + var passes atomic.Int32 |
| + w.passHook = func() { passes.Add(1) } |
| + startWarm(t, w) |
| + waitCount(t, &passes, 1, "passes") |
| + for i := 0; i < 5; i++ { |
| + w.nudge() |
| + time.Sleep(5 * time.Millisecond) |
| + } |
| + time.Sleep(20 * time.Millisecond) |
| + if got := passes.Load(); got != 1 { |
| + t.Fatalf("passes = %d, want 1 — a wake over an already-warmed set ran a pass", got) |
| + } |
| +} |
| + |
| +// A non-issuer orders nothing, so a wake there must not run a pass at all. |
| +func TestWarmWakeOnANonIssuerRunsNoPass(t *testing.T) { |
| + w := newCertWarm( |
| + func() []string { return []string{"www.customer.test"} }, |
| + func(*tls.ClientHelloInfo) (*tls.Certificate, error) { return &tls.Certificate{}, nil }, |
| + func(string) bool { return true }, |
| + func() (context.Context, bool) { return nil, false }, |
| + nil, |
| + ) |
| + var passes atomic.Int32 |
| + w.passHook = func() { passes.Add(1) } |
| + startWarm(t, w) |
| + waitCount(t, &passes, 1, "passes") |
| + w.nudge() |
| + time.Sleep(30 * time.Millisecond) |
| + if got := passes.Load(); got != 1 { |
| + t.Fatalf("passes = %d, want 1 — a non-issuer ran a pass on a wake", got) |
| + } |
| +} |
| + |
| +// A host already waiting on its DNS has its own retry schedule. A wake must |
| +// not run an extra pass for it, or every reload in the fleet would re-check |
| +// a domain whose owner has not finished their DNS. |
| +func TestWarmWakeIgnoresAHostAlreadyWaitingOnDNS(t *testing.T) { |
| + w := newCertWarm( |
| + func() []string { return []string{"www.customer.test"} }, |
| + func(*tls.ClientHelloInfo) (*tls.Certificate, error) { return &tls.Certificate{}, nil }, |
| + func(string) bool { return false }, |
| + nil, |
| + nil, |
| + ) |
| + var passes atomic.Int32 |
| + w.passHook = func() { passes.Add(1) } |
| + startWarm(t, w) |
| + waitCount(t, &passes, 1, "passes") |
| + w.nudge() |
| + time.Sleep(30 * time.Millisecond) |
| + if got := passes.Load(); got != 1 { |
| + t.Fatalf("passes = %d, want 1 — a wake re-checked a host already on its DNS schedule", got) |
| + } |
| +} |
| + |
| +// A host in failure backoff is not new either. |
| +func TestWarmWakeIgnoresAHostInBackoff(t *testing.T) { |
| + w := newCertWarm( |
| + func() []string { return []string{"bad.customer.test"} }, |
| + func(*tls.ClientHelloInfo) (*tls.Certificate, error) { return nil, context.DeadlineExceeded }, |
| + func(string) bool { return true }, |
| + nil, |
| + nil, |
| + ) |
| + var passes atomic.Int32 |
| + w.passHook = func() { passes.Add(1) } |
| + startWarm(t, w) |
| + waitCount(t, &passes, 1, "passes") |
| + w.nudge() |
| + time.Sleep(30 * time.Millisecond) |
| + if got := passes.Load(); got != 1 { |
| + t.Fatalf("passes = %d, want 1 — a wake retried a host inside its backoff", got) |
| + } |
| +} |
| + |
| +// nudge is called from the reload path and the home map's refresh; it must |
| +// never block them, with or without a loop running. |
| +func TestWarmNudgeNeverBlocks(t *testing.T) { |
| + w := newCertWarm(nil, nil, nil, nil, nil) |
| + done := make(chan struct{}) |
| + go func() { |
| + for i := 0; i < 100; i++ { |
| + w.nudge() |
| + } |
| + close(done) |
| + }() |
| + select { |
| + case <-done: |
| + case <-time.After(2 * time.Second): |
| + t.Fatal("nudge blocked") |
| + } |
| + var nilWarm *certWarm |
| + nilWarm.nudge() // no panic |
| +} |
| + |
| +// The reload hook was a single slot the wildcard manager held. Both it and |
| +// the warm need it now, so registering a second hook must not drop the first. |
| +func TestOnReloadKeepsEveryHook(t *testing.T) { |
| + e := &Edge{} |
| + var a, b atomic.Int32 |
| + e.OnReload(func() { a.Add(1) }) |
| + e.OnReload(func() { b.Add(1) }) |
| + e.runReloadHooks() |
| + if a.Load() != 1 || b.Load() != 1 { |
| + t.Fatalf("hooks ran a=%d b=%d, want 1 each", a.Load(), b.Load()) |
| + } |
| +} |
| + |
| +// The home map wakes the warm when the set of hosts homed ELSEWHERE changes. |
| +// That is how the issuer hears about a custom domain attached on another |
| +// box: setHostsWake only fires on platform-class names, which never include |
| +// a custom domain. |
| +func TestHomeMapServedWakeFiresOnACustomDomain(t *testing.T) { |
| + s := meshTestStore(t) |
| + putJSON(t, s, "control/edges/annie.json", `{"v":1,"box":"annie","eip":"1.1.1.1","hosts":{}}`) |
| + putJSON(t, s, "control/edges/betty.json", `{"v":1,"box":"betty","eip":"2.2.2.2","hosts":{"app.acct.apps.test":{"role":"home","platform":true}}}`) |
| + m := newHomeMap(s, "annie") |
| + var woke atomic.Int32 |
| + m.setServedWake(func() { woke.Add(1) }) |
| + refreshHomeMap(t, m) |
| + waitWoke(t, &woke, 1) |
| + refreshHomeMap(t, m) // unchanged: no wake |
| + time.Sleep(20 * time.Millisecond) |
| + if woke.Load() != 1 { |
| + t.Fatalf("an unchanged refresh woke the warm (%d)", woke.Load()) |
| + } |
| + // A custom domain attached on betty: unclassed, exact, dialable. |
| + putJSON(t, s, "control/edges/betty.json", `{"v":1,"box":"betty","eip":"2.2.2.2","hosts":{"app.acct.apps.test":{"role":"home","platform":true},"www.customer.test":{"role":"home","alias":true}}}`) |
| + refreshHomeMap(t, m) |
| + waitWoke(t, &woke, 2) |
| + // Betty's address moving changes no name: no wake. |
| + putJSON(t, s, "control/edges/betty.json", `{"v":1,"box":"betty","eip":"3.3.3.3","hosts":{"app.acct.apps.test":{"role":"home","platform":true},"www.customer.test":{"role":"home","alias":true}}}`) |
| + refreshHomeMap(t, m) |
| + time.Sleep(20 * time.Millisecond) |
| + if woke.Load() != 2 { |
| + t.Fatalf("a moved address woke the warm (%d)", woke.Load()) |
| + } |
| + // The platform-class wake and the served wake are separate hooks: setting |
| + // one must not replace the other. |
| + var platform atomic.Int32 |
| + m.setHostsWake(func() { platform.Add(1) }) |
| + putJSON(t, s, "control/edges/betty.json", `{"v":1,"box":"betty","eip":"3.3.3.3","hosts":{"app.acct.apps.test":{"role":"home","platform":true},"www.customer.test":{"role":"home","alias":true},"two.acct.apps.test":{"role":"home","platform":true}}}`) |
| + refreshHomeMap(t, m) |
| + waitWoke(t, &woke, 3) |
| + waitWoke(t, &platform, 1) |
| + |
| + var nilMap *homeMap |
| + nilMap.setServedWake(func() {}) // no panic |
| +} |
diff --git a/internal/edge/edge.go b/internal/edge/edge.go| index 4427c05cb..1e58cd558 100644 |
| --- a/internal/edge/edge.go |
| +++ b/internal/edge/edge.go |
| @@ -230,11 +230,12 @@ type Edge struct { |
| // created under logMu. |
| logMu sync.Mutex |
| logN map[string]int |
| - // reloadHook, when set (OnReload), runs after every successful route |
| - // swap — the wildcard manager recomputes its parent set from it. Atomic |
| + // reloadHooks (OnReload) run after every successful route swap: the |
| + // wildcard manager recomputes its parent set from them and the cert warm |
| + // looks for new hosts (#386). Copy-on-write behind an atomic pointer |
| // because registration happens in RunWithActivator after Watch may |
| // already be reloading. |
| - reloadHook atomic.Pointer[func()] |
| + reloadHooks atomic.Pointer[[]func()] |
| // probers owns the health-probe goroutines for multi-upstream addr routes |
| // (prober.go) and the live set multiProxy picks from. Created in New, |
| // BEFORE the startup Reload — Reload calls Sync on it unconditionally and |
| @@ -1022,16 +1023,36 @@ func (e *Edge) Reload() error { |
| if e.act != nil { |
| e.act.RetireExcept(managed) |
| } |
| - if fn := e.reloadHook.Load(); fn != nil { |
| - (*fn)() |
| - } |
| + e.runReloadHooks() |
| return nil |
| } |
| // OnReload registers fn to run after every successful Reload — after the new |
| // route set is swapped in, so fn sees the post-reload table via current(). |
| +// Additive: every registered hook runs, in registration order. It was a |
| +// single slot until the cert warm needed it beside the wildcard manager |
| +// (#386), and a second registration silently dropping the first is exactly |
| +// the failure that would leave one of them deaf. |
| func (e *Edge) OnReload(fn func()) { |
| - e.reloadHook.Store(&fn) |
| + for { |
| + old := e.reloadHooks.Load() |
| + var next []func() |
| + if old != nil { |
| + next = append(next, (*old)...) |
| + } |
| + next = append(next, fn) |
| + if e.reloadHooks.CompareAndSwap(old, &next) { |
| + return |
| + } |
| + } |
| +} |
| + |
| +func (e *Edge) runReloadHooks() { |
| + if hooks := e.reloadHooks.Load(); hooks != nil { |
| + for _, fn := range *hooks { |
| + fn() |
| + } |
| + } |
| } |
| // logThrottled logs msg the first time key occurs and every 300th time after |
| @@ -3354,6 +3375,12 @@ func RunWithActivatorReady(args []string, act Activator, cache *Cache, ready fun |
| e.issuerGate, |
| store, |
| ) |
| + // Woken when its set may have grown (#386), so a custom domain is |
| + // ordered seconds after it reaches this box rather than at the next |
| + // hourly pass: a local route reload, or a change in the names other |
| + // boxes home. A wake with nothing new in the set is dropped. |
| + e.OnReload(warm.nudge) |
| + e.homes.Load().setServedWake(warm.nudge) |
| go warm.run() |
| log.Printf("edge: cert warm on (%s pass) — custom domains are ordered and renewed proactively, not on first handshake", warmInterval) |
| } |
diff --git a/internal/edge/homemap.go b/internal/edge/homemap.go| index da513e36c..c1cf8d7ac 100644 |
| --- a/internal/edge/homemap.go |
| +++ b/internal/edge/homemap.go |
| @@ -250,6 +250,12 @@ type homeMap struct { |
| snap *homeSnapshot |
| // hostsWake fires after a Refresh that changed the NAME set (setHostsWake). |
| hostsWake func() |
| + // servedWake fires after a Refresh that changed the ServedHosts set |
| + // (setServedWake): the cert warm's cue that a custom domain was attached |
| + // on another box (#386). Separate from hostsWake because their filters |
| + // are opposites: hostsWake ignores exactly the unclassed rows a custom |
| + // domain arrives as. |
| + servedWake func() |
| // partialPolls counts polls that could not read (or parse) every object, |
| // for the first-and-every-tenth log cadence. |
| partialPolls int |
| @@ -492,6 +498,7 @@ func (m *homeMap) refreshHosts(ctx context.Context) error { |
| m.mu.Lock() |
| m.snap = snap |
| wake := m.hostsWake |
| + served := m.servedWake |
| m.mu.Unlock() |
| // A partial read reflects no generation: the file it could not read or |
| // parse is retried on the next poll, as it always was. |
| @@ -505,9 +512,39 @@ func (m *homeMap) refreshHosts(ctx context.Context) error { |
| // that. |
| go wake() |
| } |
| + if served != nil && !sameServedSet(prev.hosts, snap.hosts) { |
| + go served() |
| + } |
| return nil |
| } |
| +// sameServedSet reports whether two folds serve the same exact names, by |
| +// ServedHosts' filter. Addresses and owners moving change nothing here: the |
| +// warm's question is only which names exist. |
| +func sameServedSet(a, b map[string]homeEntry) bool { |
| + n := 0 |
| + for h, e := range a { |
| + if !servedInput(h, e) { |
| + continue |
| + } |
| + n++ |
| + if eb, ok := b[h]; !ok || !servedInput(h, eb) { |
| + return false |
| + } |
| + } |
| + for h, e := range b { |
| + if servedInput(h, e) { |
| + n-- |
| + } |
| + } |
| + return n == 0 |
| +} |
| + |
| +// servedInput is ServedHosts' filter: a dialable, exact name. |
| +func servedInput(host string, e homeEntry) bool { |
| + return e.dial && !strings.HasPrefix(host, "*.") |
| +} |
| + |
| // samePlatformSet reports whether two folds name the same PLATFORM hosts — |
| // the only names that can move a wildcard parent (homes may differ; an |
| // unclassed name may come and go). |
| @@ -625,7 +662,7 @@ func (m *homeMap) ServedHosts() []string { |
| snap := m.snapshot() |
| out := make([]string, 0, len(snap.hosts)) |
| for h, e := range snap.hosts { |
| - if !e.dial || strings.HasPrefix(h, "*.") { |
| + if !servedInput(h, e) { |
| continue |
| } |
| out = append(out, h) |
| @@ -646,6 +683,17 @@ func (m *homeMap) setHostsWake(fn func()) { |
| m.mu.Unlock() |
| } |
| +// setServedWake installs the callback a changed ServedHosts set fires |
| +// (Refresh), on the same terms as setHostsWake. |
| +func (m *homeMap) setServedWake(fn func()) { |
| + if m == nil { |
| + return |
| + } |
| + m.mu.Lock() |
| + m.servedWake = fn |
| + m.mu.Unlock() |
| +} |
| + |
| // parseEdgesFile decodes one box's file. A parse failure gets the same |
| // treatment an unreadable object gets — carry the last good parse forward — |
| // because a half-written body is no more usable than a body that never |