aiohttp icon indicating copy to clipboard operation
aiohttp copied to clipboard

Client TCPConnector supports TCP Keep-Alive

Open thpica opened this issue 3 years ago • 2 comments

🐣 Is your feature request related to a problem? Please describe.

When streaming data using HTTP Chunked Encoding there is no way to know if the connection was broken or if the next chunk is not ready yet. Enabling TCP Keep-Alive would allow to reestablish broken connexions and resume streaming. The concrete use case is watching Kubernetes entities.

💡 Describe the solution you'd like

The TCPConnector should accept a TCPKeepAliveConfig namedtuple:

class TCPKeepAliveConfig(NamedTuple):
    enabled: bool = False
    after_idle_sec: Optional[int] = None
    interval_sec: Optional[int] = None
    max_fails: Optional[int] = None

Describe alternatives you've considered

Currently periodically restarting the connection is the only solution that was tried.

📋 Additional context

Example of how to enable keepalive: https://stackoverflow.com/a/14855726

thpica avatar Jun 02 '21 08:06 thpica

I also have a use case where this would be useful, either directly or just enabling setting socket options for the underlying socket. The issue we are experiencing is related to some (admittedly weird) defaults in Azure: https://carsonip.me/posts/azure-tcp-idle-timeout-tcp-keepalive-python/.

In some cases it's not possible to change the underlying system default tcp_keepalive_timeout (which I assume is what @thpica's after_idle_sec is meant to mirror), and as such it's only possible to change for a specific connection/socket.

cpnielsen avatar Jul 21 '22 10:07 cpnielsen