From cebc6e3b8a31bced955be78cc53f94b0b217ec04 Mon Sep 17 00:00:00 2001 From: Roy Razon Date: Thu, 27 Dec 2018 12:33:27 +0200 Subject: [PATCH] added timeout options to config --- lib/hub_client.rb | 27 ++++++++++--- lib/hub_client/configuration.rb | 2 +- .../exponential_backoff_interval.rb | 2 +- spec/hub_client_spec.rb | 39 ++++++++++++++++++- 4 files changed, 61 insertions(+), 9 deletions(-) diff --git a/lib/hub_client.rb b/lib/hub_client.rb index c77f7b2..226c56a 100644 --- a/lib/hub_client.rb +++ b/lib/hub_client.rb @@ -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}") @@ -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" diff --git a/lib/hub_client/configuration.rb b/lib/hub_client/configuration.rb index ae73e51..c2e7ec9 100644 --- a/lib/hub_client/configuration.rb +++ b/lib/hub_client/configuration.rb @@ -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 diff --git a/lib/hub_client/exponential_backoff_interval.rb b/lib/hub_client/exponential_backoff_interval.rb index 60ac435..013e97b 100644 --- a/lib/hub_client/exponential_backoff_interval.rb +++ b/lib/hub_client/exponential_backoff_interval.rb @@ -4,7 +4,7 @@ class ExponentialBackoffInterval DEFAULT_OPTS = { initial: 0.5, - multiplier: 1.2, + multiplier: 1.5, rand_factor: 0.05, max_count: 10, } diff --git a/spec/hub_client_spec.rb b/spec/hub_client_spec.rb index 2e13621..5f9eacb 100644 --- a/spec/hub_client_spec.rb +++ b/spec/hub_client_spec.rb @@ -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" } @@ -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" })