Add sleep_threshold parameter to send() method

- Decrease the default sleep threshold from 60 to 10 seconds
- Use a higher sleep threshold for generator methods
This commit is contained in:
Dan 2020-08-26 09:01:01 +02:00
parent ebf222bbb7
commit 7c987889f0
10 changed files with 34 additions and 17 deletions

View File

@ -165,7 +165,7 @@ class Client(Methods, Scaffold):
Set a sleep threshold for flood wait exceptions happening globally in this client instance, below which any Set a sleep threshold for flood wait exceptions happening globally in this client instance, below which any
request that raises a flood wait will be automatically invoked again after sleeping for the required amount request that raises a flood wait will be automatically invoked again after sleeping for the required amount
of time. Flood wait exceptions requiring higher waiting times will be raised. of time. Flood wait exceptions requiring higher waiting times will be raised.
Defaults to 60 (seconds). Defaults to 10 seconds.
hide_password (``bool``, *optional*): hide_password (``bool``, *optional*):
Pass True to hide the password when typing it during the login. Pass True to hide the password when typing it during the login.

View File

@ -27,7 +27,13 @@ log = logging.getLogger(__name__)
class Send(Scaffold): class Send(Scaffold):
async def send(self, data: TLObject, retries: int = Session.MAX_RETRIES, timeout: float = Session.WAIT_TIMEOUT): async def send(
self,
data: TLObject,
retries: int = Session.MAX_RETRIES,
timeout: float = Session.WAIT_TIMEOUT,
sleep_threshold: float = None
):
"""Send raw Telegram queries. """Send raw Telegram queries.
This method makes it possible to manually call every single Telegram API method in a low-level manner. This method makes it possible to manually call every single Telegram API method in a low-level manner.
@ -50,6 +56,9 @@ class Send(Scaffold):
timeout (``float``): timeout (``float``):
Timeout in seconds. Timeout in seconds.
sleep_threshold (``float``):
Sleep threshold in seconds.
Returns: Returns:
``RawType``: The raw type response generated by the query. ``RawType``: The raw type response generated by the query.
@ -65,7 +74,7 @@ class Send(Scaffold):
if self.takeout_id: if self.takeout_id:
data = raw.functions.InvokeWithTakeout(takeout_id=self.takeout_id, query=data) data = raw.functions.InvokeWithTakeout(takeout_id=self.takeout_id, query=data)
r = await self.session.send(data, retries, timeout, self.sleep_threshold) r = await self.session.send(data, retries, timeout, sleep_threshold or self.sleep_threshold)
await self.fetch_peers(getattr(r, "users", [])) await self.fetch_peers(getattr(r, "users", []))
await self.fetch_peers(getattr(r, "chats", [])) await self.fetch_peers(getattr(r, "chats", []))

View File

@ -141,7 +141,8 @@ class GetChatMembers(Scaffold):
offset=offset, offset=offset,
limit=limit, limit=limit,
hash=0 hash=0
) ),
sleep_threshold=60
) )
members = r.participants members = r.participants

View File

@ -66,7 +66,10 @@ class GetDialogs(Scaffold):
""" """
if pinned_only: if pinned_only:
r = await self.send(raw.functions.messages.GetPinnedDialogs(folder_id=0)) r = await self.send(
raw.functions.messages.GetPinnedDialogs(folder_id=0),
sleep_threshold=60
)
else: else:
r = await self.send( r = await self.send(
raw.functions.messages.GetDialogs( raw.functions.messages.GetDialogs(
@ -76,7 +79,8 @@ class GetDialogs(Scaffold):
limit=limit, limit=limit,
hash=0, hash=0,
exclude_pinned=True exclude_pinned=True
) ),
sleep_threshold=60
) )
users = {i.id: i for i in r.users} users = {i.id: i for i in r.users}

View File

