Skip to content

Commit 26d2328

Browse files
authored
Simplify socket timeout (#967)
1 parent 811ee20 commit 26d2328

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

pychromecast/socket_client.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@
6767

6868
HB_PING_TIME = 10
6969
HB_PONG_TIME = 10
70-
POLL_TIME_BLOCKING = 5.0
71-
POLL_TIME_NON_BLOCKING = 0.01
70+
SELECT_TIMEOUT = 5.0
7271
TIMEOUT_TIME = 30.0
7372
RETRY_TIME = 5.0
7473

@@ -540,7 +539,7 @@ def run(self) -> None:
540539
self.logger.debug("Thread started...")
541540
while not self.stop.is_set():
542541
try:
543-
if self._run_once(timeout=POLL_TIME_BLOCKING) == 1:
542+
if self._run_once() == 1:
544543
break
545544
except Exception: # pylint: disable=broad-except
546545
self._force_recon = True
@@ -555,7 +554,7 @@ def run(self) -> None:
555554
# Clean up
556555
self._cleanup()
557556

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

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

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

0 commit comments

Comments
 (0)