From c9c63998d2bc1f0a9326ffe053c59cd58ae32353 Mon Sep 17 00:00:00 2001 From: chaosinthecrd Date: Thu, 25 Jun 2026 11:13:49 +0000 Subject: [PATCH] kube/certs: jitter initial CertPair fire All cert loops fired their first CertPair immediately. With many domains starting at once they stampeded tailscaled's shared cert mutex, which is what caused the customer's 12 contended domains to all time out together. Jitter the initial timer by 0-60s. --- kube/certs/certs.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/kube/certs/certs.go b/kube/certs/certs.go index 0432e89b4..4adfb80b8 100644 --- a/kube/certs/certs.go +++ b/kube/certs/certs.go @@ -9,6 +9,7 @@ import ( "context" "errors" "fmt" + mathrand "math/rand/v2" "net" "slices" "sync" @@ -147,7 +148,9 @@ func (cm *CertManager) runCertLoop(ctx context.Context, domain string) { cm.logf("error waiting for cert domain %s: %v", domain, err) } - timer := time.NewTimer(0) // fire off timer immediately + // Stagger initial fire so that when several domains come online at + // once we don't stampede tailscaled's shared cert mutex. + timer := time.NewTimer(mathrand.N(initialJitter)) defer timer.Stop() retryCount := 0 for { @@ -216,6 +219,11 @@ func (cm *CertManager) runCertLoop(ctx context.Context, domain string) { // waiting. var (not const) so tests can shorten it. var waitForCertDomainHeartbeat = 5 * time.Minute +// initialJitter is the max random delay before a cert loop's first CertPair +// call. Spreads startup load across the shared cert mutex in tailscaled. +// var (not const) so tests can shorten it. +var initialJitter = 60 * time.Second + // domains before issuing the cert for the first time. It uses the IPN bus // only as a wake-up trigger (Notify.SelfChange) and queries the current // cert domains explicitly via [LocalClient.CertDomains].