Flood Wait Middleware
Invoker middleware that automatically handles Telegram FLOOD_WAIT errors by sleeping for the required duration and retrying the RPC call.
Install
bash
go get github.com/mtgo-labs/middlewares/floodwaitUsage
go
import (
"log"
"time"
tg "github.com/mtgo-labs/mtgo/telegram"
"github.com/mtgo-labs/middlewares/floodwait"
)
client, _ := tg.NewClient(apiID, apiHash, &tg.Config{BotToken: botToken})
waiter := floodwait.New()
waiter.OnWait(func(d time.Duration) {
log.Printf("flood wait: sleeping %v", d)
})
waiter.WithMaxWait(60 * time.Second)
waiter.WithMaxRetries(5)
client.UseInvokerMiddleware(waiter.Middleware())
// All RPC calls are now automatically retried on FLOOD_WAITConfiguration
| Method | Default | Description |
|---|---|---|
OnWait(fn) | none | Callback invoked when a flood wait occurs |
WithMaxWait(d) | unlimited | Maximum wait time before giving up |
WithMaxRetries(n) | 5 | Maximum retry attempts per RPC call |
How It Works
- RPC call returns
FLOOD_WAITerror - Middleware extracts the required wait duration from the error
- Sleeps for
duration + 1sbuffer (respecting context cancellation) - Retries the RPC call
- Repeats up to
MaxRetries
No handler changes required — register once and all outgoing API calls (ctx.Reply, ctx.SendMessage, client.Invoke, etc.) are automatically protected.
