Skip to content

Commit 7c98788

Browse files
committed
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
1 parent ebf222b commit 7c98788

File tree

10 files changed

+34
-17
lines changed

10 files changed

+34
-17
lines changed

pyrogram/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class Client(Methods, Scaffold):
165165
Set a sleep threshold for flood wait exceptions happening globally in this client instance, below which any
166166
request that raises a flood wait will be automatically invoked again after sleeping for the required amount
167167
of time. Flood wait exceptions requiring higher waiting times will be raised.
168-
Defaults to 60 (seconds).
168+
Defaults to 10 seconds.
169169
170170
hide_password (``bool``, *optional*):
171171
Pass True to hide the password when typing it during the login.

pyrogram/methods/advanced/send.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@
2727

2828

2929
class Send(Scaffold):
30-
async def send(self, data: TLObject, retries: int = Session.MAX_RETRIES, timeout: float = Session.WAIT_TIMEOUT):
30+
async def send(
31+
self,
32+
data: TLObject,
33+
retries: int = Session.MAX_RETRIES,
34+
timeout: float = Session.WAIT_TIMEOUT,
35+
sleep_threshold: float = None
36+
):
3137
"""Send raw Telegram queries.
3238
3339
This method makes it possible to manually call every single Telegram API method in a low-level manner.
@@ -50,6 +56,9 @@ async def send(self, data: TLObject, retries: int = Session.MAX_RETRIES, timeout
5056
timeout (``float``):
5157
Timeout in seconds.
5258
59+
sleep_threshold (``float``):
60+
Sleep threshold in seconds.
61+
5362
Returns:
5463
``RawType``: The raw type response generated by the query.
5564
@@ -65,7 +74,7 @@ async def send(self, data: TLObject, retries: int = Session.MAX_RETRIES, timeout
6574
if self.takeout_id:
6675
data = raw.functions.InvokeWithTakeout(takeout_id=self.takeout_id, query=data)
6776

68-
r = await self.session.send(data, retries, timeout, self.sleep_threshold)
77+
r = await self.session.send(data, retries, timeout, sleep_threshold or self.sleep_threshold)
6978

7079
await self.fetch_peers(getattr(r, "users", []))
7180
await self.fetch_peers(getattr(r, "chats", []))

pyrogram/methods/chats/get_chat_members.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ async def get_chat_members(
141141
offset=offset,
142142
limit=limit,
143143
hash=0
144-
)
144+
),
145+
sleep_threshold=60
145146
)
146147

147148
members = r.participants

pyrogram/methods/chats/get_dialogs.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ async def get_dialogs(
6666
"""
6767

6868
if pinned_only:
69-
r = await self.send(raw.functions.messages.GetPinnedDialogs(folder_id=0))
69+
r = await self.send(
70+
raw.functions.messages.GetPinnedDialogs(folder_id=0),
71+
sleep_threshold=60
72+
)
7073
else:
7174
r = await self.send(
7275
raw.functions.messages.GetDialogs(
@@ -76,7 +79,8 @@ async def get_dialogs(
7679
limit=limit,
7780
hash=0,
7881
exclude_pinned=True
79-
)
82+
),
83+
sleep_threshold=60
8084
)
8185

8286
users = {i.id: i for i in r.users}

pyrogram/methods/messages/get_history.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from typing import Union, List
2121

2222
from pyrogram import raw
23-
# from pyrogram import types
23+
from pyrogram import types
2424
from pyrogram import utils
2525
from pyrogram.scaffold import Scaffold
2626

@@ -95,7 +95,8 @@ async def get_history(
9595
max_id=0,
9696
min_id=0,
9797
hash=0
98-
)
98+
),
99+
sleep_threshold=60
99100
)
100101
)
101102

pyrogram/methods/messages/get_messages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ async def get_messages(
111111
else:
112112
rpc = raw.functions.messages.GetMessages(id=ids)
113113

114-
r = await self.send(rpc)
114+
r = await self.send(rpc, sleep_threshold=-1)
115115

116116
messages = await utils.parse_messages(self, r, replies=replies)
117117

pyrogram/methods/messages/search_global.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ async def search_global(
7474
offset_peer=offset_peer,
7575
offset_id=offset_id,
7676
limit=limit
77-
)
77+
),
78+
sleep_threshold=60
7879
),
7980
replies=0
8081
)

pyrogram/methods/messages/search_messages.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ async def get_chunk(
8080
else None
8181
),
8282
hash=0
83-
)
83+
),
84+
sleep_threshold=60
8485
)
8586

8687
return await utils.parse_messages(client, r)

pyrogram/methods/messages/send_media_group.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ async def send_media_group(
176176
multi_media=multi_media,
177177
silent=disable_notification or None,
178178
reply_to_msg_id=reply_to_message_id
179-
)
179+
),
180+
sleep_threshold=60
180181
)
181182

182183
return await utils.parse_messages(

pyrogram/session/session.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818

1919
import asyncio
2020
import logging
21-
import time
21+
import os
2222
from concurrent.futures.thread import ThreadPoolExecutor
2323
from datetime import datetime, timedelta
2424
from hashlib import sha1
2525
from io import BytesIO
26-
import os
2726

2827
import pyrogram
2928
from pyrogram import __copyright__, __license__, __version__
@@ -32,7 +31,7 @@
3231
from pyrogram.crypto import mtproto
3332
from pyrogram.errors import RPCError, InternalServerError, AuthKeyDuplicated, FloodWait
3433
from pyrogram.raw.all import layer
35-
from pyrogram.raw.core import TLObject, MsgContainer, Int, Long, FutureSalt, FutureSalts
34+
from pyrogram.raw.core import TLObject, MsgContainer, Int, FutureSalt, FutureSalts
3635
from .internals import MsgId, MsgFactory
3736

3837
log = logging.getLogger(__name__)
@@ -48,7 +47,7 @@ class Session:
4847
INITIAL_SALT = 0x616e67656c696361
4948
START_TIMEOUT = 1
5049
WAIT_TIMEOUT = 15
51-
SLEEP_THRESHOLD = 60
50+
SLEEP_THRESHOLD = 10
5251
MAX_RETRIES = 5
5352
ACKS_THRESHOLD = 8
5453
PING_INTERVAL = 5
@@ -443,7 +442,7 @@ async def send(
443442
except FloodWait as e:
444443
amount = e.x
445444

446-
if amount > sleep_threshold:
445+
if amount > sleep_threshold > 0:
447446
raise
448447

449448
log.warning(f'[{self.client.session_name}] Sleeping for {amount}s (required by "{query}")')

0 commit comments

Comments
 (0)