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

New websockets #2158

Merged
merged 22 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
190ce7b
First attempt at new Websockets implementation based on websockets >=…
ashleysommer Jun 8, 2021
4b814ec
Merge remote-tracking branch 'origin/main' into new_websockets
ashleysommer Jun 9, 2021
4e9d984
Update sanic/websocket.py
ashleysommer Jun 16, 2021
2c8f750
Update sanic/websocket.py
ashleysommer Jun 16, 2021
e2d0198
Update sanic/websocket.py
ashleysommer Jun 16, 2021
e6379ea
Merge remote-tracking branch 'origin/main' into new_websockets
ashleysommer Aug 31, 2021
98785e7
Merge remote-tracking branch 'ashleysommer_github/new_websockets' int…
ashleysommer Sep 1, 2021
cc2082c
wip, update websockets code to new Sans/IO API
ashleysommer Sep 1, 2021
e687517
Merge branch 'main' into new_websockets
ashleysommer Sep 1, 2021
2436780
Refactored new websockets impl into own modules
ashleysommer Sep 1, 2021
b24e914
Merge remote-tracking branch 'origin/main' into new_websockets
ashleysommer Sep 15, 2021
aea3538
Another round of work on the new websockets impl
ashleysommer Sep 15, 2021
13d49b8
Further new websockets impl fixes
ashleysommer Sep 15, 2021
5f6cc06
Change a warning message to debug level
ashleysommer Sep 23, 2021
37d462a
Fix flake8 errors
ashleysommer Sep 23, 2021
cb495ac
Fix a couple of missed failing tests
ashleysommer Sep 23, 2021
955d515
remove websocket bench from examples
ashleysommer Sep 26, 2021
19c98b9
Integrate suggestions from code reviews
ashleysommer Sep 26, 2021
cd26e00
Merge branch 'main' into new_websockets
ahopkins Sep 27, 2021
791b693
Fix long line lengths of debug messages
ashleysommer Sep 28, 2021
f264477
remove unused import in websocket example app
ashleysommer Sep 28, 2021
2162854
re-run isort after Flake8 fixes
ashleysommer Sep 28, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions examples/websocket_bench.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!DOCTYPE html>
<html>
<head>
<title>WebSocket benchmark</title>
</head>
<body>
<script>
function make_msg(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var clen = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * clen));
}
return result;
}
var ws = new WebSocket('ws://' + document.domain + ':' + location.port + '/bench');
var msg = make_msg(16384);
var started = false;
function send_msg() {
if (ws.readyState === ws.OPEN) {
ws.send(msg);
window.setTimeout(send_msg, 1);
}
}
function run_bench() {
console.log("first msg seen, starting bench...");
window.setTimeout(send_msg, 0);
}
function stop_bench() {
ws.close(1000);
}
ws.onmessage = function(event) {
console.log("Received "+ event.data);
if (started === false) {
started = true;
window.setTimeout(run_bench, 0);
window.setTimeout(stop_bench, 40000);
}
};
ws.onclose = function(event) {
console.log("Websocket connection closed.");
}

</script>
</body>
</html>
70 changes: 70 additions & 0 deletions examples/websocket_bench.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import asyncio
import logging
from sanic import Sanic
from sanic.response import file
from sanic.log import error_logger
import time
error_logger.setLevel(logging.INFO)
app = Sanic(__name__)

@app.route('/')
async def index(request):
return await file('websocket_bench.html')
ashleysommer marked this conversation as resolved.
Show resolved Hide resolved


@app.websocket('/bench')
async def bench_p_time(request, ws):
i = 0
bytes_total = 0
start = 0
end_time = 0
started = False
await ws.send("1")
while started is False or (end_time > time.time()):
i += 1
in_data = await ws.recv()
if started is False:
del in_data
error_logger.info("received first data: starting benchmark now..")
started = True
start = time.time()
end_time = start + 30.0
continue
bytes_total += len(in_data)
del in_data
end = time.time()
elapsed = end - start
error_logger.info("Done. Took {} seconds".format(elapsed))
error_logger.info("{} bytes in 30 seconds = {}".format(bytes_total, (bytes_total/30.0)))

@app.websocket('/benchp')
async def bench_p_time(request, ws):
i = 0
bytes_total = 0
real_start = 0
start_ptime = 0
end_ptime = 0
started = False
await ws.send("1")
while started is False or (end_ptime > time.process_time()):
i += 1
in_data = await ws.recv()
if started is False:
del in_data
error_logger.info("received first data: starting benchmark now..")
started = True
real_start = time.time()
start_ptime = time.process_time()
end_ptime = start_ptime + 30.0
continue
bytes_total += len(in_data)
del in_data
real_end = time.time()
elapsed = real_end - real_start
error_logger.info("Done. Took {} seconds".format(elapsed))
error_logger.info("{} bytes in 30 seconds = {}".format(bytes_total, (bytes_total/30.0)))


