Allow per-request flood sleep threshold selection (#3123)

This commit is contained in:
penn5 2021-08-06 07:13:34 +01:00 committed by GitHub
parent e5599c178b
commit e546ae2f85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 3 deletions

View File

@ -837,6 +837,11 @@ class TelegramBaseClient(abc.ABC):
executed sequentially on the server. They run in arbitrary executed sequentially on the server. They run in arbitrary
order by default. 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: Returns:
The result of the request (often a `TLObject`) or a list of The result of the request (often a `TLObject`) or a list of
results if more than one request was given. results if more than one request was given.

View File

@ -26,10 +26,12 @@ def _fmt_flood(delay, request, *, early=False, td=datetime.timedelta):
class UserMethods: 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) 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,)) requests = (request if utils.is_list_like(request) else (request,))
for r in requests: for r in requests:
if not isinstance(r, TLRequest): if not isinstance(r, TLRequest):
@ -42,7 +44,7 @@ class UserMethods:
diff = round(due - time.time()) diff = round(due - time.time())
if diff <= 3: # Flood waits below 3 seconds are "ignored" if diff <= 3: # Flood waits below 3 seconds are "ignored"
self._flood_waited_requests.pop(r.CONSTRUCTOR_ID, None) 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)) self._log[__name__].info(*_fmt_flood(diff, r, early=True))
await asyncio.sleep(diff) await asyncio.sleep(diff)
self._flood_waited_requests.pop(r.CONSTRUCTOR_ID, None) self._flood_waited_requests.pop(r.CONSTRUCTOR_ID, None)