As a heavy simplification of my code, I have a goroutine that gets ran thousands of times in parallel, with each one waiting to run via a rate limiter from golang.org/x/time/rate
:
func main() {
limiter := rate.NewLimiter(rate.Every(time.Second/100), 1) // 100 RPS
for _ := range jobs {
go run(&limiter)
}
}
func run(limiter *rate.Limiter) {
limiter.Wait()
// do stuff...
}
The idea is to have run()
execute as often as possible, but limited to exactly 100 calls / sec in order to comply with a third-party API's rate limit of 100 RPS.
Would this cause a race condition when limiter.Wait()
allows the execution to proceed? Because I'm still being rate limited by the third party.
No, it doesn't.
limiter.Wait()
is concurrent safe, you can see this in source files of Limiter's implementation.
You are free to use Limiter in any concurrent scenario.