go-telegram / bot

Telegram Bot API Go framework
MIT License
752 stars 68 forks source link

Any example for rate limiting #124

Open imokoi opened 1 month ago

imokoi commented 1 month ago

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

mkorobovv commented 4 weeks ago

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)
        }
    })
}
negasus commented 4 weeks ago

I have plans to add rate limit support to the library