Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asyncio-based Socket Mode client improvements #1117

Merged
merged 5 commits into from Sep 18, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 19 additions & 9 deletions slack_sdk/socket_mode/aiohttp/__init__.py
Expand Up @@ -154,18 +154,17 @@ async def monitor_current_session(self) -> None:
)
should_reconnect = True

if self.last_ping_pong_time is not None:
if await self.is_ping_pong_failing():
disconnected_seconds = int(
time.time() - self.last_ping_pong_time
)
if disconnected_seconds >= (self.ping_interval * 4):
self.logger.info(
"The connection seems to be stale. Reconnecting..."
f" reason: disconnected for {disconnected_seconds}+ seconds)"
)
self.stale = True
self.last_ping_pong_time = None
should_reconnect = True
self.logger.info(
"The connection seems to be stale. Reconnecting..."
f" reason: disconnected for {disconnected_seconds}+ seconds)"
)
self.stale = True
self.last_ping_pong_time = None
should_reconnect = True

if should_reconnect is True or not await self.is_connected():
await self.connect_to_new_endpoint()
Expand Down Expand Up @@ -253,12 +252,19 @@ async def receive_messages(self) -> None:
self.logger.debug("The running receive_messages task is now cancelled")
raise

async def is_ping_pong_failing(self) -> bool:
if self.last_ping_pong_time is None:
return False
disconnected_seconds = int(time.time() - self.last_ping_pong_time)
return disconnected_seconds >= (self.ping_interval * 4)

async def is_connected(self) -> bool:
return (
not self.closed
and not self.stale
and self.current_session is not None
and not self.current_session.closed
and not await self.is_ping_pong_failing()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the ping-pong validation logic to is_ping_pong_failing method and added it to is_connected() method

)

async def connect(self):
Expand All @@ -275,6 +281,10 @@ async def connect(self):
self.stale = False
self.logger.info("A new session has been established")

# The first ping from the new connection
t = time.time()
await self.current_session.ping(f"sdk-ping-pong:{t}")

if self.current_session_monitor is not None:
self.current_session_monitor.cancel()

Expand Down