Module: HttpConnectionPool::Connectable

Defined in:
lib/http_connection_pool/connectable.rb

Overview

Mixin that gives any class a ‘connection_pool` class-level accessor and a `with_connection` method for safely borrowing a persistent HTTP client.

── Include (instance + class API) ──────────────────────────────────────────

class GithubClient
  include HttpConnectionPool::Connectable

  self.base_url     = 'https://api.github.com'
  self.pool_size    = 10
  self.pool_timeout = 3.0
  self.pool_options = { headers: { 'Authorization' => "Bearer #{ENV['GITHUB_TOKEN']}" } }

  def user()
    with_connection { |conn| conn.get("/users/#{}").parse }
  end
end

── Subclassing ─────────────────────────────────────────────────────────────

Subclasses inherit base_url, pool_size, pool_timeout, and pool_options from their parent. Each class that declares its own pool_options receives its own isolated pool (keyed by origin + options digest), so a subclass that adds credentials never shares connections with the base class or siblings:

class AdminClient < GithubClient
  self.pool_options = { headers: { 'Authorization' => "Bearer #{ENV['ADMIN_TOKEN']}" } }
end

── Extend (class-level methods only) ───────────────────────────────────────

module GithubAPI
  extend HttpConnectionPool::Connectable

  self.base_url = 'https://api.github.com'

  def self.user()
    with_connection { |conn| conn.get("/users/#{}").parse }
  end
end

Defined Under Namespace

Modules: ClassMethods, PoolAccessors

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

Called when the module is extended onto an object/module.



55
56
57
58
# File 'lib/http_connection_pool/connectable.rb', line 55

def self.extended(base)
  base.extend(ClassMethods)
  base.extend(PoolAccessors)
end

.included(base) ⇒ Object

Called when the module is included into a class.



49
50
51
52
# File 'lib/http_connection_pool/connectable.rb', line 49

def self.included(base)
  base.extend(ClassMethods)
  base.extend(PoolAccessors)
end

Instance Method Details

#connection_poolObject



166
167
168
# File 'lib/http_connection_pool/connectable.rb', line 166

def connection_pool
  self.class.connection_pool
end

#connection_pool_statsObject



171
172
173
# File 'lib/http_connection_pool/connectable.rb', line 171

def connection_pool_stats
  self.class.connection_pool_stats
end

#with_connectionObject



161
162
163
# File 'lib/http_connection_pool/connectable.rb', line 161

def with_connection(&)
  self.class.with_connection(&)
end