'Rate limit function 40/second with "golang.org/x/time/rate"

I'm trying to use "golang.org/x/time/rate" 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

RateLimitFunc()

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.

go


Solution 1:[1]

Reading the documentation here; link

You can see that the first parameter to NewLimiter is of type rate.Limit.

If you want 40 requests / second then that translates into a rate of 1 request every 25 ms.

You can create that by doing:

limiter := rate.NewLimiter(rate.Every(25 * time.Millisecond), 2)

Side note:

  • In generate, a context, ctx, should not be stored on a struct and should be per request. It would appear that Client will be reused, thus you could pass a context to the RateLimitFunc() or wherever appropriate instead of storing a single context on the client struct.

Solution 2:[2]

func RateLimit(ctx context.Context) {
    limiter := rate.NewLimiter(40, 10)
    err := limiter.Wait(ctx)
    if err != nil {
        // Log the error and return
    }
    // Do the actual work here
}

As Zak said, do not store Context inside a struct type according to the Go documentation context.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Mortensen
Solution 2 Peter Mortensen