@ -20,7 +20,7 @@ import logging
from typing import Union, List from typing import Union, List
from pyrogram import raw from pyrogram import raw
# from pyrogram import types from pyrogram import types
from pyrogram import utils from pyrogram import utils
from pyrogram.scaffold import Scaffold from pyrogram.scaffold import Scaffold
@ -95,7 +95,8 @@ class GetHistory(Scaffold):
max_id=0, max_id=0,
min_id=0, min_id=0,
hash=0 hash=0
) ),
sleep_threshold=60
) )
) )

View File

@ -111,7 +111,7 @@ class GetMessages(Scaffold):
else: else:
rpc = raw.functions.messages.GetMessages(id=ids) rpc = raw.functions.messages.GetMessages(id=ids)
r = await self.send(rpc) r = await self.send(rpc, sleep_threshold=-1)
messages = await utils.parse_messages(self, r, replies=replies) messages = await utils.parse_messages(self, r, replies=replies)

View File

@ -74,7 +74,8 @@ class SearchGlobal(Scaffold):
offset_peer=offset_peer, offset_peer=offset_peer,
offset_id=offset_id, offset_id=offset_id,
limit=limit limit=limit
) ),
sleep_threshold=60
), ),
replies=0 replies=0
) )

View File

@ -80,7 +80,8 @@ async def get_chunk(
else None else None
), ),
hash=0 hash=0
) ),
sleep_threshold=60
) )
return await utils.parse_messages(client, r) return await utils.parse_messages(client, r)

View File

@ -176,7 +176,8 @@ class SendMediaGroup(Scaffold):
multi_media=multi_media, multi_media=multi_media,
silent=disable_notification or None, silent=disable_notification or None,
reply_to_msg_id=reply_to_message_id reply_to_msg_id=reply_to_message_id
) ),
sleep_threshold=60
) )
return await utils.parse_messages( return await utils.parse_messages(

View File

@ -18,12 +18,11 @@
import asyncio import asyncio
import logging import logging
import time import os
from concurrent.futures.thread import ThreadPoolExecutor from concurrent.futures.thread import ThreadPoolExecutor
from datetime import datetime, timedelta from datetime import datetime, timedelta
from hashlib import sha1 from hashlib import sha1
from io import BytesIO from io import BytesIO
import os
import pyrogram import pyrogram
from pyrogram import __copyright__, __license__, __version__ from pyrogram import __copyright__, __license__, __version__
@ -32,7 +31,7 @@ from pyrogram.connection import Connection
from pyrogram.crypto import mtproto from pyrogram.crypto import mtproto
from pyrogram.errors import RPCError, InternalServerError, AuthKeyDuplicated, FloodWait from pyrogram.errors import RPCError, InternalServerError, AuthKeyDuplicated, FloodWait
from pyrogram.raw.all import layer from pyrogram.raw.all import layer
from pyrogram.raw.core import TLObject, MsgContainer, Int, Long, FutureSalt, FutureSalts from pyrogram.raw.core import TLObject, MsgContainer, Int, FutureSalt, FutureSalts
from .internals import MsgId, MsgFactory from .internals import MsgId, MsgFactory
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -48,7 +47,7 @@ class Session:
INITIAL_SALT = 0x616e67656c696361 INITIAL_SALT = 0x616e67656c696361
START_TIMEOUT = 1 START_TIMEOUT = 1
WAIT_TIMEOUT = 15 WAIT_TIMEOUT = 15
SLEEP_THRESHOLD = 60 SLEEP_THRESHOLD = 10
MAX_RETRIES = 5 MAX_RETRIES = 5
ACKS_THRESHOLD = 8 ACKS_THRESHOLD = 8
PING_INTERVAL = 5 PING_INTERVAL = 5
@ -443,7 +442,7 @@ class Session:
except FloodWait as e: except FloodWait as e:
amount = e.x amount = e.x
if amount > sleep_threshold: if amount > sleep_threshold > 0:
raise raise
log.warning(f'[{self.client.session_name}] Sleeping for {amount}s (required by "{query}")') log.warning(f'[{self.client.session_name}] Sleeping for {amount}s (required by "{query}")')