Skip to content

Commit

Permalink
Allow setting write_timeout for connections on Ruby 2.6+
Browse files Browse the repository at this point in the history
  • Loading branch information
bdewater committed Oct 6, 2020
1 parent f3b83f1 commit 841345d
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -191,6 +191,7 @@ Open and read timeouts are configurable:
```ruby
Stripe.open_timeout = 30 # in seconds
Stripe.read_timeout = 80
Stripe.write_timeout = 30
```

Please take care to set conservative read timeouts. Some API requests can take
Expand Down
1 change: 1 addition & 0 deletions lib/stripe.rb
Expand Up @@ -71,6 +71,7 @@ class << self
def_delegators :@configuration, :connect_base, :connect_base=
def_delegators :@configuration, :open_timeout, :open_timeout=
def_delegators :@configuration, :read_timeout, :read_timeout=
def_delegators :@configuration, :write_timeout, :write_timeout=
def_delegators :@configuration, :proxy, :proxy=
def_delegators :@configuration, :verify_ssl_certs, :verify_ssl_certs=
def_delegators :@configuration, :ca_bundle_path, :ca_bundle_path=
Expand Down
3 changes: 3 additions & 0 deletions lib/stripe/connection_manager.rb
Expand Up @@ -119,6 +119,9 @@ def execute_request(method, uri, body: nil, headers: nil, query: nil)

connection.open_timeout = Stripe.open_timeout
connection.read_timeout = Stripe.read_timeout
if connection.respond_to?(:write_timeout=)
connection.write_timeout = Stripe.write_timeout
end

connection.use_ssl = uri.scheme == "https"

Expand Down
7 changes: 7 additions & 0 deletions lib/stripe/stripe_configuration.rb
Expand Up @@ -42,6 +42,7 @@ class StripeConfiguration
attr_reader :max_network_retry_delay
attr_reader :open_timeout
attr_reader :read_timeout
attr_reader :write_timeout
attr_reader :proxy
attr_reader :verify_ssl_certs

Expand Down Expand Up @@ -72,6 +73,7 @@ def initialize

@open_timeout = 30
@read_timeout = 80
@write_timeout = 30

@api_base = "https://api.stripe.com"
@connect_base = "https://connect.stripe.com"
Expand Down Expand Up @@ -109,6 +111,11 @@ def read_timeout=(read_timeout)
StripeClient.clear_all_connection_managers
end

def write_timeout=(write_timeout)
@write_timeout = write_timeout
StripeClient.clear_all_connection_managers
end

def proxy=(proxy)
@proxy = proxy
StripeClient.clear_all_connection_managers
Expand Down
4 changes: 4 additions & 0 deletions test/stripe/connection_manager_test.rb
Expand Up @@ -39,6 +39,7 @@ class ConnectionManagerTest < Test::Unit::TestCase

old_open_timeout = Stripe.open_timeout
old_read_timeout = Stripe.read_timeout
old_write_timeout = Stripe.write_timeout

begin
# Make sure any global initialization here is undone in the `ensure`
Expand All @@ -47,6 +48,7 @@ class ConnectionManagerTest < Test::Unit::TestCase

Stripe.open_timeout = 123
Stripe.read_timeout = 456
Stripe.write_timeout = 789

conn = @manager.connection_for("https://stripe.com")

Expand All @@ -63,6 +65,7 @@ class ConnectionManagerTest < Test::Unit::TestCase
# Timeouts
assert_equal 123, conn.open_timeout
assert_equal 456, conn.read_timeout
assert_equal 789, conn.write_timeout

assert_equal true, conn.use_ssl?
assert_equal OpenSSL::SSL::VERIFY_PEER, conn.verify_mode
Expand All @@ -72,6 +75,7 @@ class ConnectionManagerTest < Test::Unit::TestCase

Stripe.open_timeout = old_open_timeout
Stripe.read_timeout = old_read_timeout
Stripe.write_timeout = old_write_timeout
end
end

Expand Down
3 changes: 3 additions & 0 deletions test/stripe/stripe_configuration_test.rb
Expand Up @@ -16,6 +16,7 @@ class StripeConfigurationTest < Test::Unit::TestCase
assert_equal 0, config.max_network_retries
assert_equal 30, config.open_timeout
assert_equal 80, config.read_timeout
assert_equal 30, config.write_timeout
assert_equal "https://api.stripe.com", config.api_base
assert_equal "https://connect.stripe.com", config.connect_base
assert_equal "https://files.stripe.com", config.uploads_base
Expand All @@ -25,10 +26,12 @@ class StripeConfigurationTest < Test::Unit::TestCase
config = Stripe::StripeConfiguration.setup do |c|
c.open_timeout = 100
c.read_timeout = 100
c.write_timeout = 100
end

assert_equal 100, config.open_timeout
assert_equal 100, config.read_timeout
assert_equal 100, config.write_timeout
end
end

Expand Down
5 changes: 5 additions & 0 deletions test/stripe_test.rb
Expand Up @@ -54,6 +54,11 @@ class StripeTest < Test::Unit::TestCase
assert_equal 10, Stripe.read_timeout
end

should "allow write timeout to be configured" do
Stripe.write_timeout = 10
assert_equal 10, Stripe.write_timeout
end

should "allow api_key to be configured" do
Stripe.api_key = "sk_local_test"
assert_equal "sk_local_test", Stripe.api_key
Expand Down

0 comments on commit 841345d

Please sign in to comment.