英文:
Does using a rate limiter within parallel goroutines cause race conditions?
问题
作为对代码的大幅简化,我有一个goroutine以并行方式运行数千次,每个goroutine都通过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...
}
这个想法是让run()
尽可能频繁地执行,但限制为每秒恰好100次调用,以符合第三方API的速率限制为100 RPS。
当limiter.Wait()
允许执行继续时,这会导致竞态条件吗?因为我仍然受到第三方的速率限制。
英文:
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.
答案1
得分: 2
不,它不是。
limiter.Wait()
是并发安全的,你可以在 Limiter 的实现源文件中看到这一点。
你可以在任何并发场景中自由使用 Limiter。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论