devbox/internal/daemon/backoff.go
shoemoney 5bfccdb297 fix(M7): review fixes — pidfile O_EXCL, publish race, backoff reset 🐛
Code-review pass on M6.5+M7:
- pidfile: claim via O_CREATE|O_EXCL and arbitrate live-vs-stale, so two
  `devbox start` invocations can't both win the guard and run concurrently
- hub: serialize publish (publishMu) so a case-clash check + create is atomic;
  reject share names ending in '.' (Windows strips it → silent collision)
- daemon: reset SSE backoff only after a genuinely healthy stream (2m), not the
  30s ceiling, so a hub that recycles connections doesn't pin a 30s event lag
- document that restore/deploy's delete pass is ignore/guard-filtered on purpose
  (never delete a user's gitignored runtime files or local secrets)
- test: pidfile claim/refuse/stale-reclaim arbitration
2026-06-23 01:37:58 -05:00

38 lines
1.1 KiB
Go

package daemon
import (
"math/rand"
"time"
)
// Reconnect backoff bounds: start at 1s, double to a 30s ceiling. A stream that
// stayed up at least healthyStream is treated as a transient drop (reset to base),
// not a hard failure — so a hub that recycles SSE connections every ~30s doesn't
// leave the daemon stuck at the ceiling with a 30s event lag.
const (
backoffBase = 1 * time.Second
backoffMax = 30 * time.Second
healthyStream = 2 * time.Minute
)
// nextBackoff returns the next clean (un-jittered) backoff: base, then doubling
// up to backoffMax. Pass the previous clean value; pass <=0 to (re)start at base.
func nextBackoff(prev time.Duration) time.Duration {
switch {
case prev <= 0:
return backoffBase
case prev*2 >= backoffMax:
return backoffMax
default:
return prev * 2
}
}
// jittered spreads a backoff by ±25% so a fleet that all lost the hub at once
// doesn't reconnect in lockstep (thundering herd). Returns 0 unchanged.
func jittered(d time.Duration) time.Duration {
if d <= 0 {
return 0
}
return d + time.Duration(rand.Int63n(int64(d/2))) - d/4
}