diff --git a/sanic/app.py b/sanic/app.py index 0ff98423..b126fc5d 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -679,6 +679,7 @@ class Sanic: 'error_handler': self.error_handler, 'request_timeout': self.config.REQUEST_TIMEOUT, 'request_max_size': self.config.REQUEST_MAX_SIZE, + 'no_keep_alive': self.config.NO_KEEP_ALIVE, 'loop': loop, 'register_sys_signals': register_sys_signals, 'backlog': backlog diff --git a/sanic/config.py b/sanic/config.py index 406c44e6..ca08d8b0 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, no_keep_alive=False): 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.NO_KEEP_ALIVE = no_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..9edda142 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, + no_keep_alive=False): 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._no_keep_alive = no_keep_alive @property def keep_alive(self): - return self.parser.should_keep_alive() and not self.signal.stopped + return (not self._no_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, no_keep_alive=False): """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, + no_keep_alive=no_keep_alive, ) server_coroutine = loop.create_server(