Add websocket max_size and max_queue configuration

This commit is contained in:
messense 2017-05-26 11:11:26 +08:00
parent 23cb39b557
commit 0024edbbb9
No known key found for this signature in database
GPG Key ID: BB41A8A2C716CCA9
4 changed files with 20 additions and 6 deletions

View File

@ -696,7 +696,9 @@ class Sanic:
'loop': loop, 'loop': loop,
'register_sys_signals': register_sys_signals, 'register_sys_signals': register_sys_signals,
'backlog': backlog, '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
} }
# -------------------------------------------- # # -------------------------------------------- #

View File

@ -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.REQUEST_TIMEOUT = 60 # 60 seconds
self.KEEP_ALIVE = keep_alive self.KEEP_ALIVE = keep_alive
self.WEBSOCKET_MAX_SIZE = 2 ** 20 # 1 megabytes
self.WEBSOCKET_MAX_QUEUE = 32
if load_env: if load_env:
self.load_environment_vars() self.load_environment_vars()

View File

@ -74,7 +74,8 @@ class HttpProtocol(asyncio.Protocol):
def __init__(self, *, loop, request_handler, error_handler, def __init__(self, *, loop, request_handler, error_handler,
signal=Signal(), connections=set(), request_timeout=60, signal=Signal(), connections=set(), request_timeout=60,
request_max_size=None, request_class=None, has_log=True, 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.loop = loop
self.transport = None self.transport = None
self.request = 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, reuse_port=False, loop=None, protocol=HttpProtocol, backlog=100,
register_sys_signals=True, run_async=False, connections=None, register_sys_signals=True, run_async=False, connections=None,
signal=Signal(), request_class=None, has_log=True, keep_alive=True, 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. """Start asynchronous HTTP Server on an individual process.
:param host: Address to host on :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, keep_alive=keep_alive,
is_request_stream=is_request_stream, is_request_stream=is_request_stream,
router=router, router=router,
websocket_max_size=websocket_max_size,
websocket_max_queue=websocket_max_queue
) )
server_coroutine = loop.create_server( server_coroutine = loop.create_server(

View File

@ -6,9 +6,12 @@ from websockets import ConnectionClosed # noqa
class WebSocketProtocol(HttpProtocol): 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) super().__init__(*args, **kwargs)
self.websocket = None self.websocket = None
self.websocket_max_size = websocket_max_size
self.websocket_max_queue = websocket_max_queue
def connection_timeout(self): def connection_timeout(self):
# timeouts make no sense for websocket routes # timeouts make no sense for websocket routes
@ -62,6 +65,9 @@ class WebSocketProtocol(HttpProtocol):
request.transport.write(rv) request.transport.write(rv)
# hook up the websocket protocol # 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) self.websocket.connection_made(request.transport)
return self.websocket return self.websocket