diff --git a/sanic/app.py b/sanic/app.py index 776fcbec..f6b1e84e 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -696,7 +696,9 @@ class Sanic: 'loop': loop, 'register_sys_signals': register_sys_signals, 'backlog': backlog, - 'has_log': has_log + 'has_log': has_log, + 'websocket_max_size': self.config.WEBSOCKET_MAX_SIZE, + 'websocket_max_queue': self.config.WEBSOCKET_MAX_QUEUE } # -------------------------------------------- # diff --git a/sanic/config.py b/sanic/config.py index 8ffdb6c4..e3563bc1 100644 --- a/sanic/config.py +++ b/sanic/config.py @@ -122,9 +122,11 @@ class Config(dict): ▌ ▐ ▀▀▄▄▄▀ ▀▀▄▄▀ """ - self.REQUEST_MAX_SIZE = 100000000 # 100 megababies + self.REQUEST_MAX_SIZE = 100000000 # 100 megabytes self.REQUEST_TIMEOUT = 60 # 60 seconds self.KEEP_ALIVE = keep_alive + self.WEBSOCKET_MAX_SIZE = 2 ** 20 # 1 megabytes + self.WEBSOCKET_MAX_QUEUE = 32 if load_env: self.load_environment_vars() diff --git a/sanic/server.py b/sanic/server.py index 36b2a7a2..f3106226 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -74,7 +74,8 @@ class HttpProtocol(asyncio.Protocol): def __init__(self, *, loop, request_handler, error_handler, signal=Signal(), connections=set(), request_timeout=60, request_max_size=None, request_class=None, has_log=True, - keep_alive=True, is_request_stream=False, router=None): + keep_alive=True, is_request_stream=False, router=None, + **kwargs): self.loop = loop self.transport = None self.request = None @@ -387,7 +388,8 @@ def serve(host, port, request_handler, error_handler, before_start=None, reuse_port=False, loop=None, protocol=HttpProtocol, backlog=100, register_sys_signals=True, run_async=False, connections=None, signal=Signal(), request_class=None, has_log=True, keep_alive=True, - is_request_stream=False, router=None): + is_request_stream=False, router=None, websocket_max_size=None, + websocket_max_queue=None): """Start asynchronous HTTP Server on an individual process. :param host: Address to host on @@ -442,6 +444,8 @@ def serve(host, port, request_handler, error_handler, before_start=None, keep_alive=keep_alive, is_request_stream=is_request_stream, router=router, + websocket_max_size=websocket_max_size, + websocket_max_queue=websocket_max_queue ) server_coroutine = loop.create_server( diff --git a/sanic/websocket.py b/sanic/websocket.py index a712eda8..94320a5e 100644 --- a/sanic/websocket.py +++ b/sanic/websocket.py @@ -6,9 +6,12 @@ from websockets import ConnectionClosed # noqa class WebSocketProtocol(HttpProtocol): - def __init__(self, *args, **kwargs): + def __init__(self, *args, websocket_max_size=None, + websocket_max_queue=None, **kwargs): super().__init__(*args, **kwargs) self.websocket = None + self.websocket_max_size = websocket_max_size + self.websocket_max_queue = websocket_max_queue def connection_timeout(self): # timeouts make no sense for websocket routes @@ -62,6 +65,9 @@ class WebSocketProtocol(HttpProtocol): request.transport.write(rv) # hook up the websocket protocol - self.websocket = WebSocketCommonProtocol() + self.websocket = WebSocketCommonProtocol( + max_size=self.websocket_max_size, + max_queue=self.websocket_max_queue + ) self.websocket.connection_made(request.transport) return self.websocket