]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
websocket_ping: fix ping interval with non-zero timeout and improve docs.
authorOliver Sanders <oliver.sanders@metoffice.gov.uk>
Thu, 19 Jun 2025 10:06:29 +0000 (11:06 +0100)
committerOliver Sanders <oliver.sanders@metoffice.gov.uk>
Thu, 19 Jun 2025 10:48:47 +0000 (11:48 +0100)
* Fix a bug that caused the ping interval to be less frequent than
  configured.
* Fix erroneous documentation of the websocket_ping_timeout default and
  clarify units for the ping interval.

docs/web.rst
tornado/websocket.py

index 956336bda04054206f4bd2a141331e32eaac86cc..00066ccdfd44aea7831ca27d15ca20e4547222a1 100644 (file)
            of `UIModule` or UI methods to be made available to templates.
            May be set to a module, dictionary, or a list of modules
            and/or dicts.  See :ref:`ui-modules` for more details.
-         * ``websocket_ping_interval``: If set to a number, all websockets will
-           be pinged every n seconds. This can help keep the connection alive
-           through certain proxy servers which close idle connections, and it
-           can detect if the websocket has failed without being properly closed.
-         * ``websocket_ping_timeout``: If the ping interval is set, and the
-           server doesn't receive a 'pong' in this many seconds, it will close
-           the websocket. The default is three times the ping interval, with a
-           minimum of 30 seconds. Ignored if the ping interval is not set.
+         * ``websocket_ping_interval``: If the ping interval has a non-zero
+           value, a ping will be sent periodically every
+           ``websocket_ping_interval`` seconds, and the connection will be
+           closed if a response is not received before the
+           ``websocket_ping_timeout``.
+           This can help keep the connection alive through certain proxy
+           servers which close idle connections, and it can detect if the
+           websocket has failed without being properly closed.
+         * ``websocket_ping_timeout``: For use with ``websocket_ping_interval``,
+           if the server does not receive a pong within this many seconds, it
+           will close the websocket_ping_timeout.
+           The default timeout is equal to the ping interval. The ping timeout
+           will be turned off if the ping interval is not set or if the
+           timeout is set to ``0``.
+           This can help to detect disconnected clients to avoid keeping
+           inactive connections open.
 
          Authentication and security settings:
 
index b719547bdf94e9623a9050a2eea3c911f655f6bc..2e40636925ab15fa345a1035948b0c9b83513a50 100644 (file)
@@ -1371,7 +1371,7 @@ class WebSocketProtocol13(WebSocketProtocol):
                 return
 
             # wait until the next scheduled ping
-            await asyncio.sleep(IOLoop.current().time() - ping_time + interval)
+            await asyncio.sleep(ping_time + interval - IOLoop.current().time())
 
 
 class WebSocketClientConnection(simple_httpclient._HTTPConnection):