Skip to content

Commit

Permalink
Simplify socket timeout (#967)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored Oct 7, 2024
1 parent 811ee20 commit 26d2328
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions pychromecast/socket_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@

HB_PING_TIME = 10
HB_PONG_TIME = 10
POLL_TIME_BLOCKING = 5.0
POLL_TIME_NON_BLOCKING = 0.01
SELECT_TIMEOUT = 5.0
TIMEOUT_TIME = 30.0
RETRY_TIME = 5.0

Expand Down Expand Up @@ -540,7 +539,7 @@ def run(self) -> None:
self.logger.debug("Thread started...")
while not self.stop.is_set():
try:
if self._run_once(timeout=POLL_TIME_BLOCKING) == 1:
if self._run_once() == 1:
break
except Exception: # pylint: disable=broad-except
self._force_recon = True
Expand All @@ -555,7 +554,7 @@ def run(self) -> None:
# Clean up
self._cleanup()

def _run_once(self, timeout: float = POLL_TIME_NON_BLOCKING) -> int:
def _run_once(self) -> int:
"""Receive from the socket and handle data."""
# pylint: disable=too-many-branches, too-many-statements, too-many-return-statements

Expand All @@ -568,9 +567,13 @@ def _run_once(self, timeout: float = POLL_TIME_NON_BLOCKING) -> int:
# A connection has been established at this point by self._check_connection
assert self.socket is not None

# poll the socket, as well as the socketpair to allow us to be interrupted
# Poll the socket and the socketpair, with a timeout of SELECT_TIMEOUT
# The timeout ensures we call _check_connection often enough to avoid
# the HeartbeatController from detecting a timeout
# The socketpair allow us to be interrupted on shutdown without waiting
# for the SELECT_TIMEOUT timout to expire
try:
ready = self.selector.select(timeout)
ready = self.selector.select(SELECT_TIMEOUT)
except (ValueError, OSError) as exc:
self.logger.error(
"[%s(%s):%s] Error in select call: %s",
Expand Down

0 comments on commit 26d2328

Please sign in to comment.