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.1'
Instance Attribute Summary collapse
-
#default_path ⇒ Object
Default path for the request.
-
#host ⇒ Object
readonly
Host for the Net:HTTP connection.
-
#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.
-
#port ⇒ Object
readonly
Port for the Net:HTTP connection.
-
#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'
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/persistent_http.rb', line 99 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 @default_path = [:default_path] @host = [:host] @port = [:port] url = [:url] if url url = URI.parse(url) if url.kind_of? String @default_path ||= url.request_uri @host ||= url.host @port ||= url.port end @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
#default_path ⇒ Object
Default path for the request
80 81 82 |
# File 'lib/persistent_http.rb', line 80 def default_path @default_path end |
#host ⇒ Object (readonly)
Host for the Net:HTTP connection
72 73 74 |
# File 'lib/persistent_http.rb', line 72 def host @host end |
#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 |
#port ⇒ Object (readonly)
Port for the Net:HTTP connection
76 77 78 |
# File 'lib/persistent_http.rb', line 76 def port @port 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.
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/persistent_http.rb', line 151 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.
164 165 166 |
# File 'lib/persistent_http.rb', line 164 def shutdown(timeout=10) @pool.close(timeout) end |