diff options
author | Patrick J Cherry <patrick@bytemark.co.uk> | 2011-08-31 14:04:16 +0100 |
---|---|---|
committer | Patrick J Cherry <patrick@bytemark.co.uk> | 2011-08-31 14:04:16 +0100 |
commit | dfb79527aeab7b973c0ece9e681b9813eb148ab6 (patch) | |
tree | fd44dac90ae9a1831f2accff5ea6375f2f164a86 | |
parent | e34524d3562906d96ecfb276c17a0536d587b6c0 (diff) |
Fixed do_get timeout and redirection
-rw-r--r-- | debian/changelog | 7 | ||||
-rw-r--r-- | lib/mauve/calendar_interface.rb | 32 | ||||
-rw-r--r-- | lib/mauve/person.rb | 2 | ||||
-rw-r--r-- | lib/mauve/version.rb | 2 |
4 files changed, 34 insertions, 9 deletions
diff --git a/debian/changelog b/debian/changelog index 88dd009..d057301 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,10 @@ +mauvealert (3.5.3) stable; urgency=low + + * Fixed do_get timeout + * Added small cache for fetching external URIs. + + -- Patrick J Cherry <patrick@bytemark.co.uk> Wed, 31 Aug 2011 14:03:45 +0100 + mauvealert (3.5.2) stable; urgency=low * Added holiday checks diff --git a/lib/mauve/calendar_interface.rb b/lib/mauve/calendar_interface.rb index 4479bf4..f06c597 100644 --- a/lib/mauve/calendar_interface.rb +++ b/lib/mauve/calendar_interface.rb @@ -28,7 +28,7 @@ module Mauve # @param [String] url A Calendar API url. # @return [Array] A list of all the username on support. def get_users_on_support(url) - result = do_get(url) + result = do_get_with_cache(url) if result.is_a?(String) result = result.split("\n") @@ -57,11 +57,11 @@ module Mauve # @param [String] url A Calendar API url. # @param [String] usr User single sign on. # @return [Boolean] True if on holiday, false otherwise. - def is_user_on_holiday?(url, usr) - result = do_get(url) + def is_user_on_holiday?(url) + result = do_get_with_cache(url) if result.is_a?(String) and result =~ /^\d{4}(-\d\d){2}[ T](\d\d:){2}\d\d/ - return result + return true else return false end @@ -91,7 +91,10 @@ module Mauve http = Net::HTTP.new(uri.host, uri.port) - http.open_timeout = http.read_timeout = TIMEOUT + # + # Five second timeouts. + # + http.open_timeout = http.read_timeout = 5 if (uri.scheme == "https") http.use_ssl = true @@ -101,9 +104,9 @@ module Mauve response = http.start { http.get(uri.request_uri()) } if response.is_a?(Net::HTTPOK) - return response.body + return response.body - elsif response.is_a?(Net::HTTPRedirection) and response.has_key?('Location') + elsif response.is_a?(Net::HTTPRedirection) and response.key?('Location') location = response['Location'] # @@ -132,6 +135,21 @@ module Mauve return nil end + def do_get_with_cache(url, cache_until = Time.now + 5.minutes) + @cache ||= {} + + if @cache.has_key?(url.to_s) + result, cache_until = @cache[url.to_s] + + return result if cache_until >= Time.now and not result.nil? + end + + result = do_get(url) + @cache[url] = [result, cache_until] unless result.nil? + + return result + end + end end diff --git a/lib/mauve/person.rb b/lib/mauve/person.rb index 5484271..a656bf3 100644 --- a/lib/mauve/person.rb +++ b/lib/mauve/person.rb @@ -173,7 +173,7 @@ module Mauve def is_on_holiday? () return false if holiday_url.nil? or holiday_url.empty? - return CalendarInterface.is_user_on_holiday?(holiday_url, username) + return CalendarInterface.is_user_on_holiday?(holiday_url) end end diff --git a/lib/mauve/version.rb b/lib/mauve/version.rb index c473b83..4690bab 100644 --- a/lib/mauve/version.rb +++ b/lib/mauve/version.rb @@ -1,5 +1,5 @@ module Mauve - VERSION="3.5.2" + VERSION="3.5.3" end |