Skip to content

Commit

Permalink
Add option to set websocket ping interval and timeout (#1048)
Browse files Browse the repository at this point in the history
* 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 <marcelotryle@gmail.com>

* Corrected flags

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>

* Updated docs

* Update docs/settings.md

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>

* Update docs/settings.md

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
  • Loading branch information
euri10 and Kludex committed Jun 9, 2021
1 parent 87da6cf commit f66e717
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/deployment.md
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/index.md
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/settings.md
Expand Up @@ -43,6 +43,8 @@ you should put `uvicorn.run` into `if __name__ == '__main__'` clause in the main
* `--http <str>` - 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 <str>` - 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 <int>` - Set the WebSockets max message size, in bytes. Please note that this can be used only with the default `websockets` protocol.
* `--ws-ping-interval <float>` - Set the WebSockets ping interval, in seconds. Please note that this can be used only with the default `websockets` protocol.
* `--ws-ping-timeout <float>` - Set the WebSockets ping timeout, in seconds. Please note that this can be used only with the default `websockets` protocol.
* `--lifespan <str>` - Set the Lifespan protocol implementation. **Options:** *'auto', 'on', 'off'.* **Default:** *'auto'*.

## Application Interface
Expand Down
4 changes: 4 additions & 0 deletions uvicorn/config.py
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions uvicorn/main.py
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion uvicorn/protocols/websockets/websockets_impl.py
Expand Up @@ -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):
Expand Down

0 comments on commit f66e717

Please sign in to comment.