From 710024125e9fbe820b0d993ad53874920507a702 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=2E=20K=C3=A4rkk=C3=A4inen?= <98187+Tronic@users.noreply.github.com> Date: Thu, 9 Apr 2020 08:10:58 +0300 Subject: [PATCH] Remove server config args that can be read directly from app. (#1807) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Remove server config args that can be read directly from app. * Linter Co-authored-by: L. Kärkkäinen --- sanic/app.py | 18 ------- sanic/server.py | 139 +++++++++++------------------------------------- 2 files changed, 32 insertions(+), 125 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index d17328b6..6d7f2daf 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -1368,33 +1368,15 @@ class Sanic: server_settings = { "protocol": protocol, - "request_class": self.request_class, - "is_request_stream": self.is_request_stream, - "router": self.router, "host": host, "port": port, "sock": sock, "ssl": ssl, "app": self, "signal": Signal(), - "debug": debug, - "request_handler": self.handle_request, - "error_handler": self.error_handler, - "request_timeout": self.config.REQUEST_TIMEOUT, - "response_timeout": self.config.RESPONSE_TIMEOUT, - "keep_alive_timeout": self.config.KEEP_ALIVE_TIMEOUT, - "request_max_size": self.config.REQUEST_MAX_SIZE, - "request_buffer_queue_size": self.config.REQUEST_BUFFER_QUEUE_SIZE, - "keep_alive": self.config.KEEP_ALIVE, "loop": loop, "register_sys_signals": register_sys_signals, "backlog": backlog, - "access_log": self.config.ACCESS_LOG, - "websocket_max_size": self.config.WEBSOCKET_MAX_SIZE, - "websocket_max_queue": self.config.WEBSOCKET_MAX_QUEUE, - "websocket_read_limit": self.config.WEBSOCKET_READ_LIMIT, - "websocket_write_limit": self.config.WEBSOCKET_WRITE_LIMIT, - "graceful_shutdown_timeout": self.config.GRACEFUL_SHUTDOWN_TIMEOUT, } # -------------------------------------------- # diff --git a/sanic/server.py b/sanic/server.py index 5128b30a..6fff747f 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -71,7 +71,6 @@ class HttpProtocol(asyncio.Protocol): "request_buffer_queue_size", "request_class", "is_request_stream", - "router", "error_handler", # enable or disable access log purpose "access_log", @@ -89,7 +88,6 @@ class HttpProtocol(asyncio.Protocol): "_keep_alive", "_header_fragment", "state", - "_debug", "_body_chunks", ) @@ -98,49 +96,36 @@ class HttpProtocol(asyncio.Protocol): *, loop, app, - request_handler, - error_handler, signal=Signal(), connections=None, - request_timeout=60, - response_timeout=60, - keep_alive_timeout=5, - request_max_size=None, - request_buffer_queue_size=100, - request_class=None, - access_log=True, - keep_alive=True, - is_request_stream=False, - router=None, state=None, - debug=False, **kwargs, ): + asyncio.set_event_loop(loop) self.loop = loop + deprecated_loop = self.loop if sys.version_info < (3, 7) else None self.app = app self.transport = None self.request = None self.parser = None self.url = None self.headers = None - self.router = router self.signal = signal - self.access_log = access_log + self.access_log = self.app.config.ACCESS_LOG self.connections = connections if connections is not None else set() - self.request_handler = request_handler - self.error_handler = error_handler - self.request_timeout = request_timeout - self.request_buffer_queue_size = request_buffer_queue_size - self.response_timeout = response_timeout - self.keep_alive_timeout = keep_alive_timeout - self.request_max_size = request_max_size - self.request_class = request_class or Request - self.is_request_stream = is_request_stream + self.request_handler = self.app.handle_request + self.error_handler = self.app.error_handler + self.request_timeout = self.app.config.REQUEST_TIMEOUT + self.request_buffer_queue_size = ( + self.app.config.REQUEST_BUFFER_QUEUE_SIZE + ) + self.response_timeout = self.app.config.RESPONSE_TIMEOUT + self.keep_alive_timeout = self.app.config.KEEP_ALIVE_TIMEOUT + self.request_max_size = self.app.config.REQUEST_MAX_SIZE + self.request_class = self.app.request_class or Request + self.is_request_stream = self.app.is_request_stream self._is_stream_handler = False - if sys.version_info.minor >= 8: - self._not_paused = asyncio.Event() - else: - self._not_paused = asyncio.Event(loop=loop) + self._not_paused = asyncio.Event(loop=deprecated_loop) self._total_request_size = 0 self._request_timeout_handler = None self._response_timeout_handler = None @@ -149,12 +134,11 @@ class HttpProtocol(asyncio.Protocol): self._last_response_time = None self._request_handler_task = None self._request_stream_task = None - self._keep_alive = keep_alive + self._keep_alive = self.app.config.KEEP_ALIVE self._header_fragment = b"" self.state = state if state else {} if "requests_count" not in self.state: self.state["requests_count"] = 0 - self._debug = debug self._not_paused.set() self._body_chunks = deque() @@ -282,7 +266,7 @@ class HttpProtocol(asyncio.Protocol): self.parser.feed_data(data) except HttpParserError: message = "Bad Request" - if self._debug: + if self.app.debug: message += "\n" + traceback.format_exc() self.write_error(InvalidUsage(message)) @@ -330,7 +314,7 @@ class HttpProtocol(asyncio.Protocol): self.expect_handler() if self.is_request_stream: - self._is_stream_handler = self.router.is_stream_handler( + self._is_stream_handler = self.app.router.is_stream_handler( self.request ) if self._is_stream_handler: @@ -488,7 +472,7 @@ class HttpProtocol(asyncio.Protocol): ) self.write_error(ServerError("Invalid response type")) except RuntimeError: - if self._debug: + if self.app.debug: logger.error( "Connection lost before response written @ %s", self.request.ip, @@ -539,7 +523,7 @@ class HttpProtocol(asyncio.Protocol): ) self.write_error(ServerError("Invalid response type")) except RuntimeError: - if self._debug: + if self.app.debug: logger.error( "Connection lost before response written @ %s", self.request.ip, @@ -570,7 +554,7 @@ class HttpProtocol(asyncio.Protocol): version = self.request.version if self.request else "1.1" self.transport.write(response.output(version)) except RuntimeError: - if self._debug: + if self.app.debug: logger.error( "Connection lost before error written @ %s", self.request.ip if self.request else "Unknown", @@ -638,7 +622,7 @@ class HttpProtocol(asyncio.Protocol): :return: boolean - True if closed, false if staying open """ - if not self.parser: + if not self.parser and self.transport is not None: self.transport.close() return True return False @@ -760,20 +744,12 @@ def serve( host, port, app, - request_handler, - error_handler, before_start=None, after_start=None, before_stop=None, after_stop=None, - debug=False, - request_timeout=60, - response_timeout=60, - keep_alive_timeout=5, ssl=None, sock=None, - request_max_size=None, - request_buffer_queue_size=100, reuse_port=False, loop=None, protocol=HttpProtocol, @@ -783,25 +759,13 @@ def serve( run_async=False, connections=None, signal=Signal(), - request_class=None, - access_log=True, - keep_alive=True, - is_request_stream=False, - router=None, - websocket_max_size=None, - websocket_max_queue=None, - websocket_read_limit=2 ** 16, - websocket_write_limit=2 ** 16, state=None, - graceful_shutdown_timeout=15.0, asyncio_server_kwargs=None, ): """Start asynchronous HTTP Server on an individual process. :param host: Address to host on :param port: Port to host on - :param request_handler: Sanic request handler with middleware - :param error_handler: Sanic error handler with middleware :param before_start: function to be executed before the server starts listening. Takes arguments `app` instance and `loop` :param after_start: function to be executed after the server starts @@ -812,35 +776,12 @@ def serve( :param after_stop: function to be executed when a stop signal is received after it is respected. Takes arguments `app` instance and `loop` - :param debug: enables debug output (slows server) - :param request_timeout: time in seconds - :param response_timeout: time in seconds - :param keep_alive_timeout: time in seconds :param ssl: SSLContext :param sock: Socket for the server to accept connections from - :param request_max_size: size in bytes, `None` for no limit :param reuse_port: `True` for multiple workers :param loop: asyncio compatible event loop - :param protocol: subclass of asyncio protocol class :param run_async: bool: Do not create a new event loop for the server, and return an AsyncServer object rather than running it - :param request_class: Request class to use - :param access_log: disable/enable access log - :param websocket_max_size: enforces the maximum size for - incoming messages in bytes. - :param websocket_max_queue: sets the maximum length of the queue - that holds incoming messages. - :param websocket_read_limit: sets the high-water limit of the buffer for - incoming bytes, the low-water limit is half - the high-water limit. - :param websocket_write_limit: sets the high-water limit of the buffer for - outgoing bytes, the low-water limit is a - quarter of the high-water limit. - :param is_request_stream: disable/enable Request.stream - :param request_buffer_queue_size: streaming request buffer queue size - :param router: Router object - :param graceful_shutdown_timeout: How long take to Force close non-idle - connection :param asyncio_server_kwargs: key-value args for asyncio/uvloop create_server method :return: Nothing @@ -850,8 +791,8 @@ def serve( loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) - if debug: - loop.set_debug(debug) + if app.debug: + loop.set_debug(app.debug) app.asgi = False @@ -862,24 +803,7 @@ def serve( connections=connections, signal=signal, app=app, - request_handler=request_handler, - error_handler=error_handler, - request_timeout=request_timeout, - response_timeout=response_timeout, - keep_alive_timeout=keep_alive_timeout, - request_max_size=request_max_size, - request_buffer_queue_size=request_buffer_queue_size, - request_class=request_class, - access_log=access_log, - keep_alive=keep_alive, - is_request_stream=is_request_stream, - router=router, - websocket_max_size=websocket_max_size, - websocket_max_queue=websocket_max_queue, - websocket_read_limit=websocket_read_limit, - websocket_write_limit=websocket_write_limit, state=state, - debug=debug, ) asyncio_server_kwargs = ( asyncio_server_kwargs if asyncio_server_kwargs else {} @@ -897,12 +821,12 @@ def serve( if run_async: return AsyncioServer( - loop, - server_coroutine, - connections, - after_start, - before_stop, - after_stop, + loop=loop, + serve_coro=server_coroutine, + connections=connections, + after_start=after_start, + before_stop=before_stop, + after_stop=after_stop, ) trigger_events(before_start, loop) @@ -949,8 +873,9 @@ def serve( # We should provide graceful_shutdown_timeout, # instead of letting connection hangs forever. # Let's roughly calcucate time. + graceful = app.config.GRACEFUL_SHUTDOWN_TIMEOUT start_shutdown = 0 - while connections and (start_shutdown < graceful_shutdown_timeout): + while connections and (start_shutdown < graceful): loop.run_until_complete(asyncio.sleep(0.1)) start_shutdown = start_shutdown + 0.1