Class: Seahorse::Client::NetHttp::ConnectionPool Private

Inherits:
Object
  • Object
show all
Defined in:
lib/seahorse/client/net_http/connection_pool.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

API:

  • private

Defined Under Namespace

Classes: ExtendedSession

Constant Summary collapse

OPTIONS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

API:

  • private

{
  http_proxy: nil,
  http_open_timeout: 15,
  http_read_timeout: 60,
  http_idle_timeout: 5,
  http_continue_timeout: 1,
  http_wire_trace: false,
  logger: nil,
  ssl_verify_peer: true,
  ssl_ca_bundle: nil,
  ssl_ca_directory: nil,
  ssl_ca_store: nil,
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ConnectionPool

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ConnectionPool.

API:

  • private



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/seahorse/client/net_http/connection_pool.rb', line 33

def initialize(options = {})
  OPTIONS.each_pair do |opt_name, default_value|
    value = options[opt_name].nil? ? default_value : options[opt_name]
    instance_variable_set("@#{opt_name}", value)
  end
  @pool_mutex = Mutex.new
  @pool = Hash.new do |pool, endpoint|
    pool[endpoint] = []
    pool[endpoint]
  end
end

Class Method Details

.for(options = {}) ⇒ ConnectionPool

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a connection pool constructed from the given options. Calling this method twice with the same options will return the same pool.

Parameters:

  • (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :http_proxy (URI::HTTP, String)

    A proxy to send requests through. Formatted like ‘proxy.com:123’.

  • :http_open_timeout (Float) — default: 15

    The number of seconds to wait when opening a HTTP session before rasing a ‘Timeout::Error`.

  • :http_read_timeout (Integer) — default: 60

    The default number of seconds to wait for response data. This value can safely be set per-request on the session yeidled by #session_for.

  • :http_idle_timeout (Float) — default: 5

    The number of seconds a connection is allowed to sit idble before it is considered stale. Stale connections are closed and removed from the pool before making a request.

  • :http_continue_timeout (Float) — default: 1

    The number of seconds to wait for a 100-continue response before sending the request body. This option has no effect unless the request has “Expect” header set to “100-continue”. Defaults to ‘nil` which disables this behaviour. This value can safely be set per request on the session yeidled by #session_for.

  • :http_wire_trace (Boolean) — default: false

    When ‘true`, HTTP debug output will be sent to the `:logger`.

  • :logger (Logger)

    Where debug output is sent. Defaults to ‘nil` when `:http_wire_trace` is `false`. Defaults to `Logger.new($stdout)` when `:http_wire_trace` is `true`.

  • :ssl_verify_peer (Boolean) — default: true

    When ‘true`, SSL peer certificates are verified when establishing a connection.

  • :ssl_ca_bundle (String)

    Full path to the SSL certificate authority bundle file that should be used when verifying peer certificates. If you do not pass ‘:ssl_ca_bundle` or `:ssl_ca_directory` the the system default will be used if available.

  • :ssl_ca_directory (String)

    Full path of the directory that contains the unbundled SSL certificate authority files for verifying peer certificates. If you do not pass ‘:ssl_ca_bundle` or `:ssl_ca_directory` the the system default will be used if available.

Returns:

API:

  • private



207
208
209
210
211
212
# File 'lib/seahorse/client/net_http/connection_pool.rb', line 207

def for options = {}
  options = pool_options(options)
  @pools_mutex.synchronize do
    @pools[options] ||= new(options)
  end
end

.poolsArray<ConnectionPool>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a list of of the constructed connection pools.

Returns:

  • Returns a list of of the constructed connection pools.

API:

  • private



216
217
218
# File 'lib/seahorse/client/net_http/connection_pool.rb', line 216

def pools
  @pools.values
end

Instance Method Details

#clean!nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Removes stale http sessions from the pool (that have exceeded the idle timeout).

Returns:

API:

  • private



123
124
125
126
# File 'lib/seahorse/client/net_http/connection_pool.rb', line 123

def clean!
  @pool_mutex.synchronize { _clean }
  nil
end

#empty!nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Closes and removes removes all sessions from the pool. If empty! is called while there are outstanding requests they may get checked back into the pool, leaving the pool in a non-empty state.

Returns:

API:

  • private



133
134
135
136
137
138
139
140
141
# File 'lib/seahorse/client/net_http/connection_pool.rb', line 133

def empty!
  @pool_mutex.synchronize do
    @pool.each_pair do |endpoint,sessions|
      sessions.each(&:finish)
    end
    @pool.clear
  end
  nil
end

#request(endpoint, request) {|net_http_response| ... } ⇒ nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Makes an HTTP request, yielding a Net::HTTPResponse object.

pool.request('http://domain', Net::HTTP::Get.new('/')) do |resp|
  puts resp.code # status code
  puts resp.to_h.inspect # dump the headers
  puts resp.body
end

Parameters:

  • The HTTP(S) endpoint to connect to (e.g. ‘domain.com’).

  • The request to make. This can be any request object from Net::HTTP (e.g. Net::HTTP::Get, Net::HTTP::POST, etc).

Yield Parameters:

  • net_http_response (Net::HTTPResponse)

Returns:

API:

  • private



70
71
72
73
74
# File 'lib/seahorse/client/net_http/connection_pool.rb', line 70

def request(endpoint, request, &block)
  session_for(endpoint) do |http|
    yield(http.request(request))
  end
end

#session_for(endpoint) {|session| ... } ⇒ nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • The HTTP(S) endpoint to connect to (e.g. ‘domain.com’).

Yield Parameters:

  • session (Net::HTTPSession)

Returns:

API:

  • private



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/seahorse/client/net_http/connection_pool.rb', line 82

def session_for(endpoint, &block)
  endpoint = remove_path_and_query(endpoint)
  session = nil

  # attempt to recycle an already open session
  @pool_mutex.synchronize do
    _clean
    session = @pool[endpoint].shift
  end

  begin
    session ||= start_session(endpoint)
    session.read_timeout = http_read_timeout
    session.continue_timeout = http_continue_timeout if
      session.respond_to?(:continue_timeout=)
    yield(session)
  rescue
    session.finish if session
    raise
  else
    # No error raised? Good, check the session into the pool.
    @pool_mutex.synchronize { @pool[endpoint] << session }
  end
  nil
end

#sizeInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the count of sessions currently in the pool, not counting those currently in use.

Returns:

  • Returns the count of sessions currently in the pool, not counting those currently in use.

API:

  • private



110
111
112
113
114
115
116
117
118
# File 'lib/seahorse/client/net_http/connection_pool.rb', line 110

def size
  @pool_mutex.synchronize do
    size = 0
    @pool.each_pair do |endpoint,sessions|
      size += sessions.size
    end
    size
  end
end