https://github.com/rfyiamcool/backoff
🦀 go backoff lib, avoid 'thundering herd' effect
https://github.com/rfyiamcool/backoff
backoff
Last synced: 3 months ago
JSON representation
🦀 go backoff lib, avoid 'thundering herd' effect
- Host: GitHub
- URL: https://github.com/rfyiamcool/backoff
- Owner: rfyiamcool
- Created: 2019-04-12T00:33:25.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-08-30T09:15:32.000Z (almost 5 years ago)
- Last Synced: 2025-10-14T17:40:34.032Z (8 months ago)
- Topics: backoff
- Language: Go
- Homepage: http://xiaorui.cc
- Size: 2.93 KB
- Stars: 37
- Watchers: 2
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# backoff
backoff policy, avoid `thundering herd` effect. the backoff lib is concurrent-safe.
## Install:
```
go get github.com/rfyiamcool/backoff
```
## Desc
**How to calculate the delay time**
```
float64(b.MinDelay) * math.Pow(b.Factor, b.Attempts)
```
## Usage:
**Simple example 1**
factor = 2
```
b := NewBackOff(
WithMinDelay(100*time.Millisecond),
WithMaxDelay(10*time.Second),
WithFactor(2),
)
equals(t, b.Duration(), 100*time.Millisecond)
equals(t, b.Duration(), 200*time.Millisecond)
equals(t, b.Duration(), 400*time.Millisecond)
for index := 0; index < 100; index++ {
b.Duration()
}
// is max
equals(t, b.Duration(), 10*time.Second)
b.Reset()
equals(t, b.Duration(), 100*time.Millisecond)
```
factor = 1.7
```
b := NewBackOff(
WithMinDelay(100*time.Millisecond),
WithMaxDelay(10*time.Second),
WithFactor(1.7),
)
equals(t, b.Duration(), 100*time.Nanosecond)
equals(t, b.Duration(), 175*time.Nanosecond)
equals(t, b.Duration(), 306*time.Nanosecond)
b.Reset()
equals(t, b.Duration(), 100*time.Nanosecond)
```
**Jitter example**
enabling Jitter adds some randomization to the backoff durations.
```
b := NewBackOff(
WithMinDelay(100*time.Millisecond),
WithMaxDelay(10*time.Second),
WithFactor(2),
WithJitterFlag(true),
)
equals(t, b.Duration(), 100*time.Millisecond)
between(t, b.Duration(), 100*time.Millisecond, 200*time.Millisecond)
between(t, b.Duration(), 100*time.Millisecond, 400*time.Millisecond)
b.Reset()
equals(t, b.Duration(), 100*time.Millisecond)
```