在并行的goroutine中使用速率限制器是否会导致竞争条件?

huangapple go评论87阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2022年9月4日 02:37:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/73594626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定