Module: HttpConnectionPool::Connectable::ClassMethods

Defined in:
lib/http_connection_pool/connectable.rb

Overview

Class-level behaviour available after include/extend.

Instance Method Summary collapse

Instance Method Details

#connection_poolHttpConnectionPool::Pool

Lazy accessor for the pool — initialised on first call and memoized so the request hot path avoids re-parsing the URL and re-allocating the options hash on every ‘with_connection`. The memo is dropped whenever the underlying pool has been closed (e.g. via the registry), so the next call transparently obtains a fresh pool.



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/http_connection_pool/connectable.rb', line 131

def connection_pool
  cached = @connection_pool
  return cached if cached && !cached.closed?

  @connection_pool = HttpConnectionPool::Registry.instance.pool_for(
    base_url,
    size: pool_size,
    timeout: pool_timeout,
    **pool_options
  )
end

#connection_pool_statsHash

Snapshot of the pool’s current stats.

Returns:

  • (Hash)


153
154
155
# File 'lib/http_connection_pool/connectable.rb', line 153

def connection_pool_stats
  connection_pool.stats
end

#release_connection_poolObject

Explicitly release and close this class’s pool. The next call to ‘with_connection` will open a fresh pool.



145
146
147
148
# File 'lib/http_connection_pool/connectable.rb', line 145

def release_connection_pool
  @connection_pool = nil
  HttpConnectionPool::Registry.instance.release(base_url, **pool_options)
end

#with_connection {|conn| ... } ⇒ Object

Borrow a connection from the pool and yield it to the block.

Yield Parameters:

  • conn (HTTP::Session)

    a persistent session pre-configured for base_url

Returns:

  • (Object)

    the return value of the block



120
121
122
# File 'lib/http_connection_pool/connectable.rb', line 120

def with_connection(&)
  connection_pool.with(&)
end