Skip to content

Commit

Permalink
Allow per-request flood sleep threshold selection (#3123)
Browse files Browse the repository at this point in the history
  • Loading branch information
penn5 committed Aug 6, 2021
1 parent e5599c1 commit e546ae2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 5 additions & 0 deletions telethon/client/telegrambaseclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,11 @@ def __call__(self: 'TelegramClient', request, ordered=False):
executed sequentially on the server. They run in arbitrary
order by default.
flood_sleep_threshold (`int` | `None`, optional):
The flood sleep threshold to use for this request. This overrides
the default value stored in
`client.flood_sleep_threshold <telethon.client.telegrambaseclient.TelegramBaseClient.flood_sleep_threshold>`
Returns:
The result of the request (often a `TLObject`) or a list of
results if more than one request was given.
Expand Down
8 changes: 5 additions & 3 deletions telethon/client/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ def _fmt_flood(delay, request, *, early=False, td=datetime.timedelta):


class UserMethods:
async def __call__(self: 'TelegramClient', request, ordered=False):
async def __call__(self: 'TelegramClient', request, ordered=False, flood_sleep_threshold=None):
return await self._call(self._sender, request, ordered=ordered)

async def _call(self: 'TelegramClient', sender, request, ordered=False):
async def _call(self: 'TelegramClient', sender, request, ordered=False, flood_sleep_threshold=None):
if flood_sleep_threshold is None:
flood_sleep_threshold = self.flood_sleep_threshold
requests = (request if utils.is_list_like(request) else (request,))
for r in requests:
if not isinstance(r, TLRequest):
Expand All @@ -42,7 +44,7 @@ async def _call(self: 'TelegramClient', sender, request, ordered=False):
diff = round(due - time.time())
if diff <= 3: # Flood waits below 3 seconds are "ignored"
self._flood_waited_requests.pop(r.CONSTRUCTOR_ID, None)
elif diff <= self.flood_sleep_threshold:
elif diff <= flood_sleep_threshold:
self._log[__name__].info(*_fmt_flood(diff, r, early=True))
await asyncio.sleep(diff)
self._flood_waited_requests.pop(r.CONSTRUCTOR_ID, None)
Expand Down

0 comments on commit e546ae2

Please sign in to comment.