Class: LeanPool::HTTPPool
- Inherits:
-
Object
- Object
- LeanPool::HTTPPool
- Defined in:
- lib/lean_pool/http_pool.rb
Overview
Uses Ruby's standard Net::HTTP library. Connections are reused when possible but removed automatically if errors occur.
HTTP connection pool implementation using Net::HTTP.
HTTPPool provides a convenient wrapper around Pool for managing HTTP/1 connections. It automatically handles connection lifecycle, request/response processing, and connection reuse.
This is an example implementation demonstrating how to use LeanPool for HTTP connections. For production use with more advanced features (HTTP/2, connection keep-alive optimization, etc.), consider using libraries like HTTP.rb or Faraday with connection pooling.
Features:
- Automatic connection management and reuse
- Support for both HTTP and HTTPS (SSL)
- GET and POST request methods
- Custom headers and timeouts
- Automatic connection removal on errors
Instance Method Summary collapse
-
#get(path, headers: {}, timeout: nil) ⇒ Hash
Perform a GET HTTP request.
-
#initialize(host, port, size: 10, timeout: 5.0, use_ssl: false) ⇒ HTTPPool
constructor
Initialize a new HTTP connection pool.
-
#post(path, body: "", headers: {}, timeout: nil) ⇒ Hash
Perform a POST HTTP request.
-
#shutdown ⇒ void
Shutdown the HTTP pool gracefully.
-
#stats ⇒ Hash
Get HTTP pool statistics.
Constructor Details
#initialize(host, port, size: 10, timeout: 5.0, use_ssl: false) ⇒ HTTPPool
Initialize a new HTTP connection pool.
Creates a new pool for managing HTTP connections to the specified host and port. Connections are created lazily on-demand and reused when possible.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/lean_pool/http_pool.rb', line 64 def initialize(host, port, size: 10, timeout: 5.0, use_ssl: false) @host = host @port = port @use_ssl = use_ssl @pool = Pool.new( size: size, timeout: timeout, pool_state: { host: host, port: port, use_ssl: use_ssl } ) do |state| http = Net::HTTP.new(state[:host], state[:port]) http.use_ssl = state[:use_ssl] if state[:use_ssl] http end end |
Instance Method Details
#get(path, headers: {}, timeout: nil) ⇒ Hash
Perform a GET HTTP request.
Retrieves a connection from the pool, performs a GET request to the specified path, and returns the response. The connection is automatically returned to the pool unless an error occurs, in which case it's removed.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/lean_pool/http_pool.rb', line 109 def get(path, headers: {}, timeout: nil) @pool.checkout(timeout: timeout) do |http| http.start unless http.started? request = Net::HTTP::Get.new(path) headers.each { |k, v| request[k] = v } response = http.request(request) { status: response.code.to_i, body: response.body, headers: response.to_hash } end rescue => e # Remove connection if it's broken { error: e., remove: true } end |
#post(path, body: "", headers: {}, timeout: nil) ⇒ Hash
Perform a POST HTTP request.
Retrieves a connection from the pool, performs a POST request to the specified path with the given body, and returns the response. The connection is automatically returned to the pool unless an error occurs, in which case it's removed.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/lean_pool/http_pool.rb', line 162 def post(path, body: "", headers: {}, timeout: nil) @pool.checkout(timeout: timeout) do |http| http.start unless http.started? request = Net::HTTP::Post.new(path) headers.each { |k, v| request[k] = v } request.body = body response = http.request(request) { status: response.code.to_i, body: response.body, headers: response.to_hash } end rescue => e { error: e., remove: true } end |
#shutdown ⇒ void
This method returns an undefined value.
Shutdown the HTTP pool gracefully.
Closes all HTTP connections in the pool and prevents new requests. All connections are properly finished before being removed from the pool.
200 201 202 203 204 |
# File 'lib/lean_pool/http_pool.rb', line 200 def shutdown @pool.shutdown do |http| http.finish if http.started? end end |
#stats ⇒ Hash
Get HTTP pool statistics.
Returns statistics about the underlying connection pool, including size, available connections, in-use connections, and total connections.
189 190 191 |
# File 'lib/lean_pool/http_pool.rb', line 189 def stats @pool.stats end |