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

TCP Keep Alive doesn't seem to be enabled by default #6354

Closed
ltshb opened this issue Feb 15, 2023 · 1 comment
Closed

TCP Keep Alive doesn't seem to be enabled by default #6354

ltshb opened this issue Feb 15, 2023 · 1 comment

Comments

@ltshb
Copy link

ltshb commented Feb 15, 2023

In the documentation, it stated the TCP Keep Alive are enabled by default when using requests, but looking at the code it doesn't seem to be the case.

Expected Result

TCP Keep Alive be enable by default.

Actual Result

I could not find anywhere in the code where the TCP Keep Alive are enabled. Maybe I missed something ? In the urllib3 library it seems to me that TCP Keep Alive are not enabled by default, see https://github.com/urllib3/urllib3/blob/5a24dc55d86e4c5ca7fe0bd496da768caf617591/src/urllib3/connection.py#L109.

It would also be good to have the possibility to pass socket arguments to the HTTPAdapter, for my use case where I needed TCP keep alive, I had to overwrite the HTTPAdapter like this in order to pass socket options:

class HTTPKeepAliveAdapter(HTTPAdapter):

    def __init__(self, max_retries):
        self._pool_kwargs = {
            'socket_options':
                HTTPConnection.default_socket_options + [
                    (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),  # enable keep alive
                    (socket.SOL_TCP, socket.TCP_KEEPIDLE, 120),  # idle time 120s
                    (socket.SOL_TCP, socket.TCP_KEEPINTVL, 120)
                ]  # interval time 120s
        }
        super().__init__(pool_connections=40, pool_maxsize=60, max_retries=max_retries)
        self.init_poolmanager(
            self._pool_connections, self._pool_maxsize, block=self._pool_block, **self._pool_kwargs
        )

    def __setstate__(self, state):
        super().__init__(state)

        self.init_poolmanager(
            self._pool_connections, self._pool_maxsize, block=self._pool_block, **self._pool_kwargs
        )

But I may have missed something as well ?

@sigmavirus24
Copy link
Contributor

If you'd like to specify custom socket options more conveniently, https://toolbelt.readthedocs.io/en/latest/adapters.html#socketoptionsadapter

I believe TCP Keep-Alive caused more issues than benefits but the references in the docs were never removed. There's also: https://toolbelt.readthedocs.io/en/latest/adapters.html#requests_toolbelt.adapters.socket_options.TCPKeepAliveAdapter to make this easier for folks who know they need it

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants