Class: PersistentHTTP

Inherits:
Object
  • Object
show all
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 send_get_message
  response = @http.request
  ... Handle response as you would a normal Net::HTTPResponse ...
end

def send_post_message
  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.3'

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

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(options={})
  @name            = options[:name]            || 'PersistentHTTP'
  @idle_timeout    = options[:idle_timeout]    || 10
  @logger          = options[:logger]
  @pool_timeout    = options[:pool_timeout]
  @pool_size       = options[:pool_size]       || 1
  @warn_timeout    = options[:warn_timeout]    || 0.5
  @default_path    = options[:default_path]
  @host            = options[:host]
  @port            = options[:port]
  url              = options[: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(options)
  end
end

Instance Attribute Details

#default_pathObject

Default path for the request



80
81
82
# File 'lib/persistent_http.rb', line 80

def default_path
  @default_path
end

#hostObject (readonly)

Host for the Net:HTTP connection



72
73
74
# File 'lib/persistent_http.rb', line 72

def host
  @host
end

#idle_timeoutObject (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

#loggerObject

Logger for message logging.



50
51
52
# File 'lib/persistent_http.rb', line 50

def logger
  @logger
end

#nameObject (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_sizeObject

Return the size of the connection pool



63
64
65
# File 'lib/persistent_http.rb', line 63

def pool_size
  @pool_size
end

#pool_timeoutObject

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

#portObject (readonly)

Port for the Net:HTTP connection



76
77
78
# File 'lib/persistent_http.rb', line 76

def port
  @port
end

#warn_timeoutObject (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, options = {}, &block)
  @pool.with_connection do |connection|
    begin
      connection.request req, options, &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