Improve flood wait log message

This commit is contained in:
Lonami Exo 2019-07-17 12:04:53 +02:00
parent 2ffd1e8e7c
commit cca50ef842

View File

@ -1,4 +1,5 @@
import asyncio import asyncio
import datetime
import itertools import itertools
import time import time
import typing import typing
@ -14,6 +15,16 @@ if typing.TYPE_CHECKING:
from .telegramclient import TelegramClient from .telegramclient import TelegramClient
def _fmt_flood(delay, request, *, early=False, td=datetime.timedelta):
return (
'Sleeping%s for %ds (%s) on %s flood wait',
' early' if early else '',
delay,
td(seconds=delay),
request.__class__.__name__
)
class UserMethods: class UserMethods:
async def __call__(self: 'TelegramClient', request, ordered=False): async def __call__(self: 'TelegramClient', request, ordered=False):
requests = (request if utils.is_list_like(request) else (request,)) requests = (request if utils.is_list_like(request) else (request,))
@ -29,8 +40,7 @@ class UserMethods:
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 <= self.flood_sleep_threshold:
self._log[__name__].info( self._log[__name__].info(*_fmt_flood(diff, r, early=True))
'Sleeping early for %ds on flood wait', diff)
await asyncio.sleep(diff, loop=self._loop) await asyncio.sleep(diff, loop=self._loop)
self._flood_waited_requests.pop(r.CONSTRUCTOR_ID, None) self._flood_waited_requests.pop(r.CONSTRUCTOR_ID, None)
else: else:
@ -80,8 +90,7 @@ class UserMethods:
[request.CONSTRUCTOR_ID] = time.time() + e.seconds [request.CONSTRUCTOR_ID] = time.time() + e.seconds
if e.seconds <= self.flood_sleep_threshold: if e.seconds <= self.flood_sleep_threshold:
self._log[__name__].info('Sleeping for %ds on flood wait', self._log[__name__].info(*_fmt_flood(e.seconds, request))
e.seconds)
await asyncio.sleep(e.seconds, loop=self._loop) await asyncio.sleep(e.seconds, loop=self._loop)
else: else:
raise raise