Set socket options on HTTP client

13 views
Skip to first unread message

stephen pierce

unread,
Aug 26, 2019, 9:26:48 PM8/26/19
to http.rb: a fast, easy-to-use Ruby HTTP client with a chainable API
I want to turn on TCP keepalives on an HTTP session.

I'm running ruby 2.6.3 on Linux, and http-3.3.0 gem is installed.

I can set the socket options on a socket object like this:

s = TCPSocket.new('127.0.0.1', 80)

s
.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true)
s
.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPIDLE, 5)
s
.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPINTVL, 20)
s
.setsockopt(Socket::SOL_TCP, Socket::TCP_KEEPCNT, 5)

However, I am using a library that uses HTTP like this:

client = HTTP::Client.new(follow: follow_option)

And I get back the client object. So, I'd like to be able to set the socket option using the client variable. I figure I need to get the underlying socket object from client, but I do not know how.

I am new to ruby, but I have 20+ years in a bunch of other languages (python, perl, and c mostly), so pardon the stupid question, if it's easy.

stephen pierce

unread,
Aug 27, 2019, 12:53:04 AM8/27/19
to http.rb: a fast, easy-to-use Ruby HTTP client with a chainable API
After thinking about it more, there may be another solution to my problem.

The problem I am having is long-lived HTTP connections silently dying. The library I'm using is opening a long connection (like a websocket, but it's just chunked-mode for the response) and getting pushed updates. I suspect some NAT device/loadbalancer is *silently* dropping the connection from it's NAT mapping table due to inactivity. After this, new push notifications (TCP segments) are getting a TCP_RESET from the NAT/LB (clearing the server state), but the TCP state on the client is still connected, and no new data. The client can't tell this has happened. It's still connected, as far as it knows.

So, sending TCP keepalives will detect this condition, or more likely prevent it. Any kind of data (TCP control or data) would do the trick. Is there a way to send HTTP traffic periodically during a chunked response to do the same thing as the TCP keepalive?

Steve


--
You received this message because you are subscribed to the Google Groups "http.rb: a fast, easy-to-use Ruby HTTP client with a chainable API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to httprb+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/httprb/3432d6ca-9cb8-45b7-ab35-ead00c57994c%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages