Skip to content

Commit 8339f49

Browse files
committed
Don't retry forever
1 parent 68424d3 commit 8339f49

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

pyrogram/session/auth.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333

3434
class Auth:
35+
MAX_RETRIES = 5
36+
3537
CURRENT_DH_PRIME = int(
3638
"C71CAEB9C6B1C9048E6C522F70F13F73980D40238E3E21C14934D037563D930F"
3739
"48198A0AA7C14058229493D22530F4DBFA336F6E0AC925139543AED44CCE7C37"
@@ -76,9 +78,10 @@ def create(self):
7678
https://core.telegram.org/mtproto/auth_key
7779
https://core.telegram.org/mtproto/samples-auth_key
7880
"""
81+
retries_left = self.MAX_RETRIES
7982

8083
# The server may close the connection at any time, causing the auth key creation to fail.
81-
# If that happens, just try again until it succeed.
84+
# If that happens, just try again up to MAX_RETRIES times.
8285
while True:
8386
try:
8487
log.info("Start creating a new auth key on DC{}".format(self.dc_id))
@@ -243,6 +246,11 @@ def create(self):
243246
)
244247
)
245248
except Exception as e:
249+
if retries_left:
250+
retries_left -= 1
251+
else:
252+
raise e
253+
246254
log.warning("Auth key creation failed. Let's try again: {}".format(repr(e)))
247255
time.sleep(1)
248256
continue

0 commit comments

Comments
 (0)