Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bygui86/go-rate-limit
Sample project to investigate about rate limiting in Golang
https://github.com/bygui86/go-rate-limit
Last synced: 2 days ago
JSON representation
Sample project to investigate about rate limiting in Golang
- Host: GitHub
- URL: https://github.com/bygui86/go-rate-limit
- Owner: bygui86
- License: apache-2.0
- Created: 2020-07-31T11:14:52.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-07-31T11:47:32.000Z (over 4 years ago)
- Last Synced: 2023-03-07T10:57:53.949Z (almost 2 years ago)
- Language: Go
- Size: 14.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
### Go Rate Limiter
Rate limiting examples in Golang.
`$ go get github.com/jpg013/ratelimiter`
Throttle Rate Limiter
```golang
func main() {
r, err := ratelimiter.NewThrottleRateLimiter(&ratelimiter.Config{
Throttle: 1 * time.Second,
})if err != nil {
panic(err)
}doWork(r, 10)
}func doWork(r RateLimiter, workerCount int) {
var wg sync.WaitGroup
rand.Seed(time.Now().UnixNano())doWork := func(id int) {
// Acquire a rate limit token
token, err := r.Acquire()
fmt.Printf("Rate Limit Token acquired %s...\n", token.ID)
if err != nil {
panic(err)
}
// Simulate some work
n := rand.Intn(5)
fmt.Printf("Worker %d Sleeping %d seconds...\n", id, n)
time.Sleep(time.Duration(n) * time.Second)
fmt.Printf("Worker %d Done\n", id)
r.Release(token)
wg.Done()
}// Spin up a 10 workers that need a rate limit resource
for i := 0; i < workerCount; i++ {
wg.Add(1)
go doWork(i)
}wg.Wait()
}
```Max Concurrency Rate Limiter
```golang
func main() {
r, err := ratelimiter.NewMaxConcurrencyRateLimiter(&ratelimiter.Config{
Limit: 4,
TokenResetsAfter: 10 * time.Second,
})if err != nil {
panic(err)
}doWork(r, 10)
}
```Fixed Window Rate Limiter
```golang
func main() {
r, err := ratelimiter.NewFixedWindowRateLimiter(&ratelimiter.Config{
Limit: 5,
FixedInterval: 15 * time.Second,
})if err != nil {
panic(err)
}doWork(r, 10)
}
```