Golang sleep functions
1 min readJun 29, 2020
Inspired by the article I decided to write a library ravblk/sleep for golang. In which these delays are implemented.
Simple sleep.
All delays are the same.
sleep.MS(2)
Example: 2ms->2ms->2ms
Sleep with jitter
All delays are the same but with a little (0–1ms) jitter
sleep.MSJitter(5)
Example: 5.3ms->5.5ms->5.2ms
Random sleep 0-max ms
max := 10
sleep.MSRandom(max)
Example: 3ms->9ms->7ms
Exponention sleep
Use 2^n ms delay where n = retry count
power := 0
for i := 0; i < 3; i++ {
power = MSExponent(power)
}
Or iteration variable
for i := 0; i < 3; i++ {
MSExponent(i)
}
Example: 2->4->8 ms
Exponention sleep with jitter
Use 2^n ms delay where n = retry count with a little (0–1ms) jitter
power := 0
for i := 0; i < 3; i++ {
power = MSExponentJitter(power)
}
Or iteration variable
for i := 0; i < 3; i++ {
MSExponentJitter(i)
}
Example: 2.6->4.1->8.3 ms