bot icon indicating copy to clipboard operation
bot copied to clipboard

Any example for rate limiting

Open imokoi opened this issue 1 year ago • 5 comments

Hello, is there any example for rate limiting? avoid error code 429.

imokoi avatar Oct 17 '24 23:10 imokoi

you can wrap your handlers in middleware, where you set rate limiter from lib golang.org/x/time/rate

example:

func rateLimiter(next func(ctx context.Context, bot *Bot, update *models.Update)) HandlerFunc {
    limiter := rate.NewLimiter(2, 4)
    return HandlerFunc(func(ctx context.Context, bot *Bot, update *models.Update) {
        if !limiter.Allow() {
            // process if now allow
        } else {
            next(w, r)
        }
    })
}

mkorobovv avatar Oct 21 '24 09:10 mkorobovv

I have plans to add rate limit support to the library

negasus avatar Oct 21 '24 09:10 negasus

I have plans to add rate limit support to the library

any updates?

plandem avatar Jan 22 '25 01:01 plandem

This is not an easy task for a universal solution.

What behavior do you expect if the call call has returned 429? Automatic attempt to re -send? But if we do not return the error, your application will not find out about the error and will constantly try to repeat the request. And he will not know about what has reached the limits.

negasus avatar Jan 24 '25 13:01 negasus

idk, hoped for something like https://github.com/grammyjs/grammY has.

plandem avatar Jan 28 '25 16:01 plandem