Remove server config args that can be read directly from app. (#1807)

* Remove server config args that can be read directly from app.

* Linter

Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
This commit is contained in:
L. Kärkkäinen 2020-04-09 08:10:58 +03:00 committed by GitHub
parent 9a39aff803
commit 710024125e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 125 deletions

View File

@ -1368,33 +1368,15 @@ class Sanic:
server_settings = { server_settings = {
"protocol": protocol, "protocol": protocol,
"request_class": self.request_class,
"is_request_stream": self.is_request_stream,
"router": self.router,
"host": host, "host": host,
"port": port, "port": port,
"sock": sock, "sock": sock,
"ssl": ssl, "ssl": ssl,
"app": self, "app": self,
"signal": Signal(), "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, "loop": loop,
"register_sys_signals": register_sys_signals, "register_sys_signals": register_sys_signals,
"backlog": backlog, "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,
} }
# -------------------------------------------- # # -------------------------------------------- #

View File

@ -71,7 +71,6 @@ class HttpProtocol(asyncio.Protocol):
"request_buffer_queue_size", "request_buffer_queue_size",
"request_class", "request_class",
"is_request_stream", "is_request_stream",
"router",
"error_handler", "error_handler",
# enable or disable access log purpose # enable or disable access log purpose
"access_log", "access_log",
@ -89,7 +88,6 @@ class HttpProtocol(asyncio.Protocol):
"_keep_alive", "_keep_alive",
"_header_fragment", "_header_fragment",
"state", "state",
"_debug",
"_body_chunks", "_body_chunks",
) )
@ -98,49 +96,36 @@ class HttpProtocol(asyncio.Protocol):
*, *,
loop, loop,
app, app,
request_handler,
error_handler,
signal=Signal(), signal=Signal(),
connections=None, 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, state=None,
debug=False,
**kwargs, **kwargs,
): ):
asyncio.set_event_loop(loop)
self.loop = loop self.loop = loop
deprecated_loop = self.loop if sys.version_info < (3, 7) else None
self.app = app self.app = app
self.transport = None self.transport = None
self.request = None self.request = None
self.parser = None self.parser = None
self.url = None self.url = None
self.headers = None self.headers = None
self.router = router
self.signal = signal 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.connections = connections if connections is not None else set()
self.request_handler = request_handler self.request_handler = self.app.handle_request
self.error_handler = error_handler self.error_handler = self.app.error_handler
self.request_timeout = request_timeout self.request_timeout = self.app.config.REQUEST_TIMEOUT
self.request_buffer_queue_size = request_buffer_queue_size self.request_buffer_queue_size = (
self.response_timeout = response_timeout self.app.config.REQUEST_BUFFER_QUEUE_SIZE
self.keep_alive_timeout = keep_alive_timeout )
self.request_max_size = request_max_size self.response_timeout = self.app.config.RESPONSE_TIMEOUT
self.request_class = request_class or Request self.keep_alive_timeout = self.app.config.KEEP_ALIVE_TIMEOUT
self.is_request_stream = is_request_stream 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 self._is_stream_handler = False
if sys.version_info.minor >= 8: self._not_paused = asyncio.Event(loop=deprecated_loop)
self._not_paused = asyncio.Event()
else:
self._not_paused = asyncio.Event(loop=loop)
self._total_request_size = 0 self._total_request_size = 0
self._request_timeout_handler = None self._request_timeout_handler = None
self._response_timeout_handler = None self._response_timeout_handler = None
@ -149,12 +134,11 @@ class HttpProtocol(asyncio.Protocol):
self._last_response_time = None self._last_response_time = None
self._request_handler_task = None self._request_handler_task = None
self._request_stream_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._header_fragment = b""
self.state = state if state else {} self.state = state if state else {}
if "requests_count" not in self.state: if "requests_count" not in self.state:
self.state["requests_count"] = 0 self.state["requests_count"] = 0
self._debug = debug
self._not_paused.set() self._not_paused.set()
self._body_chunks = deque() self._body_chunks = deque()
@ -282,7 +266,7 @@ class HttpProtocol(asyncio.Protocol):
self.parser.feed_data(data) self.parser.feed_data(data)
except HttpParserError: except HttpParserError:
message = "Bad Request" message = "Bad Request"
if self._debug: if self.app.debug:
message += "\n" + traceback.format_exc() message += "\n" + traceback.format_exc()
self.write_error(InvalidUsage(message)) self.write_error(InvalidUsage(message))
@ -330,7 +314,7 @@ class HttpProtocol(asyncio.Protocol):
self.expect_handler() self.expect_handler()
if self.is_request_stream: 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 self.request
) )
if self._is_stream_handler: if self._is_stream_handler:
@ -488,7 +472,7 @@ class HttpProtocol(asyncio.Protocol):
) )
self.write_error(ServerError("Invalid response type")) self.write_error(ServerError("Invalid response type"))
except RuntimeError: except RuntimeError:
if self._debug: if self.app.debug:
logger.error( logger.error(
"Connection lost before response written @ %s", "Connection lost before response written @ %s",
self.request.ip, self.request.ip,
@ -539,7 +523,7 @@ class HttpProtocol(asyncio.Protocol):
) )
self.write_error(ServerError("Invalid response type")) self.write_error(ServerError("Invalid response type"))
except RuntimeError: except RuntimeError:
if self._debug: if self.app.debug:
logger.error( logger.error(
"Connection lost before response written @ %s", "Connection lost before response written @ %s",
self.request.ip, self.request.ip,
@ -570,7 +554,7 @@ class HttpProtocol(asyncio.Protocol):
version = self.request.version if self.request else "1.1" version = self.request.version if self.request else "1.1"
self.transport.write(response.output(version)) self.transport.write(response.output(version))
except RuntimeError: except RuntimeError:
if self._debug: if self.app.debug:
logger.error( logger.error(
"Connection lost before error written @ %s", "Connection lost before error written @ %s",
self.request.ip if self.request else "Unknown", 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 :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() self.transport.close()
return True return True
return False return False
@ -760,20 +744,12 @@ def serve(
host, host,
port, port,
app, app,
request_handler,
error_handler,
before_start=None, before_start=None,
after_start=None, after_start=None,
before_stop=None, before_stop=None,
after_stop=None, after_stop=None,
debug=False,
request_timeout=60,
response_timeout=60,
keep_alive_timeout=5,
ssl=None, ssl=None,
sock=None, sock=None,
request_max_size=None,
request_buffer_queue_size=100,
reuse_port=False, reuse_port=False,
loop=None, loop=None,
protocol=HttpProtocol, protocol=HttpProtocol,
@ -783,25 +759,13 @@ def serve(
run_async=False, run_async=False,
connections=None, connections=None,
signal=Signal(), 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, state=None,
graceful_shutdown_timeout=15.0,
asyncio_server_kwargs=None, asyncio_server_kwargs=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
:param port: Port 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 :param before_start: function to be executed before the server starts
listening. Takes arguments `app` instance and `loop` listening. Takes arguments `app` instance and `loop`
:param after_start: function to be executed after the server starts :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 :param after_stop: function to be executed when a stop signal is
received after it is respected. Takes arguments received after it is respected. Takes arguments
`app` instance and `loop` `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 ssl: SSLContext
:param sock: Socket for the server to accept connections from :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 reuse_port: `True` for multiple workers
:param loop: asyncio compatible event loop :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, :param run_async: bool: Do not create a new event loop for the server,
and return an AsyncServer object rather than running it 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 :param asyncio_server_kwargs: key-value args for asyncio/uvloop
create_server method create_server method
:return: Nothing :return: Nothing
@ -850,8 +791,8 @@ def serve(
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop) asyncio.set_event_loop(loop)
if debug: if app.debug:
loop.set_debug(debug) loop.set_debug(app.debug)
app.asgi = False app.asgi = False
@ -862,24 +803,7 @@ def serve(
connections=connections, connections=connections,
signal=signal, signal=signal,
app=app, 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, state=state,
debug=debug,
) )
asyncio_server_kwargs = ( asyncio_server_kwargs = (
asyncio_server_kwargs if asyncio_server_kwargs else {} asyncio_server_kwargs if asyncio_server_kwargs else {}
@ -897,12 +821,12 @@ def serve(
if run_async: if run_async:
return AsyncioServer( return AsyncioServer(
loop, loop=loop,
server_coroutine, serve_coro=server_coroutine,
connections, connections=connections,
after_start, after_start=after_start,
before_stop, before_stop=before_stop,
after_stop, after_stop=after_stop,
) )
trigger_events(before_start, loop) trigger_events(before_start, loop)
@ -949,8 +873,9 @@ def serve(
# We should provide graceful_shutdown_timeout, # We should provide graceful_shutdown_timeout,
# instead of letting connection hangs forever. # instead of letting connection hangs forever.
# Let's roughly calcucate time. # Let's roughly calcucate time.
graceful = app.config.GRACEFUL_SHUTDOWN_TIMEOUT
start_shutdown = 0 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)) loop.run_until_complete(asyncio.sleep(0.1))
start_shutdown = start_shutdown + 0.1 start_shutdown = start_shutdown + 0.1