Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keepalive_timeout parameter to web.run_app #5095

Merged
merged 16 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/5094.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add keepalive_timeout parameter to web.run_app.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ Jaesung Lee
Jake Davis
Jakob Ackermann
Jakub Wilk
Jan Buchar
Jashandeep Sohi
Jens Steinhauser
Jeonghun Lee
Expand Down
4 changes: 4 additions & 0 deletions aiohttp/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ async def _run_app(
path: Optional[str] = None,
sock: Optional[socket.socket] = None,
shutdown_timeout: float = 60.0,
keepalive_timeout: float = 75.0,
ssl_context: Optional[SSLContext] = None,
print: Optional[Callable[..., None]] = print,
backlog: int = 128,
Expand All @@ -314,6 +315,7 @@ async def _run_app(
access_log_class=access_log_class,
access_log_format=access_log_format,
access_log=access_log,
keepalive_timeout=keepalive_timeout,
)

await runner.setup()
Expand Down Expand Up @@ -466,6 +468,7 @@ def run_app(
path: Optional[str] = None,
sock: Optional[socket.socket] = None,
shutdown_timeout: float = 60.0,
keepalive_timeout: float = 75.0,
ssl_context: Optional[SSLContext] = None,
print: Optional[Callable[..., None]] = print,
backlog: int = 128,
Expand Down Expand Up @@ -496,6 +499,7 @@ def run_app(
path=path,
sock=sock,
shutdown_timeout=shutdown_timeout,
keepalive_timeout=keepalive_timeout,
ssl_context=ssl_context,
print=print,
backlog=backlog,
Expand Down
9 changes: 8 additions & 1 deletion docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2737,7 +2737,8 @@ Utilities

.. function:: run_app(app, *, debug=False, host=None, port=None, \
path=None, sock=None, shutdown_timeout=60.0, \
ssl_context=None, print=print, backlog=128, \
keepalive_timeout=75.0, ssl_context=None, \
print=print, backlog=128, \
access_log_class=aiohttp.helpers.AccessLogger, \
access_log_format=aiohttp.helpers.AccessLogger.LOG_FORMAT, \
access_log=aiohttp.log.access_logger, \
Expand Down Expand Up @@ -2793,6 +2794,12 @@ Utilities
timeout but closes a server in a few
milliseconds.

:param float keepalive_timeout: a delay before a TCP connection is
asvetlov marked this conversation as resolved.
Show resolved Hide resolved
closed after a HTTP request. The delay
allows for reuse of a TCP connection.

janbuchar marked this conversation as resolved.
Show resolved Hide resolved
.. versionadded:: 3.8

:param ssl_context: :class:`ssl.SSLContext` for HTTPS server,
``None`` for HTTP connection.

Expand Down
14 changes: 14 additions & 0 deletions tests/test_run_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from aiohttp import web
from aiohttp.helpers import PY_37
from aiohttp.test_utils import make_mocked_coro
from aiohttp.web_runner import BaseRunner

# Test for features of OS' socket support
_has_unix_domain_socks = hasattr(socket, "AF_UNIX")
Expand Down Expand Up @@ -824,6 +825,19 @@ async def on_startup(app):
exc_handler.assert_called_with(patched_loop, msg)


def test_run_app_keepalive_timeout(patched_loop, mocker, monkeypatch):
new_timeout = 1234
base_runner_init_orig = BaseRunner.__init__

def base_runner_init_spy(self, *args, **kwargs):
assert kwargs["keepalive_timeout"] == new_timeout
base_runner_init_orig(self, *args, **kwargs)
webknjaz marked this conversation as resolved.
Show resolved Hide resolved

app = web.Application()
monkeypatch.setattr(BaseRunner, "__init__", base_runner_init_spy)
web.run_app(app, keepalive_timeout=new_timeout, print=stopper(patched_loop))


@pytest.mark.skipif(not PY_37, reason="contextvars support is required")
def test_run_app_context_vars(patched_loop):
from contextvars import ContextVar
Expand Down