Skip to content

Commit 8e13b26

Browse files
committed
Code review feedback
1 parent 21531c0 commit 8e13b26

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,21 @@ feature.
8989
```python
9090
async def heartbeat(ws, timeout, interval):
9191
'''
92-
Send periodic pings on WebSocket ``ws``. Wait up to ``timeout`` seconds to
93-
receive a pong before raising an exception. If a pong is received, then wait
94-
``interval`` seconds before sending the next ping.
92+
Send periodic pings on WebSocket ``ws``.
93+
94+
Wait up to ``timeout`` seconds to send a ping and receive a pong. Raises
95+
``TooSlowError`` if the timeout is exceeded. If a pong is received, then
96+
wait ``interval`` seconds before sending the next ping.
97+
98+
This function runs until cancelled.
99+
100+
:param ws: A WebSocket to send heartbeat pings on.
101+
:param float timeout: Timeout in seconds.
102+
:param float interval: Interval between receiving pong and sending next
103+
ping, in seconds.
104+
:raises: ``ConnectionClosed`` if ``ws`` is closed.
105+
:raises: ``TooSlowError`` if the timeout expires.
106+
:returns: This function runs until cancelled.
95107
'''
96108
while True:
97109
with trio.fail_after(timeout):

examples/client.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,20 @@ async def handle_connection(ws, use_heartbeat):
7979
async def heartbeat(ws, timeout, interval):
8080
'''
8181
Send periodic pings on WebSocket ``ws``.
82-
After sending a ping, wait up to ``timeout`` seconds to receive a pong
83-
before raising an exception. If a pong is received, then wait ``interval``
84-
seconds before sending the next ping.
82+
83+
Wait up to ``timeout`` seconds to send a ping and receive a pong. Raises
84+
``TooSlowError`` if the timeout is exceeded. If a pong is received, then
85+
wait ``interval`` seconds before sending the next ping.
86+
87+
This function runs until cancelled.
88+
89+
:param ws: A WebSocket to send heartbeat pings on.
90+
:param float timeout: Timeout in seconds.
91+
:param float interval: Interval between receiving pong and sending next
92+
ping, in seconds.
93+
:raises: ``ConnectionClosed`` if ``ws`` is closed.
94+
:raises: ``TooSlowError`` if the timeout expires.
95+
:returns: This function runs until cancelled.
8596
'''
8697
while True:
8798
with trio.fail_after(timeout):

trio_websocket/__init__.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,11 @@ async def ping(self, payload=None):
425425
format(payload))
426426
if payload is None:
427427
payload = struct.pack('!I', random.getrandbits(32))
428-
self._pings[payload] = trio.Event()
428+
event = trio.Event()
429+
self._pings[payload] = event
429430
self._wsproto.ping(payload)
430431
await self._write_pending()
431-
await self._pings[payload].wait()
432+
await event.wait()
432433

433434
async def pong(self, payload=None):
434435
'''
@@ -589,13 +590,13 @@ async def _handle_pong_received_event(self, event):
589590
# We received a pong that doesn't match any in-flight pongs. Nothing
590591
# we can do with it, so ignore it.
591592
return
592-
key, event = self._pings.popitem(0)
593-
while key != payload:
594-
logger.debug('conn#%d pong [skipped] %r', self._id, key)
595-
event.set()
593+
while self._pings:
596594
key, event = self._pings.popitem(0)
597-
logger.debug('conn#%d pong %r', self._id, key)
598-
event.set()
595+
skipped = ' [skipped] ' if payload != key else ' '
596+
logger.debug('conn#%d pong%s%r', self._id, skipped, key)
597+
event.set()
598+
if payload == key:
599+
break
599600

600601
async def _reader_task(self):
601602
''' A background task that reads network data and generates events. '''
@@ -624,7 +625,7 @@ async def _reader_task(self):
624625
event_type)
625626
await handler(event)
626627
except KeyError:
627-
logger.warning('Received unknown event type: %s',
628+
logger.warning('Received unknown event type: "%s"',
628629
event_type)
629630

630631
# Get network data.

0 commit comments

Comments
 (0)