if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000, debug=False, auto_reload=False)

23 changes: 8 additions & 15 deletions sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@
from sanic.server import AsyncioServer, HttpProtocol
from sanic.server import Signal as ServerSignal
from sanic.server import serve, serve_multiple, serve_single
from sanic.server.protocols.websocket_protocol import WebSocketProtocol
from sanic.server.websockets.impl import ConnectionClosed
from sanic.signals import Signal, SignalRouter
from sanic.touchup import TouchUp, TouchUpMeta
from sanic.websocket import ConnectionClosed, WebSocketProtocol


class Sanic(BaseSanic, metaclass=TouchUpMeta):
Expand Down Expand Up @@ -871,39 +872,31 @@ async def handle_request(self, request: Request): # no cov
async def _websocket_handler(
self, handler, request, *args, subprotocols=None, **kwargs
):
request.app = self
if not getattr(handler, "__blueprintname__", False):
request._name = handler.__name__
else:
request._name = (
getattr(handler, "__blueprintname__", "") + handler.__name__
)

pass

if self.asgi:
ws = request.transport.get_websocket_connection()
await ws.accept(subprotocols)
else:
protocol = request.transport.get_protocol()
protocol.app = self

ws = await protocol.websocket_handshake(request, subprotocols)

# schedule the application handler
# its future is kept in self.websocket_tasks in case it
# needs to be cancelled due to the server being stopped
fut = ensure_future(handler(request, ws, *args, **kwargs))
self.websocket_tasks.add(fut)
cancelled = False
try:
await fut
except Exception as e:
self.error_handler.log(request, e)
except (CancelledError, ConnectionClosed):
pass
cancelled = True
finally:
self.websocket_tasks.remove(fut)
await ws.close()
if cancelled:
ws.end_connection(1000)
else:
await ws.close()

# -------------------------------------------------------------------- #
# Testing
Expand Down
2 changes: 1 addition & 1 deletion sanic/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from sanic.models.asgi import ASGIReceive, ASGIScope, ASGISend, MockTransport
from sanic.request import Request
from sanic.server import ConnInfo
from sanic.websocket import WebSocketConnection
from sanic.server.websockets.connection import WebSocketConnection


class Lifespan:
Expand Down
6 changes: 0 additions & 6 deletions sanic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@
"REQUEST_MAX_SIZE": 100000000, # 100 megabytes
"REQUEST_TIMEOUT": 60, # 60 seconds
"RESPONSE_TIMEOUT": 60, # 60 seconds
"WEBSOCKET_MAX_QUEUE": 32,
"WEBSOCKET_MAX_SIZE": 2 ** 20, # 1 megabyte
"WEBSOCKET_PING_INTERVAL": 20,
"WEBSOCKET_PING_TIMEOUT": 20,
"WEBSOCKET_READ_LIMIT": 2 ** 16,
"WEBSOCKET_WRITE_LIMIT": 2 ** 16,
}
ahopkins marked this conversation as resolved.
Show resolved Hide resolved


Expand All @@ -62,12 +59,9 @@ class Config(dict):
REQUEST_MAX_SIZE: int
REQUEST_TIMEOUT: int
RESPONSE_TIMEOUT: int
WEBSOCKET_MAX_QUEUE: int
WEBSOCKET_MAX_SIZE: int
WEBSOCKET_PING_INTERVAL: int
WEBSOCKET_PING_TIMEOUT: int
WEBSOCKET_READ_LIMIT: int
WEBSOCKET_WRITE_LIMIT: int

def __init__(
self,
Expand Down
7 changes: 5 additions & 2 deletions sanic/mixins/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@ def decorator(handler):
"Expected either string or Iterable of host strings, "
"not %s" % host
)

if isinstance(subprotocols, (list, tuple, set)):
if isinstance(subprotocols, list):
# Ordered subprotocols, maintain order
subprotocols = tuple(subprotocols)
elif isinstance(subprotocols, set):
# subprotocol is unordered, keep it unordered
subprotocols = frozenset(subprotocols)

route = FutureRoute(
Expand Down
2 changes: 1 addition & 1 deletion sanic/models/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Any, Awaitable, Callable, MutableMapping, Optional, Union

from sanic.exceptions import InvalidUsage
from sanic.websocket import WebSocketConnection
from sanic.server.websockets.connection import WebSocketConnection


ASGIScope = MutableMapping[str, Any]
Expand Down
Loading