Skip to content

Commit

Permalink
Increase connection keep alive timeout to 30 seconds
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
brandur committed Aug 20, 2019
1 parent b31632a commit 25bd677
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/stripe/connection_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ def connection_for(uri)

if connection.nil?
connection = create_connection(u)

# TODO: what happens after TTL?
connection.start

@active_connections[[u.host, u.port]] = connection
Expand Down Expand Up @@ -92,6 +90,15 @@ def execute_request(method, uri, body: nil, headers: nil, query: nil)
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

Expand Down

0 comments on commit 25bd677

Please sign in to comment.