-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Integrate websocket periodic pinging code from Jupyter #1642
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ | |
from tornado.concurrent import TracebackFuture | ||
from tornado.escape import utf8, native_str, to_unicode | ||
from tornado import httpclient, httputil | ||
from tornado.ioloop import IOLoop | ||
from tornado.ioloop import IOLoop, PeriodicCallback | ||
from tornado.iostream import StreamClosedError | ||
from tornado.log import gen_log, app_log | ||
from tornado import simple_httpclient | ||
|
@@ -193,6 +193,29 @@ def get(self, *args, **kwargs): | |
"Sec-WebSocket-Version: 7, 8, 13\r\n\r\n")) | ||
self.stream.close() | ||
|
||
ping_callback = None | ||
last_ping = 0 | ||
last_pong = 0 | ||
stream = None | ||
|
||
@property | ||
def ping_interval(self): | ||
"""The interval for websocket keep-alive pings. | ||
|
||
Set ws_ping_interval = 0 to disable pings. | ||
""" | ||
return self.settings.get('websocket_ping_interval', 0) | ||
|
||
@property | ||
def ping_timeout(self): | ||
"""If no ping is received in this many seconds, | ||
close the websocket connection (VPNs, etc. can fail to cleanly close ws connections). | ||
Default is max of 3 pings or 30 seconds. | ||
""" | ||
return self.settings.get('websocket_ping_timeout', | ||
max(3 * self.ping_interval, 30) | ||
) | ||
|
||
def write_message(self, message, binary=False): | ||
"""Sends the given message to the client of this Web Socket. | ||
|
||
|
@@ -251,6 +274,39 @@ def open(self, *args, **kwargs): | |
""" | ||
pass | ||
|
||
def start_pinging(self): | ||
"""Start sending periodic pings to keep the connection alive""" | ||
if self.ping_interval > 0: | ||
loop = IOLoop.current() | ||
self.last_ping = loop.time() # Remember time of last ping | ||
self.last_pong = self.last_ping | ||
self.ping_callback = PeriodicCallback( | ||
self.send_ping, self.ping_interval*1000, io_loop=loop, | ||
) | ||
self.ping_callback.start() | ||
|
||
def send_ping(self): | ||
"""Send a ping to keep the websocket alive | ||
|
||
Called periodically if the websocket_ping_interval is set and non-zero. | ||
""" | ||
if self.stream.closed() and self.ping_callback is not None: | ||
self.ping_callback.stop() | ||
return | ||
|
||
# check for timeout on pong. Make sure that we really have sent a recent ping in | ||
# case the machine with both server and client has been suspended since the last ping. | ||
now = IOLoop.current().time() | ||
since_last_pong = now - self.last_pong | ||
since_last_ping = now - self.last_ping | ||
if since_last_ping < 2*self.ping_interval and since_last_pong > self.ping_timeout: | ||
self.log.warn("WebSocket ping timeout after %i ms.", since_last_pong) | ||
self.close() | ||
return | ||
|
||
self.ping(b'') | ||
self.last_ping = now | ||
|
||
def on_message(self, message): | ||
"""Handle incoming messages on the WebSocket | ||
|
||
|
@@ -265,8 +321,13 @@ def ping(self, data): | |
self.ws_connection.write_ping(data) | ||
|
||
def on_pong(self, data): | ||
"""Invoked when the response to a ping frame is received.""" | ||
pass | ||
"""Invoked when the response to a ping frame is received. | ||
|
||
If you override this, be sure to call the parent method, otherwise | ||
tornado's regular pinging may decide that the connection has dropped | ||
and close the websocket. | ||
""" | ||
self.last_pong = IOLoop.current().time() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's better to do this outside the overridable method so there's no question about calling the superclass method. |
||
|
||
def on_close(self): | ||
"""Invoked when the WebSocket is closed. | ||
|
@@ -592,6 +653,7 @@ def _accept_connection(self): | |
"\r\n" % (self._challenge_response(), | ||
subprotocol_header, extension_header))) | ||
|
||
self._run_callback(self.handler.start_pinging) | ||
self._run_callback(self.handler.open, *self.handler.open_args, | ||
**self.handler.open_kwargs) | ||
self._receive_frame() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no such thing as
self.log.warn
here.