]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-101336: Add keep_alive keyword arg for asyncio create_server() (#112485)
authorbeavailable <beavailable@proton.me>
Wed, 13 Dec 2023 03:23:29 +0000 (11:23 +0800)
committerGitHub <noreply@github.com>
Wed, 13 Dec 2023 03:23:29 +0000 (03:23 +0000)
Doc/library/asyncio-eventloop.rst
Lib/asyncio/base_events.py
Lib/asyncio/events.py
Misc/NEWS.d/next/Library/2023-11-28-02-39-30.gh-issue-101336.ya433z.rst [new file with mode: 0644]

index ea1d146f06cf2bdd073b71d3b43e3c6bc2a831ec..828e506a72c9374a7e4ddd6b7ea33d526dc8ed57 100644 (file)
@@ -671,6 +671,7 @@ Creating network servers
                         flags=socket.AI_PASSIVE, \
                         sock=None, backlog=100, ssl=None, \
                         reuse_address=None, reuse_port=None, \
+                        keep_alive=None, \
                         ssl_handshake_timeout=None, \
                         ssl_shutdown_timeout=None, \
                         start_serving=True)
@@ -735,6 +736,13 @@ Creating network servers
      set this flag when being created. This option is not supported on
      Windows.
 
+   * *keep_alive* set to ``True`` keeps connections active by enabling the
+     periodic transmission of messages.
+
+   .. versionchanged:: 3.13
+
+      Added the *keep_alive* parameter.
+
    * *ssl_handshake_timeout* is (for a TLS server) the time in seconds to wait
      for the TLS handshake to complete before aborting the connection.
      ``60.0`` seconds if ``None`` (default).
index 416c732298d9a943ba262cba27a2d59afac221a5..a8870b636d1df50fd0fdeee0c1d09b68cad84e86 100644 (file)
@@ -1496,6 +1496,7 @@ class BaseEventLoop(events.AbstractEventLoop):
             ssl=None,
             reuse_address=None,
             reuse_port=None,
+            keep_alive=None,
             ssl_handshake_timeout=None,
             ssl_shutdown_timeout=None,
             start_serving=True):
@@ -1569,6 +1570,9 @@ class BaseEventLoop(events.AbstractEventLoop):
                             socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
                     if reuse_port:
                         _set_reuseport(sock)
+                    if keep_alive:
+                        sock.setsockopt(
+                            socket.SOL_SOCKET, socket.SO_KEEPALIVE, True)
                     # Disable IPv4/IPv6 dual stack support (enabled by
                     # default on Linux) which makes a single socket
                     # listen on both address families.
index 0ccf85105e6673f377f4e466d1aa75d63d44b9b8..ebc3836bdc0c4d32fd71f1c1879c0ecfe2910d4d 100644 (file)
@@ -316,6 +316,7 @@ class AbstractEventLoop:
             *, family=socket.AF_UNSPEC,
             flags=socket.AI_PASSIVE, sock=None, backlog=100,
             ssl=None, reuse_address=None, reuse_port=None,
+            keep_alive=None,
             ssl_handshake_timeout=None,
             ssl_shutdown_timeout=None,
             start_serving=True):
@@ -354,6 +355,9 @@ class AbstractEventLoop:
         they all set this flag when being created. This option is not
         supported on Windows.
 
+        keep_alive set to True keeps connections active by enabling the
+        periodic transmission of messages.
+
         ssl_handshake_timeout is the time in seconds that an SSL server
         will wait for completion of the SSL handshake before aborting the
         connection. Default is 60s.
diff --git a/Misc/NEWS.d/next/Library/2023-11-28-02-39-30.gh-issue-101336.ya433z.rst b/Misc/NEWS.d/next/Library/2023-11-28-02-39-30.gh-issue-101336.ya433z.rst
new file mode 100644 (file)
index 0000000..c222feb
--- /dev/null
@@ -0,0 +1 @@
+Add ``keep_alive`` keyword parameter for :meth:`AbstractEventLoop.create_server` and :meth:`BaseEventLoop.create_server`.