Skip to content

Commit e425990

Browse files
committed
Use a lower timeout when starting a session to speed up re-connections
Sometimes the server drops right after a successful connection and pyrogram keeps waiting up 15 seconds (current WAIT_TIMEOUT) for the first query to time out and start again a new connection.
1 parent f84f9ec commit e425990

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

pyrogram/session/session.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def __init__(self):
4848
class Session:
4949
INITIAL_SALT = 0x616e67656c696361
5050
NET_WORKERS = 1
51+
START_TIMEOUT = 1
5152
WAIT_TIMEOUT = 15
5253
MAX_RETRIES = 5
5354
ACKS_THRESHOLD = 8
@@ -130,8 +131,14 @@ def start(self):
130131
Thread(target=self.recv, name="RecvThread").start()
131132

132133
self.current_salt = FutureSalt(0, 0, self.INITIAL_SALT)
133-
self.current_salt = FutureSalt(0, 0, self._send(functions.Ping(0)).new_server_salt)
134-
self.current_salt = self._send(functions.GetFutureSalts(1)).salts[0]
134+
self.current_salt = FutureSalt(
135+
0, 0,
136+
self._send(
137+
functions.Ping(0),
138+
timeout=self.START_TIMEOUT
139+
).new_server_salt
140+
)
141+
self.current_salt = self._send(functions.GetFutureSalts(1), timeout=self.START_TIMEOUT).salts[0]
135142

136143
self.next_salt_thread = Thread(target=self.next_salt, name="NextSaltThread")
137144
self.next_salt_thread.start()
@@ -150,7 +157,8 @@ def start(self):
150157
lang_pack="",
151158
query=functions.help.GetConfig(),
152159
)
153-
)
160+
),
161+
timeout=self.START_TIMEOUT
154162
)
155163

156164
self.ping_thread = Thread(target=self.ping, name="PingThread")

0 commit comments

Comments
 (0)