Skip to content

Instantly share code, notes, and snippets.

@sudorandom
Last active August 29, 2015 14:01
Show Gist options
  • Save sudorandom/b61c3c0aa6f771c0a7c9 to your computer and use it in GitHub Desktop.
Save sudorandom/b61c3c0aa6f771c0a7c9 to your computer and use it in GitHub Desktop.
Example Rate Limiter in Go
// Based on https://gobyexample.com/rate-limiting
package limiters
import (
"time"
)
func NewRateLimiter(rate time.Duration, burst uint) chan time.Time {
burstyLimiter := make(chan time.Time, burst)
for i := uint(0); i < burst; i++ {
burstyLimiter <- time.Now()
}
go func() {
for t := range time.Tick(rate) {
burstyLimiter <- t
}
}()
return burstyLimiter
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment