LoginSignup
0
0

More than 3 years have passed since last update.

現場で使えるQPS制御(Rate Limit) 1秒間にn回の実行に制限する

Last updated at Posted at 2020-11-13

1秒間に10回実行したい場合の例

https://godoc.org/golang.org/x/time/rate を使う

func main() {
    limiter := rate.NewLimiter(rate.Limit(10), 1)
    ctx := context.Background()

    for i := 0; i < 1000; i++ {
        if err := limiter.Wait(ctx); err != nil {
            fmt.Println(err)
        }
        fmt.Println(i)
    }
}

参考

https://qiita.com/lufia/items/29bf1aeb0a0fe69d16f0
https://blog.lufia.org/entry/2016/08/28/000000

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0