diff options
author | Cheah Chu Yeow <chuyeow@gmail.com> | 2008-04-13 14:40:30 +0800 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2008-04-22 09:30:41 +1200 |
commit | 105910429d5873dce677ef32eef5f705e0625d86 (patch) | |
tree | f68855ea14f72e6a094f84f660b5421baaa58308 /activeresource/lib/active_resource/connection.rb | |
parent | 4809dcc1b50330a04ec61dd1fef6cdba9892ac3d (diff) | |
download | rails-105910429d5873dce677ef32eef5f705e0625d86.tar.gz rails-105910429d5873dce677ef32eef5f705e0625d86.tar.bz2 rails-105910429d5873dce677ef32eef5f705e0625d86.zip |
Introduce ActiveResource::Base.timeout. This allows a timeout to be set on the internal Net::HTTP instance ARes uses (default is 60 seconds). Setting a low timeout allows ARes clients to fail-fast in the event of a unresponsive/crashed server, rather than cause cascading failures in your application.
Signed-off-by: Michael Koziarski <michael@koziarski.com>
Diffstat (limited to 'activeresource/lib/active_resource/connection.rb')
-rw-r--r-- | activeresource/lib/active_resource/connection.rb | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb index c8cee7aaa3..3b61009f43 100644 --- a/activeresource/lib/active_resource/connection.rb +++ b/activeresource/lib/active_resource/connection.rb @@ -55,7 +55,7 @@ module ActiveResource # This class is used by ActiveResource::Base to interface with REST # services. class Connection - attr_reader :site, :user, :password + attr_reader :site, :user, :password, :timeout attr_accessor :format class << self @@ -90,6 +90,11 @@ module ActiveResource @password = password end + # Set the number of seconds after which HTTP requests to the remote service should time out. + def timeout=(timeout) + @timeout = timeout + end + # Execute a GET request. # Used to get (find) resources. def get(path, headers = {}) @@ -167,18 +172,19 @@ module ActiveResource http = Net::HTTP.new(@site.host, @site.port) http.use_ssl = @site.is_a?(URI::HTTPS) http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl + http.read_timeout = @timeout if @timeout # If timeout is not set, the default Net::HTTP timeout (60s) is used. http end def default_header @default_header ||= { 'Content-Type' => format.mime_type } end - + # Builds headers for request to remote service. def build_request_headers(headers) authorization_header.update(default_header).update(headers) end - + # Sets authorization header def authorization_header (@user || @password ? { 'Authorization' => 'Basic ' + ["#{@user}:#{ @password}"].pack('m').delete("\r\n") } : {}) |