lib/unimatrix/request.rb in unimatrix-3.3.0 vs lib/unimatrix/request.rb in unimatrix-3.3.1

- old
+ new

@@ -6,10 +6,15 @@ class Request def initialize( default_parameters = {} ) uri = URI( Unimatrix.configuration.url ) @http = Net::HTTP.new( uri.host, uri.port ) + + timeout_limit = ( ENV[ 'TIMEOUT_LIMIT' ] || 60 ).to_i + + @http.open_timeout = timeout_limit + @http.read_timeout = timeout_limit @http.use_ssl = ( uri.scheme == 'https' ) @http.verify_mode = OpenSSL::SSL::VERIFY_NONE @default_parameters = default_parameters.stringify_keys @@ -53,20 +58,28 @@ 3.times do response = begin yield rescue Timeout::Error => error + log_error( error.inspect ) + error end - unless response.nil? || ( response.is_a?( Response ) && retry_codes.include?( response.code ) ) - - response = nil if response.is_a?( Timeout::Error ) - + unless response.nil? || + ( response.is_a?( Response ) && retry_codes.include?( response.code ) ) || + response.is_a?( Timeout::Error ) + break end end + + if response.is_a?( Timeout::Error ) + response = Unimatrix::TimeoutError.new( + message: response.message + ) + end response end protected; def compose_request_path( path, parameters = {} ) @@ -75,9 +88,17 @@ addressable.path = path addressable.query = parameters.to_param unless parameters.blank? addressable.to_s + end + + protected; def log_error( message ) + if defined?( logger.error ) + logger.error( message ) + else + puts "Error: #{ message }" + end end end end