Skip to content

Commit

Permalink
improvements + fix session .start() re-try on restart + better sleep …
Browse files Browse the repository at this point in the history
…time management + more logical

Signed-off-by: eyMarv <[email protected]>
  • Loading branch information
eyMarv committed Jul 20, 2024
1 parent fec1674 commit 9b551c8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
1 change: 0 additions & 1 deletion pyrogram/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ def __init__(
self.updates_watchdog_task = None
self.updates_watchdog_event = asyncio.Event()
self.updates_invoke_error = None
self.instant_stop = False # toggle via: client.instant_stop = True
self.last_update_time = datetime.now()
self.listeners = {
listener_type: [] for listener_type in pyrogram.enums.ListenerTypes
Expand Down
34 changes: 14 additions & 20 deletions pyrogram/session/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,15 @@ def __init__(

self.loop = asyncio.get_event_loop()

self.instant_stop = False
self.last_reconnect_attempt = None
self.currently_restarting = False
self.currently_stopping = False

async def start(self):
while True:
if self.client.instant_stop:
log.info("Session init stopped")
if self.instant_stop:
log.info("Session init force stopped (loop)")
return # stop instantly

self.connection = self.client.connection_factory(
Expand Down Expand Up @@ -184,13 +185,12 @@ async def start(self):
break

self.is_started.set()

log.info("Session started")

async def stop(self, restart: bool = False):
if self.currently_stopping:
return # don't stop twice
if self.client.instant_stop:
if self.instant_stop:
log.info("Session stop process stopped")
return # stop doing anything instantly, client is manually handling

Expand All @@ -200,7 +200,7 @@ async def stop(self, restart: bool = False):
self.stored_msg_ids.clear()

if restart:
self.client.instant_stop = True # tell all funcs that we want to stop
self.instant_stop = True # tell all funcs that we want to stop

self.ping_task_event.set()
for _ in self.STOP_RANGE:
Expand Down Expand Up @@ -243,12 +243,12 @@ async def stop(self, restart: bool = False):
finally:
self.currently_stopping = False
if restart:
self.client.instant_stop = False # reset
self.instant_stop = False # reset

async def restart(self):
if self.currently_restarting:
return # don't restart twice
if self.client.instant_stop:
if self.instant_stop:
return # stop instantly

try:
Expand All @@ -266,11 +266,12 @@ async def restart(self):
)
await asyncio.sleep(to_wait)

self.last_reconnect_attempt = now
self.last_reconnect_attempt = time()
await self.stop(restart=True)
for try_ in self.RE_START_RANGE: # sometimes, the DB says "no" 😬
try:
await self.start()
break
except ValueError as e: # SQLite error
try:
await self.client.load_session()
Expand All @@ -294,12 +295,11 @@ async def restart(self):
type(e).__name__,
e,
)
break
finally:
self.currently_restarting = False

async def handle_packet(self, packet):
if self.client.instant_stop:
if self.instant_stop:
log.info("Stopped packet handler")
return # stop instantly

Expand Down Expand Up @@ -398,14 +398,10 @@ async def handle_packet(self, packet):
self.pending_acks.clear()

async def ping_worker(self):
if self.client.instant_stop:
log.info("PingTask force stopped")
return # stop instantly

log.info("PingTask started")

while True:
if self.client.instant_stop:
if self.instant_stop:
log.info("PingTask force stopped (loop)")
return # stop instantly

Expand Down Expand Up @@ -435,7 +431,7 @@ async def recv_worker(self):
log.info("NetworkTask started")

while True:
if self.client.instant_stop:
if self.instant_stop:
log.info("NetworkTask force stopped (loop)")
return # stop instantly

Expand Down Expand Up @@ -474,7 +470,7 @@ async def send(
timeout: float = WAIT_TIMEOUT,
retry: int = 0,
):
if self.client.instant_stop:
if self.instant_stop:
return # stop instantly

message = self.msg_factory(data)
Expand Down Expand Up @@ -568,11 +564,9 @@ async def invoke(
# sleep until the restart is performed
if self.currently_restarting:
while self.currently_restarting:
if self.client.instant_stop:
return # stop instantly
await asyncio.sleep(1)

if self.client.instant_stop:
if self.instant_stop:
return # stop instantly

if not self.is_started.is_set():
Expand Down

0 comments on commit 9b551c8

Please sign in to comment.