New websockets (#2158)
* First attempt at new Websockets implementation based on websockets >= 9.0, with sans-i/o features. Requires more work. * Update sanic/websocket.py Co-authored-by: Adam Hopkins <adam@amhopkins.com> * Update sanic/websocket.py Co-authored-by: Adam Hopkins <adam@amhopkins.com> * Update sanic/websocket.py Co-authored-by: Adam Hopkins <adam@amhopkins.com> * wip, update websockets code to new Sans/IO API * Refactored new websockets impl into own modules Incorporated other suggestions made by team * Another round of work on the new websockets impl * Added websocket_timeout support (matching previous/legacy support) * Lots more comments * Incorporated suggested changes from previous round of review * Changed RuntimeError usage to ServerError * Changed SanicException usage to ServerError * Removed some redundant asserts * Change remaining asserts to ServerErrors * Fixed some timeout handling issues * Fixed websocket.close() handling, and made it more robust * Made auto_close task smarter and more error-resilient * Made fail_connection routine smarter and more error-resilient * Further new websockets impl fixes * Update compatibility with Websockets v10 * Track server connection state in a more precise way * Try to handle the shutdown process more gracefully * Add a new end_connection() helper, to use as an alterative to close() or fail_connection() * Kill the auto-close task and keepalive-timeout task when sanic is shutdown * Deprecate WEBSOCKET_READ_LIMIT and WEBSOCKET_WRITE_LIMIT configs, they are not used in this implementation. * Change a warning message to debug level Remove default values for deprecated websocket parameters * Fix flake8 errors * Fix a couple of missed failing tests * remove websocket bench from examples * Integrate suggestions from code reviews Use Optional[T] instead of union[T,None] Fix mypy type logic errors change "is not None" to truthy checks where appropriate change "is None" to falsy checks were appropriate Add more debug logging when debug mode is on Change to using sanic.logger for debug logging rather than error_logger. * Fix long line lengths of debug messages Add some new debug messages when websocket IO is paused and unpaused for flow control Fix websocket example to use app.static() * remove unused import in websocket example app * re-run isort after Flake8 fixes Co-authored-by: Adam Hopkins <adam@amhopkins.com> Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
This commit is contained in:
@@ -178,9 +178,6 @@ def test_app_enable_websocket(app, websocket_enabled, enable):
|
||||
@patch("sanic.app.WebSocketProtocol")
|
||||
def test_app_websocket_parameters(websocket_protocol_mock, app):
|
||||
app.config.WEBSOCKET_MAX_SIZE = 44
|
||||
app.config.WEBSOCKET_MAX_QUEUE = 45
|
||||
app.config.WEBSOCKET_READ_LIMIT = 46
|
||||
app.config.WEBSOCKET_WRITE_LIMIT = 47
|
||||
app.config.WEBSOCKET_PING_TIMEOUT = 48
|
||||
app.config.WEBSOCKET_PING_INTERVAL = 50
|
||||
|
||||
@@ -197,11 +194,6 @@ def test_app_websocket_parameters(websocket_protocol_mock, app):
|
||||
websocket_protocol_call_args = websocket_protocol_mock.call_args
|
||||
ws_kwargs = websocket_protocol_call_args[1]
|
||||
assert ws_kwargs["websocket_max_size"] == app.config.WEBSOCKET_MAX_SIZE
|
||||
assert ws_kwargs["websocket_max_queue"] == app.config.WEBSOCKET_MAX_QUEUE
|
||||
assert ws_kwargs["websocket_read_limit"] == app.config.WEBSOCKET_READ_LIMIT
|
||||
assert (
|
||||
ws_kwargs["websocket_write_limit"] == app.config.WEBSOCKET_WRITE_LIMIT
|
||||
)
|
||||
assert (
|
||||
ws_kwargs["websocket_ping_timeout"]
|
||||
== app.config.WEBSOCKET_PING_TIMEOUT
|
||||
|
||||
@@ -10,7 +10,7 @@ from sanic.asgi import MockTransport
|
||||
from sanic.exceptions import Forbidden, InvalidUsage, ServiceUnavailable
|
||||
from sanic.request import Request
|
||||
from sanic.response import json, text
|
||||
from sanic.websocket import WebSocketConnection
|
||||
from sanic.server.websockets.connection import WebSocketConnection
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
||||
@@ -16,6 +16,7 @@ from sanic.exceptions import (
|
||||
abort,
|
||||
)
|
||||
from sanic.response import text
|
||||
from websockets.version import version as websockets_version
|
||||
|
||||
|
||||
class SanicExceptionTestException(Exception):
|
||||
@@ -260,9 +261,14 @@ def test_exception_in_ws_logged(caplog):
|
||||
|
||||
with caplog.at_level(logging.INFO):
|
||||
app.test_client.websocket("/feed")
|
||||
|
||||
assert caplog.record_tuples[1][0] == "sanic.error"
|
||||
assert caplog.record_tuples[1][1] == logging.ERROR
|
||||
# Websockets v10.0 and above output an additional
|
||||
# INFO message when a ws connection is accepted
|
||||
ws_version_parts = websockets_version.split(".")
|
||||
ws_major = int(ws_version_parts[0])
|
||||
record_index = 2 if ws_major >= 10 else 1
|
||||
assert caplog.record_tuples[record_index][0] == "sanic.error"
|
||||
assert caplog.record_tuples[record_index][1] == logging.ERROR
|
||||
assert (
|
||||
"Exception occurred while handling uri:" in caplog.record_tuples[1][2]
|
||||
"Exception occurred while handling uri:"
|
||||
in caplog.record_tuples[record_index][2]
|
||||
)
|
||||
|
||||
@@ -674,16 +674,16 @@ async def test_websocket_route_asgi(app, url):
|
||||
@pytest.mark.parametrize(
|
||||
"subprotocols,expected",
|
||||
(
|
||||
(["bar"], "bar"),
|
||||
(["bar", "foo"], "bar"),
|
||||
(["baz"], None),
|
||||
(["one"], "one"),
|
||||
(["three", "one"], "one"),
|
||||
(["tree"], None),
|
||||
(None, None),
|
||||
),
|
||||
)
|
||||
def test_websocket_route_with_subprotocols(app, subprotocols, expected):
|
||||
results = "unset"
|
||||
results = []
|
||||
|
||||
@app.websocket("/ws", subprotocols=["foo", "bar"])
|
||||
@app.websocket("/ws", subprotocols=["zero", "one", "two", "three"])
|
||||
async def handler(request, ws):
|
||||
nonlocal results
|
||||
results = ws.subprotocol
|
||||
|
||||
@@ -175,7 +175,7 @@ def test_worker_close(worker):
|
||||
worker.wsgi = mock.Mock()
|
||||
conn = mock.Mock()
|
||||
conn.websocket = mock.Mock()
|
||||
conn.websocket.close_connection = mock.Mock(wraps=_a_noop)
|
||||
conn.websocket.fail_connection = mock.Mock(wraps=_a_noop)
|
||||
worker.connections = set([conn])
|
||||
worker.log = mock.Mock()
|
||||
worker.loop = loop
|
||||
@@ -190,5 +190,5 @@ def test_worker_close(worker):
|
||||
loop.run_until_complete(_close)
|
||||
|
||||
assert worker.signal.stopped
|
||||
assert conn.websocket.close_connection.called
|
||||
assert conn.websocket.fail_connection.called
|
||||
assert len(worker.servers) == 0
|
||||
|
||||
Reference in New Issue
Block a user