ostinelli / net-http2

NetHttp2 is an HTTP/2 client for Ruby.
MIT License
140 stars 31 forks source link

Preventing disconnection due to network inactivity #42

Open abulava opened 4 years ago

abulava commented 4 years ago

I had a frustrating experience with running a net-http2 client on Azure VM. When the client is idle for about 240 seconds, Azure infrastructure will silently drop "outbound SNAT". As a result, I get the :error callback with Errno::ETIMEDOUT in 15 minutes after the async request.

A reply from Azure support confirmed that I was right about my idea to tune the TCP keepalive settings like sysctl -w net.ipv4.tcp_keepalive_time=120. Unfortunately, net-http2 was not affected by this tuning.

I found that net-http2 simply doesn't set the specific socket option SO_KEEPALIVE, which is necessary for TCP keepalive. So, I forked the net-http2 and applied a band aid.

abulava commented 4 years ago

Sure, my solution is not good. A quote from TCP Keepalive HOWTO:

The most beautiful thing you can do when writing an application is to make it as customizable as possible, and not to force decisions. If you want to consider the happiness of your users, you should implement keepalive and let the users decide if they want to use it or not by using a configuration parameter or a switch on the command line.

I think that a proper solution should add new options to NetHttp2::Client.new for enabling TCP keepalives and tuning their settings at a socket level with SOL_TCP TCP_KEEPIDLE/TCP_KEEPINTVL/TCP_KEEPCNT (these options would allow to keep system-wide sysctl settings intact).