๐ข Throttlingยถ
Rate-limit handlers so users canโt flood the bot. Raito offers a global limit, per-handler limits, and three scoping modes.
Global throttlingยถ
add_throttling() installs one limit across every message and
callback query:
raito = Raito(dispatcher, "handlers")
raito.add_throttling(1.2, mode="user")
Now the same user can trigger any handler at most once every 1.2 seconds. The
default mode is "chat".
Per-handler limitsยถ
For a specific handler, apply @rt.limiter:
from aiogram import filters, types
from raito import rt
@router.message(filters.Command("export"))
@rt.limiter(rate_limit=5.0, mode="user")
async def export(message: types.Message) -> None:
...
Its default mode is "user". A handler carrying @rt.limiter uses only
its own limit โ it bypasses the global one, so you can make one expensive command
stricter (or looser) than the rest without touching the global rule.
Modesยถ
Mode |
Cooldown is shared byโฆ |
|---|---|
|
the same user (across chats). |
|
the same chat (all members share one cooldown). |
|
everyone โ a single global cooldown for the whole bot. |
Behaviour and limitsยถ
A throttled update is silently dropped โ the handler is not called and the user gets no error. Keep limits short enough not to feel broken.
Limits are enforced with a
cachetools.TTLCache;add_throttlingacceptsmax_size(default10_000) to cap how many keys are tracked.The per-handler limit is remembered by the handlerโs identity, so its rate is fixed at first use โ changing
rate_limitfor the same handler at runtime wonโt take effect until restart.