Increase connection keep alive timeout to 30 seconds (#836)

One thing I forgot to look into was how long Ruby will hold a connection
open by default. It turns out that the language default is very low at
only two seconds. Here we increase it to 30 seconds, which is a more
reasonable default. I took this number from Go's `DefaultTransport`,
which seems to have been working pretty well so far.

I tested with a script that keeps a connection idle to Stripe for a long
period of time before issuing a new request and everything seems to be
working well.
This commit is contained in:
Brandur 2019-08-20 13:54:34 -07:00 committed by Brandur
parent 1f0a3d3ec8
commit 650114abca

View File

@ -40,8 +40,6 @@ module Stripe
if connection.nil?
connection = create_connection(u)
# TODO: what happens after TTL?
connection.start
@active_connections[[u.host, u.port]] = connection
@ -92,6 +90,15 @@ module Stripe
proxy_host, proxy_port,
proxy_user, proxy_pass)
# Time in seconds within which Net::HTTP will try to reuse an already
# open connection when issuing a new operation. Outside this window, Ruby
# will transparently close and re-open the connection without trying to
# reuse it.
#
# Ruby's default of 2 seconds is almost certainly too short. Here I've
# reused Go's default for `DefaultTransport`.
connection.keep_alive_timeout = 30
connection.open_timeout = Stripe.open_timeout
connection.read_timeout = Stripe.read_timeout