diff --git a/docs/sanic/config.md b/docs/sanic/config.md index 3ed40fda..a93d2cf1 100644 --- a/docs/sanic/config.md +++ b/docs/sanic/config.md @@ -83,3 +83,4 @@ Out of the box there are just a few predefined values which can be overwritten w | ----------------- | --------- | --------------------------------- | | REQUEST_MAX_SIZE | 100000000 | How big a request may be (bytes) | | REQUEST_TIMEOUT | 60 | How long a request can take (sec) | + | KEEP_ALIVE | True | Disables keep-alive when False | diff --git a/sanic/app.py b/sanic/app.py index d4dc4e3d..5bc7b468 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -680,6 +680,7 @@ class Sanic: 'error_handler': self.error_handler, 'request_timeout': self.config.REQUEST_TIMEOUT, 'request_max_size': self.config.REQUEST_MAX_SIZE, + 'keep_alive': self.config.KEEP_ALIVE, 'loop': loop, 'register_sys_signals': register_sys_signals, 'backlog': backlog diff --git a/sanic/config.py b/sanic/config.py index 406c44e6..4c128b4b 100644 --- a/sanic/config.py +++ b/sanic/config.py @@ -6,7 +6,7 @@ SANIC_PREFIX = 'SANIC_' class Config(dict): - def __init__(self, defaults=None, load_env=True): + def __init__(self, defaults=None, load_env=True, keep_alive=True): super().__init__(defaults or {}) self.LOGO = """ ▄▄▄▄▄ @@ -31,6 +31,7 @@ class Config(dict): """ self.REQUEST_MAX_SIZE = 100000000 # 100 megababies self.REQUEST_TIMEOUT = 60 # 60 seconds + self.KEEP_ALIVE = keep_alive if load_env: self.load_environment_vars() @@ -98,11 +99,11 @@ class Config(dict): self[key] = getattr(obj, key) def load_environment_vars(self): + """ + Looks for any SANIC_ prefixed environment variables and applies + them to the configuration if present. + """ for k, v in os.environ.items(): - """ - Looks for any SANIC_ prefixed environment variables and applies - them to the configuration if present. - """ if k.startswith(SANIC_PREFIX): _, config_key = k.split(SANIC_PREFIX, 1) self[config_key] = v diff --git a/sanic/server.py b/sanic/server.py index 40c3ee69..33763bbb 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -70,7 +70,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): + request_max_size=None, request_class=None, + keep_alive=True): self.loop = loop self.transport = None self.request = None @@ -88,10 +89,13 @@ class HttpProtocol(asyncio.Protocol): self._timeout_handler = None self._last_request_time = None self._request_handler_task = None + self._keep_alive = keep_alive @property def keep_alive(self): - return self.parser.should_keep_alive() and not self.signal.stopped + return (self._keep_alive + and not self.signal.stopped + and self.parser.should_keep_alive()) # -------------------------------------------- # # Connection @@ -322,7 +326,7 @@ def serve(host, port, request_handler, error_handler, before_start=None, request_timeout=60, ssl=None, sock=None, request_max_size=None, reuse_port=False, loop=None, protocol=HttpProtocol, backlog=100, register_sys_signals=True, run_async=False, connections=None, - signal=Signal(), request_class=None): + signal=Signal(), request_class=None, keep_alive=True): """Start asynchronous HTTP Server on an individual process. :param host: Address to host on @@ -370,6 +374,7 @@ def serve(host, port, request_handler, error_handler, before_start=None, request_timeout=request_timeout, request_max_size=request_max_size, request_class=request_class, + keep_alive=keep_alive, ) server_coroutine = loop.create_server(