From 5ddb0488f23a266762731f0d0cf9d8749cb58190 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Sun, 16 Apr 2017 21:39:18 -0700 Subject: [PATCH 1/3] allow disabling keep alive --- sanic/app.py | 1 + sanic/config.py | 11 ++++++----- sanic/server.py | 11 ++++++++--- 3 files changed, 15 insertions(+), 8 deletions(-) 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( From 6f098b3d2134d742269d718a71321e341dc32089 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Sun, 16 Apr 2017 22:05:34 -0700 Subject: [PATCH 2/3] add no_keep_alive setting to docs --- docs/sanic/config.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/sanic/config.md b/docs/sanic/config.md index 3ed40fda..2bbce5d0 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) | + | NO_KEEP_ALIVE | False | Disable keep-alive | From 81b6d988ecc10936a2388528244e7b6276fe44e6 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Sun, 16 Apr 2017 22:43:49 -0700 Subject: [PATCH 3/3] NO_KEEP_ALIVE -> KEEP_ALIVE --- docs/sanic/config.md | 2 +- sanic/app.py | 2 +- sanic/config.py | 4 ++-- sanic/server.py | 10 +++++----- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/sanic/config.md b/docs/sanic/config.md index 2bbce5d0..a93d2cf1 100644 --- a/docs/sanic/config.md +++ b/docs/sanic/config.md @@ -83,4 +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) | - | NO_KEEP_ALIVE | False | Disable keep-alive | + | KEEP_ALIVE | True | Disables keep-alive when False | diff --git a/sanic/app.py b/sanic/app.py index b126fc5d..1cb6554f 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -679,7 +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, + '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 ca08d8b0..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, no_keep_alive=False): + def __init__(self, defaults=None, load_env=True, keep_alive=True): super().__init__(defaults or {}) self.LOGO = """ ▄▄▄▄▄ @@ -31,7 +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 + self.KEEP_ALIVE = keep_alive if load_env: self.load_environment_vars() diff --git a/sanic/server.py b/sanic/server.py index 9edda142..33763bbb 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -71,7 +71,7 @@ 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, - no_keep_alive=False): + keep_alive=True): self.loop = loop self.transport = None self.request = None @@ -89,11 +89,11 @@ 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 + self._keep_alive = keep_alive @property def keep_alive(self): - return (not self._no_keep_alive + return (self._keep_alive and not self.signal.stopped and self.parser.should_keep_alive()) @@ -326,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, no_keep_alive=False): + signal=Signal(), request_class=None, keep_alive=True): """Start asynchronous HTTP Server on an individual process. :param host: Address to host on @@ -374,7 +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, + keep_alive=keep_alive, ) server_coroutine = loop.create_server(