Skip to content

Instantly share code, notes, and snippets.

@fujimaki-k
Created May 3, 2023 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fujimaki-k/fa14eaffc0c9f57fbe8730e9eb4c4cfe to your computer and use it in GitHub Desktop.
Save fujimaki-k/fa14eaffc0c9f57fbe8730e9eb4c4cfe to your computer and use it in GitHub Desktop.
Throttling and exponential backoff example
package main
import (
"fmt"
"time"
)
// Throttling is provides throttling and exponential backoff calls.
func Throttling(weight int, rate int) {
if weight < 1 {
return
}
throttle := time.NewTicker(time.Second / time.Duration(rate))
defer func() {
throttle.Stop()
}()
for count := 0; count < (weight * weight); count++ {
<-throttle.C
}
}
func main() {
for i := 0; i < 5; i++ {
Throttling(i, 1)
fmt.Println(i)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment