@@ -835,7 +835,6 @@ def __init__(
835
835
self ._wire_bytes_in = 0
836
836
self ._wire_bytes_out = 0
837
837
self .ping_callback = None # type: Optional[PeriodicCallback]
838
- self .last_ping = 0.0
839
838
self .last_pong = 0.0
840
839
self .close_code = None # type: Optional[int]
841
840
self .close_reason = None # type: Optional[str]
@@ -1303,38 +1302,29 @@ def start_pinging(self) -> None:
1303
1302
"""Start sending periodic pings to keep the connection alive"""
1304
1303
assert self .ping_interval is not None
1305
1304
if self .ping_interval > 0 :
1306
- self .last_ping = self .last_pong = IOLoop .current ().time ()
1307
1305
self .ping_callback = PeriodicCallback (
1308
1306
self .periodic_ping , self .ping_interval * 1000
1309
1307
)
1310
1308
self .ping_callback .start ()
1311
1309
1312
- def periodic_ping (self ) -> None :
1313
- """Send a ping to keep the websocket alive
1310
+ async def periodic_ping (self ) -> None :
1311
+ """Send a ping and wait for a pong if ping_timeout is configured.
1314
1312
1315
1313
Called periodically if the websocket_ping_interval is set and non-zero.
1316
1314
"""
1317
- if self .is_closing () and self .ping_callback is not None :
1318
- self .ping_callback .stop ()
1319
- return
1320
-
1321
- # Check for timeout on pong. Make sure that we really have
1322
- # sent a recent ping in case the machine with both server and
1323
- # client has been suspended since the last ping.
1324
1315
now = IOLoop .current ().time ()
1325
- since_last_pong = now - self .last_pong
1326
- since_last_ping = now - self .last_ping
1327
- assert self .ping_interval is not None
1328
- assert self .ping_timeout is not None
1329
- if (
1330
- since_last_ping < 2 * self .ping_interval
1331
- and since_last_pong > self .ping_timeout
1332
- ):
1333
- self .close ()
1334
- return
1335
1316
1317
+ # send a ping
1336
1318
self .write_ping (b"" )
1337
- self .last_ping = now
1319
+
1320
+ if self .ping_timeout and self .ping_timeout > 0 :
1321
+ # wait for the pong
1322
+ await asyncio .sleep (self .ping_timeout )
1323
+
1324
+ # close the connection if the pong is not received within the
1325
+ # configured timeout
1326
+ if self .last_pong - now > self .ping_timeout :
1327
+ self .close ()
1338
1328
1339
1329
def set_nodelay (self , x : bool ) -> None :
1340
1330
self .stream .set_nodelay (x )
0 commit comments