From 5e518f50373291ad7c649eee543572ac86c4dd2c Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 21 Jan 2016 02:07:50 +0100 Subject: [PATCH] Added total timeout of whole http request. --- app/models/avatar.rb | 1 + lib/import/otrs.rb | 4 ++++ lib/service/geo_calendar/zammad.rb | 7 ++++--- lib/service/geo_ip/zammad.rb | 1 + lib/service/geo_location/gmaps.rb | 25 +++++++++++++++++++++---- lib/service/image/zammad.rb | 5 ++++- lib/user_agent.rb | 29 +++++++++++++++++++++-------- 7 files changed, 56 insertions(+), 16 deletions(-) diff --git a/app/models/avatar.rb b/app/models/avatar.rb index c4e6a8af6..23c07c0f3 100644 --- a/app/models/avatar.rb +++ b/app/models/avatar.rb @@ -111,6 +111,7 @@ add a avatar { open_timeout: 4, read_timeout: 6, + total_timeout: 6, }, ) if !response.success? diff --git a/lib/import/otrs.rb b/lib/import/otrs.rb index 92b64428f..e80b483ae 100644 --- a/lib/import/otrs.rb +++ b/lib/import/otrs.rb @@ -60,6 +60,7 @@ module Import::OTRS { open_timeout: 10, read_timeout: 60, + total_timeout: 180, user: Setting.get('import_otrs_user'), password: Setting.get('import_otrs_password'), }, @@ -93,9 +94,11 @@ module Import::OTRS log 'PARAMS: ' + data.inspect open_timeout = 10 read_timeout = 120 + total_timeout = 360 if data.empty? open_timeout = 6 read_timeout = 20 + total_timeout = 120 end response = UserAgent.post( url, @@ -103,6 +106,7 @@ module Import::OTRS { open_timeout: open_timeout, read_timeout: read_timeout, + total_timeout: total_timeout, user: Setting.get('import_otrs_user'), password: Setting.get('import_otrs_password'), }, diff --git a/lib/service/geo_calendar/zammad.rb b/lib/service/geo_calendar/zammad.rb index 1e325d9b4..22252a51e 100644 --- a/lib/service/geo_calendar/zammad.rb +++ b/lib/service/geo_calendar/zammad.rb @@ -7,7 +7,7 @@ class Service::GeoCalendar::Zammad # check cache cache_key = "zammadgeocalendar::#{address}" - cache = Cache.get( cache_key ) + cache = Cache.get(cache_key) return cache if cache # do lookup @@ -26,6 +26,7 @@ class Service::GeoCalendar::Zammad json: true, open_timeout: 2, read_timeout: 4, + total_timeout: 12, }, ) if !response.success? && response.code.to_s !~ /^40.$/ @@ -34,10 +35,10 @@ class Service::GeoCalendar::Zammad data = response.data - Cache.write( cache_key, data, { expires_in: 30.minutes } ) + Cache.write(cache_key, data, { expires_in: 30.minutes }) rescue => e Rails.logger.error "#{host}#{url}: #{e.inspect}" - Cache.write( cache_key, data, { expires_in: 1.minute } ) + Cache.write(cache_key, data, { expires_in: 1.minute }) end data end diff --git a/lib/service/geo_ip/zammad.rb b/lib/service/geo_ip/zammad.rb index a69a7a1d8..7e8014845 100644 --- a/lib/service/geo_ip/zammad.rb +++ b/lib/service/geo_ip/zammad.rb @@ -22,6 +22,7 @@ class Service::GeoIp::Zammad json: true, open_timeout: 2, read_timeout: 4, + total_timeout: 4, }, ) if !response.success? && response.code.to_s !~ /^40.$/ diff --git a/lib/service/geo_location/gmaps.rb b/lib/service/geo_location/gmaps.rb index eb98313a6..2e688e5a8 100644 --- a/lib/service/geo_location/gmaps.rb +++ b/lib/service/geo_location/gmaps.rb @@ -4,10 +4,18 @@ class Service::GeoLocation::Gmaps def self.geocode(address) url = "http://maps.googleapis.com/maps/api/geocode/json?address=#{CGI.escape address}&sensor=true" - response = UserAgent.get(url) + response = UserAgent.get( + url, + {}, + { + open_timeout: 2, + read_timeout: 4, + total_timeout: 4, + }, + ) return if !response.success? - result = JSON.parse( response.body ) + result = JSON.parse(response.body) return if !result return if !result['results'] @@ -20,10 +28,19 @@ class Service::GeoLocation::Gmaps def self.reverse_geocode(lat, lng) url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=#{lat},#{lng}&sensor=true" - response = UserAgent.get(url) + response = UserAgent.get( + url, + {}, + { + json: true, + open_timeout: 2, + read_timeout: 4, + total_timeout: 4, + }, + ) return if !response.success? - result = JSON.parse( response.body ) + result = JSON.parse(response.body) address = result['results'].first['address_components'].first['long_name'] address diff --git a/lib/service/image/zammad.rb b/lib/service/image/zammad.rb index f1e23ec48..39c848e4b 100644 --- a/lib/service/image/zammad.rb +++ b/lib/service/image/zammad.rb @@ -6,6 +6,7 @@ class Service::Image::Zammad @@api_host = 'https://images.zammad.com' @@open_timeout = 4 @@read_timeout = 6 + @@total_timeout = 8 def self.user(email) @@ -18,6 +19,7 @@ class Service::Image::Zammad { open_timeout: @@open_timeout, read_timeout: @@read_timeout, + total_timeout: @@total_timeout, }, ) if !response.success? @@ -46,6 +48,7 @@ class Service::Image::Zammad { open_timeout: @@open_timeout, read_timeout: @@read_timeout, + total_timeout: @@total_timeout, }, ) if !response.success? @@ -66,7 +69,7 @@ class Service::Image::Zammad return false if !image # store image 1:1 - product_logo = StaticAssets.store_raw( image[:content], image[:mime_type] ) + product_logo = StaticAssets.store_raw(image[:content], image[:mime_type]) Setting.set('product_logo', product_logo) true diff --git a/lib/user_agent.rb b/lib/user_agent.rb index 739019bc9..0cb9be737 100644 --- a/lib/user_agent.rb +++ b/lib/user_agent.rb @@ -59,8 +59,11 @@ returns # start http call begin - response = http.request(request) - return process(response, uri, count, params, options) + total_timeout = options[:total_timeout] || 60 + Timeout.timeout(total_timeout) do + response = http.request(request) + return process(response, uri, count, params, options) + end rescue => e return Result.new( error: e.inspect, @@ -83,6 +86,7 @@ post http/https calls { open_timeout: 4, read_timeout: 10, + total_timeout: 60, }, ) @@ -107,8 +111,11 @@ returns # start http call begin - response = http.request(request) - return process(response, uri, count, params, options) + total_timeout = options[:total_timeout] || 60 + Timeout.timeout(total_timeout) do + response = http.request(request) + return process(response, uri, count, params, options) + end rescue => e return Result.new( error: e.inspect, @@ -155,8 +162,11 @@ returns # start http call begin - response = http.request(request) - return process(response, uri, count, params, options) + total_timeout = options[:total_timeout] || 60 + Timeout.timeout(total_timeout) do + response = http.request(request) + return process(response, uri, count, params, options) + end rescue => e return Result.new( error: e.inspect, @@ -196,8 +206,11 @@ returns # start http call begin - response = http.request(request) - return process(response, uri, count, {}, options) + total_timeout = options[:total_timeout] || 60 + Timeout.timeout(total_timeout) do + response = http.request(request) + return process(response, uri, count, {}, options) + end rescue => e return Result.new( error: e.inspect,