Files
agent-radar 2ed3415fb7
Publish Claude Code Binaries / Stage and publish binaries (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-arm64 (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build linux-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build windows-amd64 (push) Canceled after 0s
Sync Tutti Agent Skills / sync (push) Canceled after 0s
Windows Agent Adapters / Test Windows agent adapters (push) Canceled after 0s
Windows Daemon Adapters / Test Windows daemon adapters (push) Canceled after 0s
Publish Tutti App Runtime / Publish runtime catalog (push) Canceled after 0s
源码快照 @ 2026-09-01 (source-only, 剔除非源码素材)
2026-09-01 20:17:27 +08:00

65 lines
1.5 KiB
Go

package relaytransport
import (
"math"
"math/rand"
"time"
)
type exponentialBackoff struct {
cfg BackoffConfig
current time.Duration
random *rand.Rand
}
func newExponentialBackoff(cfg BackoffConfig) *exponentialBackoff {
if cfg.Initial <= 0 {
cfg.Initial = 100 * time.Millisecond
}
if cfg.Max <= 0 {
cfg.Max = 5 * time.Second
}
if cfg.Multiplier <= 1 || math.IsNaN(cfg.Multiplier) || math.IsInf(cfg.Multiplier, 0) {
cfg.Multiplier = 2
}
if cfg.Initial > cfg.Max {
cfg.Initial = cfg.Max
}
var random *rand.Rand
if cfg.RandFactory != nil {
random = cfg.RandFactory()
}
if random == nil {
// The package-level generator is concurrency-safe and supplies distinct
// seeds to the generator owned by each business lifecycle.
random = rand.New(rand.NewSource(rand.Int63()))
}
return &exponentialBackoff{cfg: cfg, random: random}
}
func (b *exponentialBackoff) Reset() { b.current = 0 }
func (b *exponentialBackoff) Cap() time.Duration { return b.current }
func (b *exponentialBackoff) Next() time.Duration {
if b.current == 0 {
b.current = b.cfg.Initial
} else if float64(b.current) >= float64(b.cfg.Max)/b.cfg.Multiplier {
b.current = b.cfg.Max
} else {
next := time.Duration(float64(b.current) * b.cfg.Multiplier)
if next <= b.current || next > b.cfg.Max {
b.current = b.cfg.Max
} else {
b.current = next
}
}
if b.current <= 0 {
return 0
}
if b.current == time.Duration(math.MaxInt64) {
return time.Duration(b.random.Int63())
}
return time.Duration(b.random.Int63n(int64(b.current) + 1))
}