Created
May 3, 2023 02:38
-
-
Save fujimaki-k/fa14eaffc0c9f57fbe8730e9eb4c4cfe to your computer and use it in GitHub Desktop.
Throttling and exponential backoff example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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