From f66e7170ee5327afa2576ce01fb7653e462e7c6f Mon Sep 17 00:00:00 2001 From: euri10 Date: Wed, 9 Jun 2021 09:51:46 +0200 Subject: [PATCH] Add option to set websocket ping interval and timeout (#1048) * Add max size to webosckets implementation * Adde ping interval and timeout to websockets inplmenetation * Post-merge glitches corrected * Added some docs * Corrected flags Co-authored-by: Marcelo Trylesinski * Corrected flags Co-authored-by: Marcelo Trylesinski * Updated docs * Update docs/settings.md Co-authored-by: Marcelo Trylesinski * Update docs/settings.md Co-authored-by: Marcelo Trylesinski Co-authored-by: Marcelo Trylesinski --- docs/deployment.md | 2 ++ docs/index.md | 2 ++ docs/settings.md | 2 ++ uvicorn/config.py | 4 ++++ uvicorn/main.py | 18 ++++++++++++++++++ .../protocols/websockets/websockets_impl.py | 4 +++- 6 files changed, 31 insertions(+), 1 deletion(-) diff --git a/docs/deployment.md b/docs/deployment.md index 3c5a08a2c..04de3842d 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -51,6 +51,8 @@ Options: [default: auto] --ws-max-size INTEGER WebSocket max size message in bytes [default: 16777216] + --ws-ping-interval FLOAT WebSocket ping interval [default: 20.0] + --ws-ping-timeout FLOAT WebSocket ping timeout [default: 20.0] --lifespan [auto|on|off] Lifespan implementation. [default: auto] --interface [auto|asgi3|asgi2|wsgi] Select ASGI3, ASGI2, or WSGI as the diff --git a/docs/index.md b/docs/index.md index c51429f4c..666ff9c50 100644 --- a/docs/index.md +++ b/docs/index.md @@ -121,6 +121,8 @@ Options: [default: auto] --ws-max-size INTEGER WebSocket max size message in bytes [default: 16777216] + --ws-ping-interval FLOAT WebSocket ping interval [default: 20.0] + --ws-ping-timeout FLOAT WebSocket ping timeout [default: 20.0] --lifespan [auto|on|off] Lifespan implementation. [default: auto] --interface [auto|asgi3|asgi2|wsgi] Select ASGI3, ASGI2, or WSGI as the diff --git a/docs/settings.md b/docs/settings.md index 81aebb15f..42ded24b5 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -43,6 +43,8 @@ you should put `uvicorn.run` into `if __name__ == '__main__'` clause in the main * `--http ` - Set the HTTP protocol implementation. The httptools implementation provides greater performance, but it not compatible with PyPy, and requires compilation on Windows. **Options:** *'auto', 'h11', 'httptools'.* **Default:** *'auto'*. * `--ws ` - Set the WebSockets protocol implementation. Either of the `websockets` and `wsproto` packages are supported. Use `'none'` to deny all websocket requests. **Options:** *'auto', 'none', 'websockets', 'wsproto'.* **Default:** *'auto'*. * `--ws-max-size ` - Set the WebSockets max message size, in bytes. Please note that this can be used only with the default `websockets` protocol. +* `--ws-ping-interval ` - Set the WebSockets ping interval, in seconds. Please note that this can be used only with the default `websockets` protocol. +* `--ws-ping-timeout ` - Set the WebSockets ping timeout, in seconds. Please note that this can be used only with the default `websockets` protocol. * `--lifespan ` - Set the Lifespan protocol implementation. **Options:** *'auto', 'on', 'off'.* **Default:** *'auto'*. ## Application Interface diff --git a/uvicorn/config.py b/uvicorn/config.py index fb39ef5d6..df21f45e2 100644 --- a/uvicorn/config.py +++ b/uvicorn/config.py @@ -132,6 +132,8 @@ def __init__( http="auto", ws="auto", ws_max_size=16 * 1024 * 1024, + ws_ping_interval=20, + ws_ping_timeout=20, lifespan="auto", env_file=None, log_config=LOGGING_CONFIG, @@ -172,6 +174,8 @@ def __init__( self.http = http self.ws = ws self.ws_max_size = ws_max_size + self.ws_ping_interval = ws_ping_interval + self.ws_ping_timeout = ws_ping_timeout self.lifespan = lifespan self.log_config = log_config self.log_level = log_level diff --git a/uvicorn/main.py b/uvicorn/main.py index 424b388d8..fb31a3ab4 100644 --- a/uvicorn/main.py +++ b/uvicorn/main.py @@ -121,6 +121,20 @@ def print_version(ctx: click.Context, param: click.Parameter, value: bool) -> No help="WebSocket max size message in bytes", show_default=True, ) +@click.option( + "--ws-ping-interval", + type=float, + default=20, + help="WebSocket ping interval", + show_default=True, +) +@click.option( + "--ws-ping-timeout", + type=float, + default=20, + help="WebSocket ping timeout", + show_default=True, +) @click.option( "--lifespan", type=LIFESPAN_CHOICES, @@ -298,6 +312,8 @@ def main( http: str, ws: str, ws_max_size: int, + ws_ping_interval: float, + ws_ping_timeout: float, lifespan: str, interface: str, debug: bool, @@ -339,6 +355,8 @@ def main( "http": http, "ws": ws, "ws_max_size": ws_max_size, + "ws_ping_interval": ws_ping_interval, + "ws_ping_timeout": ws_ping_timeout, "lifespan": lifespan, "env_file": env_file, "log_config": LOGGING_CONFIG if log_config is None else log_config, diff --git a/uvicorn/protocols/websockets/websockets_impl.py b/uvicorn/protocols/websockets/websockets_impl.py index f166f7185..f9277b74f 100644 --- a/uvicorn/protocols/websockets/websockets_impl.py +++ b/uvicorn/protocols/websockets/websockets_impl.py @@ -62,8 +62,10 @@ def __init__( super().__init__( ws_handler=self.ws_handler, ws_server=self.ws_server, - extensions=[ServerPerMessageDeflateFactory()], max_size=self.config.ws_max_size, + ping_interval=self.config.ws_ping_interval, + ping_timeout=self.config.ws_ping_timeout, + extensions=[ServerPerMessageDeflateFactory()], ) def connection_made(self, transport):