2019-02-27 07:25
浏览 220

限速功能40 /秒,带有“”

I'm trying to use "" to build a function which blocks until a token is free. Is this the correct way to use the library to rate limit blocks of code to 40 requests per second, with a bucket size of 2.

type Client struct {
    limiter        *rate.Limiter
    ctx context.Context

func NewClient() *Client {
    c :=Client{}
    c.limiter = rate.NewLimiter(40, 2)
    c.ctx = context.Background()
    return &c

func (client *Client) RateLimitFunc() {

    err := client.limiter.Wait(client.ctx)
    if err != nil {
        fmt.Printf("rate limit error: %v", err)

To rate limit a block of code I call


I don't want to use a ticker as I want the rate limiter to take into account the length of time the calling code runs for.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题