mirror of
https://github.com/soypat/lneto.git
synced 2026-08-11 02:13:44 +00:00
16c2c3de36
* huge Stack backoff rework * lneto bump: huge Stack backoff rework
40 lines
1.1 KiB
Go
40 lines
1.1 KiB
Go
package internal
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
// BackoffConnRW implements exponential backoff suitable for TCP connection
|
|
// read/write polling. It starts at 1us and caps at 5ms, doubling on each consecutive backoff.
|
|
func BackoffConnRW(consecutiveBackoffs uint) {
|
|
const (
|
|
minWait = uint32(time.Microsecond)
|
|
maxWait = 5 * uint32(time.Millisecond)
|
|
maxShift = 22
|
|
_overflowCheck = minWait << maxShift
|
|
)
|
|
wait := minWait << min(consecutiveBackoffs, maxShift)
|
|
if wait > maxWait {
|
|
wait = maxWait
|
|
}
|
|
time.Sleep(time.Duration(wait))
|
|
}
|
|
|
|
// BackoffStackProto implements exponential backoff suitable for stack-level
|
|
// protocol processing polling. It starts at 1us and caps at 100ms, doubling on each consecutive backoff.
|
|
func BackoffStackProto(consecutiveBackoffs uint) {
|
|
const (
|
|
minWait = uint32(time.Microsecond)
|
|
maxWait = 100 * uint32(time.Millisecond)
|
|
|
|
// Statically calculated numbers below.
|
|
maxShift = 22
|
|
_overflowCheck = minWait << maxShift
|
|
)
|
|
wait := minWait << min(consecutiveBackoffs, maxShift)
|
|
if wait > maxWait {
|
|
wait = maxWait
|
|
}
|
|
time.Sleep(time.Duration(wait))
|
|
}
|