Simplified implementation (#27)

Introduced minimum backoff time
Max TCP backoff increased to 5ms
This commit is contained in:
ddirect
2026-01-26 23:49:33 +02:00
committed by GitHub
parent 5e61b4600d
commit 360628f1b8
+22 -24
View File
@@ -10,18 +10,24 @@ const (
BackoffTCPConn BackoffTCPConn
) )
func NewBackoff(priority BackoffFlags) Backoff { const backoffMinWait = time.Microsecond
if priority&BackoffCriticalPath != 0 {
return Backoff{ func backoffMaxWait(priority BackoffFlags) time.Duration {
maxWait: uint32(1 * time.Millisecond), switch {
} case priority&BackoffCriticalPath != 0:
} else if priority&BackoffTCPConn != 0 { return 1 * time.Millisecond
return Backoff{ case priority&BackoffTCPConn != 0:
maxWait: uint32(5 * time.Microsecond), return 5 * time.Millisecond
} default:
return time.Second >> (priority & BackoffHasPriority)
} }
}
func NewBackoff(priority BackoffFlags) Backoff {
return Backoff{ return Backoff{
maxWait: uint32(time.Second) >> (priority & BackoffHasPriority), wait: uint32(backoffMinWait),
maxWait: uint32(backoffMaxWait(priority)),
startWait: uint32(backoffMinWait),
} }
} }
@@ -31,10 +37,8 @@ type Backoff struct {
wait uint32 wait uint32
// Maximum allowable value for Wait. // Maximum allowable value for Wait.
maxWait uint32 maxWait uint32
// startWait is the value that Wait takes after a call to Hit. // startWait is the intial Wait value, as well as the value that Wait takes after a call to Hit.
startWait uint32 startWait uint32
// expMinusOne is the shift performed on Wait minus one, so the zero value performs a shift of 1.
expMinusOne uint32
} }
// Hit sets eb.Wait to the StartWait value. // Hit sets eb.Wait to the StartWait value.
@@ -47,18 +51,12 @@ func (eb *Backoff) Hit() {
// Miss sleeps for eb.Wait and increases eb.Wait exponentially. // Miss sleeps for eb.Wait and increases eb.Wait exponentially.
func (eb *Backoff) Miss() { func (eb *Backoff) Miss() {
const k = 1 if eb.maxWait == 0 {
wait := eb.wait
maxWait := eb.maxWait
exp := eb.expMinusOne + 1
if maxWait == 0 {
panic("MaxWait cannot be zero") panic("MaxWait cannot be zero")
} }
time.Sleep(time.Duration(wait)) time.Sleep(time.Duration(eb.wait))
wait |= k eb.wait *= 2
wait <<= exp if eb.wait > eb.maxWait {
if wait > maxWait { eb.wait = eb.maxWait
wait = maxWait
} }
eb.wait = wait
} }