Class: PersistentHTTP
- Inherits:
-
Object
- Object
- PersistentHTTP
- Defined in:
- lib/persistent_http.rb,
lib/persistent_http/version.rb,
lib/persistent_http/connection.rb
Overview
Simplified frontend for Net::HTTP
Example:
@http = PersistentHTTP::Connection.new(
:logger => Rails.logger,
:force_retry => true,
:url => 'https://www.example.com/echo/foo' # equivalent to :use_ssl => true, :host => 'www.example.com', :default_path => '/echo/foo'
)
def
response = @http.request
... Handle response as you would a normal Net::HTTPResponse ...
end
def
request = Net::HTTP::Post.new('/perform_service)
... Modify request as needed ...
response = @http.request(request)
... Handle response as you would a normal Net::HTTPResponse ...
end
Defined Under Namespace
Classes: Connection, Error
Constant Summary collapse
- VERSION =
'2.0.0'
Instance Attribute Summary collapse
-
#idle_timeout ⇒ Object
readonly
Connection will be renewed if it hasn’t been used in this amount of time.
-
#logger ⇒ Object
Logger for message logging.
-
#name ⇒ Object
readonly
A name for this connection.
-
#pool_size ⇒ Object
Return the size of the connection pool.
-
#pool_timeout ⇒ Object
Seconds to wait for an available connection before a Timeout::Error is raised.
-
#warn_timeout ⇒ Object
readonly
The threshold in seconds for checking out a connection at which a warning will be logged via the logger.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ PersistentHTTP
constructor
Creates a new PersistentHTTP.
-
#request(req = nil, options = {}, &block) ⇒ Object
Makes a request per
req
. -
#shutdown(timeout = 10) ⇒ Object
Shuts down all connections.
Constructor Details
#initialize(options = {}) ⇒ PersistentHTTP
Creates a new PersistentHTTP.
Set name
to keep your connections apart from everybody else’s. Not required currently, but highly recommended. Your library name should be good enough. This parameter will be required in a future version.
proxy
may be set to a URI::HTTP or :ENV to pick up proxy options from the environment. See proxy_from_env for details.
In order to use a URI for the proxy you’ll need to do some extra work beyond URI.parse:
proxy = URI.parse 'http://proxy.example'
proxy.user = 'AzureDiamond'
proxy.password = 'hunter2'
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/persistent_http.rb', line 87 def initialize(={}) @name = [:name] || 'PersistentHTTP' @idle_timeout = [:idle_timeout] || 10 @logger = [:logger] @pool_timeout = [:pool_timeout] @pool_size = [:pool_size] || 1 @warn_timeout = [:warn_timeout] || 0.5 @pool = GenePool.new(:name => name, :pool_size => @pool_size, :timeout => @pool_timeout, :warn_timeout => @warn_timeout, :idle_timeout => @idle_timeout, :close_proc => :finish, :logger => @logger) do @logger.debug { "#{name}: Creating connection" } if @logger Connection.new() end end |
Instance Attribute Details
#idle_timeout ⇒ Object (readonly)
Connection will be renewed if it hasn’t been used in this amount of time. Defaults to 10 seconds.
46 47 48 |
# File 'lib/persistent_http.rb', line 46 def idle_timeout @idle_timeout end |
#logger ⇒ Object
Logger for message logging.
50 51 52 |
# File 'lib/persistent_http.rb', line 50 def logger @logger end |
#name ⇒ Object (readonly)
A name for this connection. Allows you to keep your connections apart from everybody else’s.
55 56 57 |
# File 'lib/persistent_http.rb', line 55 def name @name end |
#pool_size ⇒ Object
Return the size of the connection pool
63 64 65 |
# File 'lib/persistent_http.rb', line 63 def pool_size @pool_size end |
#pool_timeout ⇒ Object
Seconds to wait for an available connection before a Timeout::Error is raised
59 60 61 |
# File 'lib/persistent_http.rb', line 59 def pool_timeout @pool_timeout end |
#warn_timeout ⇒ Object (readonly)
The threshold in seconds for checking out a connection at which a warning will be logged via the logger
68 69 70 |
# File 'lib/persistent_http.rb', line 68 def warn_timeout @warn_timeout end |
Instance Method Details
#request(req = nil, options = {}, &block) ⇒ Object
Makes a request per req
. If req
is nil a Net::HTTP::Get is performed against default_path
.
If a block is passed #request behaves like Net::HTTP#request (the body of the response will not have been read).
req
must be a Net::HTTPRequest subclass (see Net::HTTP for a list).
If there is an error and the request is idempontent according to RFC 2616 it will be retried automatically.
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/persistent_http.rb', line 129 def request(req = nil, = {}, &block) @pool.with_connection do |connection| begin connection.request req, , &block rescue Exception => e @pool.remove(connection) raise end end end |
#shutdown(timeout = 10) ⇒ Object
Shuts down all connections.
142 143 144 |
# File 'lib/persistent_http.rb', line 142 def shutdown(timeout=10) @pool.close(timeout) end |