From 62f4ca250c945dc8953f4d7e5c6f99b516279730 Mon Sep 17 00:00:00 2001 From: Oliver Sanders Date: Thu, 15 Feb 2024 02:17:54 +0000 Subject: [PATCH] websockets: add configurations for ping interval and timeout (#1391) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- jupyter_server/serverapp.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/jupyter_server/serverapp.py b/jupyter_server/serverapp.py index 9b451310ca..5002715c90 100644 --- a/jupyter_server/serverapp.py +++ b/jupyter_server/serverapp.py @@ -240,6 +240,8 @@ def __init__( authorizer=None, identity_provider=None, kernel_websocket_connection_class=None, + websocket_ping_interval=None, + websocket_ping_timeout=None, ): """Initialize a server web application.""" if identity_provider is None: @@ -277,6 +279,8 @@ def __init__( authorizer=authorizer, identity_provider=identity_provider, kernel_websocket_connection_class=kernel_websocket_connection_class, + websocket_ping_interval=websocket_ping_interval, + websocket_ping_timeout=websocket_ping_timeout, ) handlers = self.init_handlers(default_services, settings) @@ -301,6 +305,8 @@ def init_settings( authorizer=None, identity_provider=None, kernel_websocket_connection_class=None, + websocket_ping_interval=None, + websocket_ping_timeout=None, ): """Initialize settings for the web application.""" _template_path = settings_overrides.get( @@ -383,6 +389,8 @@ def init_settings( "identity_provider": identity_provider, "event_logger": event_logger, "kernel_websocket_connection_class": kernel_websocket_connection_class, + "websocket_ping_interval": websocket_ping_interval, + "websocket_ping_timeout": websocket_ping_timeout, # handlers "extra_services": extra_services, # Jupyter stuff @@ -1515,6 +1523,32 @@ def _default_kernel_websocket_connection_class( return "jupyter_server.gateway.connections.GatewayWebSocketConnection" return ZMQChannelsWebsocketConnection + websocket_ping_interval = Integer( + config=True, + help=""" + Configure the websocket ping interval in seconds. + + Websockets are long-lived connections that are used by some Jupyter + Server extensions. + + Periodic pings help to detect disconnected clients and keep the + connection active. If this is set to None, then no pings will be + performed. + + When a ping is sent, the client has ``websocket_ping_timeout`` + seconds to respond. If no response is received within this period, + the connection will be closed from the server side. + """, + ) + websocket_ping_timeout = Integer( + config=True, + help=""" + Configure the websocket ping timeout in seconds. + + See ``websocket_ping_interval`` for details. + """, + ) + config_manager_class = Type( default_value=ConfigManager, config=True, @@ -2101,6 +2135,8 @@ def init_webapp(self) -> None: authorizer=self.authorizer, identity_provider=self.identity_provider, kernel_websocket_connection_class=self.kernel_websocket_connection_class, + websocket_ping_interval=self.websocket_ping_interval, + websocket_ping_timeout=self.websocket_ping_timeout, ) if self.certfile: self.ssl_options["certfile"] = self.certfile