Skip to content

Commit 073bccc

Browse files
committed
Add default open_timeout
Faraday supports this and passes it through to the underlying Net::HTTP interface. There's little to no reason not to specify something reasonable here. If clients can't connect in 1000ms, there are likely some problems with the network in one fashion or another.
1 parent 13b596b commit 073bccc

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ v2.0.0
33
- Upgrade to Rspec 3
44
- Upgrade all dependencies to current stable versions
55
- Catch MultiJson::ParseErrors and return SparkApi::InvalidJSON error instead
6+
- Add default 1 second open_timeout to HTTP requests
67

78
v1.4.28
89
- move public_suffix to development dependency where it belongs

lib/spark_api/configuration.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Configuration
1111
# valid configuration options
1212
VALID_OPTION_KEYS = [:api_key, :api_secret, :api_user, :endpoint,
1313
:user_agent, :version, :ssl, :ssl_verify, :oauth2_provider, :authentication_mode,
14-
:auth_endpoint, :callback, :compress, :timeout, :middleware, :dictionary_version, :request_id_chain].freeze
14+
:auth_endpoint, :callback, :compress, :open_timeout, :timeout, :middleware, :dictionary_version, :request_id_chain].freeze
1515
OAUTH2_KEYS = [:authorization_uri, :access_uri, :client_id, :client_secret,
1616
# Requirements for authorization_code grant type
1717
:redirect_uri,
@@ -42,6 +42,7 @@ module Configuration
4242
DEFAULT_SSL_VERIFY = true
4343
DEFAULT_OAUTH2 = nil
4444
DEFAULT_COMPRESS = false
45+
DEFAULT_OPEN_TIMEOUT = 1 # seconds
4546
DEFAULT_TIMEOUT = 5 # seconds
4647
DEFAULT_MIDDLEWARE = 'spark_api'
4748
DEFAULT_DICTIONARY_VERSION = nil
@@ -78,6 +79,7 @@ def reset_configuration
7879
self.ssl_verify = DEFAULT_SSL_VERIFY
7980
self.version = DEFAULT_VERSION
8081
self.compress = DEFAULT_COMPRESS
82+
self.open_timeout = DEFAULT_OPEN_TIMEOUT
8183
self.timeout = DEFAULT_TIMEOUT
8284
self.middleware = DEFAULT_MIDDLEWARE
8385
self.dictionary_version = DEFAULT_DICTIONARY_VERSION

lib/spark_api/connection.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def connection(force_ssl = false)
3737

3838
conn = Faraday.new(opts) do |conn|
3939
conn.response self.middleware.to_sym
40+
conn.options[:open_timeout] = self.open_timeout
4041
conn.options[:timeout] = self.timeout
4142
conn.adapter Faraday.default_adapter
4243
end

spec/unit/spark_api/configuration_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
expect(SparkApi.timeout).to eq(5)
1616
expect(SparkApi.request_id_chain).to be_nil
1717
expect(SparkApi.middleware).to eq('spark_api')
18+
expect(SparkApi.open_timeout).to eq(1)
1819
end
1920
end
2021

@@ -25,6 +26,7 @@
2526
:api_user => "1234",
2627
:auth_endpoint => "https://login.wade.dev.fbsdata.com",
2728
:endpoint => "http://api.wade.dev.fbsdata.com",
29+
:open_timeout => 2,
2830
:timeout => 15,
2931
:request_id_chain => 'foobar')
3032

@@ -34,6 +36,7 @@
3436
expect(client.auth_endpoint).to match("https://login.wade.dev.fbsdata.com")
3537
expect(client.endpoint).to match("http://api.wade.dev.fbsdata.com")
3638
expect(client.version).to match("v1")
39+
expect(client.open_timeout).to eq(2)
3740
expect(client.timeout).to eq(15)
3841
expect(client.request_id_chain).to eq('foobar')
3942
end
@@ -97,6 +100,7 @@
97100
config.version = "veleventy"
98101
config.endpoint = "test.api.sparkapi.com"
99102
config.user_agent = "my useragent"
103+
config.open_timeout = 2
100104
config.timeout = 15
101105
end
102106

@@ -107,6 +111,7 @@
107111
expect(SparkApi.endpoint).to match("test.api.sparkapi.com")
108112
expect(SparkApi.user_agent).to match("my useragent")
109113
expect(SparkApi.oauth2_enabled?()).to be false
114+
expect(SparkApi.open_timeout).to eq(2)
110115
expect(SparkApi.timeout).to eq(15)
111116
end
112117

@@ -226,14 +231,21 @@
226231
expect(c.connection.headers["Accept-Encoding"]).to eq("gzip, deflate")
227232
end
228233

229-
it "should set default timeout of 5 seconds" do
234+
it "should set default read timeout of 5 seconds" do
230235
c = SparkApi::Client.new(:endpoint => "https://sparkapi.com")
231236
expect(c.connection.options[:timeout]).to eq(5)
232237
end
233238

239+
it "should set default open timeout of 1 second" do
240+
c = SparkApi::Client.new(:endpoint => "https://sparkapi.com")
241+
expect(c.connection.options[:open_timeout]).to eq(1)
242+
end
243+
234244
it "should set alternate timeout if specified" do
235245
c = SparkApi::Client.new(:endpoint => "https://sparkapi.com",
246+
:open_timeout => 5,
236247
:timeout => 15)
248+
expect(c.connection.options[:open_timeout]).to eq(5)
237249
expect(c.connection.options[:timeout]).to eq(15)
238250
end
239251
end

0 commit comments

Comments
 (0)