Skip to content
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

Add option to set websocket ping interval and timeout #1048

Merged
merged 10 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/deployment.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.
euri10 marked this conversation as resolved.
Show resolved Hide resolved
* `--ws-ping_timeout <float>` - Set the WebSockets ping timeout, in seconds. Please note that this can be used only with the default `websockets` protocol.
euri10 marked this conversation as resolved.
Show resolved Hide resolved
* `--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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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",
euri10 marked this conversation as resolved.
Show resolved Hide resolved
type=float,
default=20,
help="WebSocket ping interval",
show_default=True,
)
@click.option(
"--ws_ping_timeout",
euri10 marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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