Skip to content

Commit

Permalink
Add keepalive_timeout parameter to web.run_app
Browse files Browse the repository at this point in the history
PR #5095 by @Teyras

Co-authored-by: Sviatoslav Sydorenko <webknjaz@redhat.com>
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
  • Loading branch information
3 people committed Oct 29, 2020
1 parent 8a20e9a commit e88dc8a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
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 @@ -136,6 +136,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: 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 @@ -465,6 +467,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: Callable[..., None] = print,
backlog: int = 128,
Expand Down Expand Up @@ -494,6 +497,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
7 changes: 7 additions & 0 deletions docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2825,6 +2825,7 @@ Utilities

.. function:: run_app(app, *, host=None, port=None, path=None, \
sock=None, shutdown_timeout=60.0, \
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, \
Expand Down Expand Up @@ -2879,6 +2880,12 @@ Utilities
timeout but closes a server in a few
milliseconds.

:param float keepalive_timeout: a delay before a TCP connection is
closed after a HTTP request. The delay
allows for reuse of a TCP connection.

.. 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 @@ -828,6 +829,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)

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

0 comments on commit e88dc8a

Please sign in to comment.