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.
This commit is contained in:
chaosinthecrd committed 2026-06-25 14:08:30 +00:00
1 parent c03c07c197
commit c9c63998d2
1 file changed
+9 -1
+9 -1
View File
@@ -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].