Merge pull request #646 from r0fls/637

allow disabling keep alive
This commit is contained in:
Raphael Deem 2017-04-24 00:43:16 -07:00 committed by GitHub
commit b3814ca89a
4 changed files with 16 additions and 8 deletions

View File

@ -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 |

View File

@ -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

View File

@ -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):
for k, v in os.environ.items():
"""
Looks for any SANIC_ prefixed environment variables and applies
them to the configuration if present.
"""
for k, v in os.environ.items():
if k.startswith(SANIC_PREFIX):
_, config_key = k.split(SANIC_PREFIX, 1)
self[config_key] = v

View File

@ -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(