Last active
August 29, 2015 14:01
-
-
Save sudorandom/b61c3c0aa6f771c0a7c9 to your computer and use it in GitHub Desktop.
Example Rate Limiter in Go
This file contains 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
// 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