Skip to content

Commit

Permalink
added timeout options to config
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy Razon committed Dec 27, 2018
1 parent 83c0fbd commit cebc6e3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
27 changes: 21 additions & 6 deletions lib/hub_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@
require "rest-client"

module HubClient
REQUEST_HEADERS = {
content_type: :json,
accept: :json,
}

def self.publish(metadata, content, env = nil)
raise ConfigArgumentMissing, "endpoint_url missing" unless HubClient.configuration.endpoint_url
config = HubClient.configuration
raise ConfigArgumentMissing, "endpoint_url missing" unless config.endpoint_url

payload = (metadata.is_a?(String) || metadata.is_a?(Symbol)) ? { type: metadata.to_s } : metadata
payload[:content] = content
payload[:env] ||= env || payload[:env] || payload['env'] || HubClient.configuration.env

hub_url = build_hub_url(HubClient.configuration.endpoint_url)
payload[:env] ||= env || payload[:env] || payload['env'] || config.env

retry_intervals = HubClient.configuration.retry_intervals
retry_intervals = config.retry_intervals

retries = 0
begin
RestClient.post(hub_url, payload.to_json, content_type: :json, accept: :json)
RestClient::Request.execute(request_opts(config, payload))
rescue RestClient::Exception => e
HubClient.logger.warn("HubClient Exception #{e.class}: #{e.message} Code: #{e.http_code} Response: #{e.response} Request: #{payload}")

Expand All @@ -32,6 +36,17 @@ def self.publish(metadata, content, env = nil)

private

def self.request_opts(config, payload)
{
method: :post,
url: build_hub_url(config.endpoint_url),
payload: payload.to_json,
headers: REQUEST_HEADERS,
timeout: config.timeout,
open_timeout: config.open_timeout,
}.reject {|_k,v| v.nil?}
end

def self.build_hub_url(endpoint_url)
endpoint_url = endpoint_url.gsub(/\/$/, '') # remove last '/' if exists
"#{endpoint_url}/api/#{HUB_VERSION}/messages"
Expand Down
2 changes: 1 addition & 1 deletion lib/hub_client/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module HubClient
class Configuration
attr_accessor :env, :access_token, :endpoint_url, :retry_intervals
attr_accessor :env, :access_token, :endpoint_url, :retry_intervals, :open_timeout, :timeout
end

def self.configuration
Expand Down
2 changes: 1 addition & 1 deletion lib/hub_client/exponential_backoff_interval.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ExponentialBackoffInterval

DEFAULT_OPTS = {
initial: 0.5,
multiplier: 1.2,
multiplier: 1.5,
rand_factor: 0.05,
max_count: 10,
}
Expand Down
39 changes: 38 additions & 1 deletion spec/hub_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

describe "#publish" do
context "not configured" do
after(:each) { HubClient.reset_configuration }
before {HubClient.reset_configuration}

it "raises an error when endpoint_url is not configured" do
HubClient.configure { |config| config.env = "il-qa2" }
Expand Down Expand Up @@ -62,6 +62,43 @@

after(:all) { HubClient.reset_configuration }

describe 'configured timeout' do
before do
allow(RestClient::Request).to receive(:new).and_call_original
stub_request(:post, HubClient.build_hub_url(HubClient.configuration.endpoint_url))
end

context 'when specified' do
before do
HubClient.configure do |config|
config.timeout = 31
config.open_timeout = 33
end
HubClient.publish(:order_created, { some: "content" })
end

it 'is passed to RestClient' do
expect(RestClient::Request).to have_received(:new).with(hash_including(timeout: 31, open_timeout: 33))
end
end

context 'when not specified' do
HubClient.reset_configuration
HubClient.configure do |config|
config.env = "il-qa2"
config.endpoint_url = "http://service-hub.com"
end

before do
HubClient.publish(:order_created, { some: "content" })
end

it 'is not passed to RestClient' do
expect(RestClient::Request).to have_received(:new).with(hash_not_including(:timeout, :open_timeout))
end
end
end

describe 'exception handling' do
def action
HubClient.publish(:order_created, { some: "content" })
Expand Down

0 comments on commit cebc6e3

Please sign in to comment.