Class: Rmega::ConnPool::ConnectionManager
- Inherits:
-
Object
- Object
- Rmega::ConnPool::ConnectionManager
- Includes:
- MonitorMixin
- Defined in:
- lib/rmega/conn_pool.rb
Overview
Constant Summary collapse
- STALE_AFTER =
if a client wasn’t used within this time range it gets removed from the cache and the connection closed. This helps to make sure there are no memory leaks.
180
- KEEP_ALIVE_TIMEOUT =
Seconds to reuse the connection of the previous request. If the idle time is less than this Keep-Alive Timeout, Net::HTTP reuses the TCP/IP socket used by the previous communication. Source: Ruby docs
300
Instance Attribute Summary collapse
-
#clients_store ⇒ Object
KEEP_ALIVE_TIMEOUT vs STALE_AFTER STALE_AFTER - how long an Net::HTTP client object is cached in ruby KEEP_ALIVE_TIMEOUT - how long that client keeps TCP/IP socket open.
-
#last_used ⇒ Object
KEEP_ALIVE_TIMEOUT vs STALE_AFTER STALE_AFTER - how long an Net::HTTP client object is cached in ruby KEEP_ALIVE_TIMEOUT - how long that client keeps TCP/IP socket open.
Instance Method Summary collapse
-
#close_connections! ⇒ Object
close connections for each client.
- #get_client(uri, options) ⇒ Object
-
#initialize(*args) ⇒ ConnectionManager
constructor
A new instance of ConnectionManager.
- #stale? ⇒ Boolean
Constructor Details
#initialize(*args) ⇒ ConnectionManager
Returns a new instance of ConnectionManager.
49 50 51 52 53 |
# File 'lib/rmega/conn_pool.rb', line 49 def initialize(*args) super self.clients_store = {} self.last_used = Time.now end |
Instance Attribute Details
#clients_store ⇒ Object
KEEP_ALIVE_TIMEOUT vs STALE_AFTER STALE_AFTER - how long an Net::HTTP client object is cached in ruby KEEP_ALIVE_TIMEOUT - how long that client keeps TCP/IP socket open.
47 48 49 |
# File 'lib/rmega/conn_pool.rb', line 47 def clients_store @clients_store end |
#last_used ⇒ Object
KEEP_ALIVE_TIMEOUT vs STALE_AFTER STALE_AFTER - how long an Net::HTTP client object is cached in ruby KEEP_ALIVE_TIMEOUT - how long that client keeps TCP/IP socket open.
47 48 49 |
# File 'lib/rmega/conn_pool.rb', line 47 def last_used @last_used end |
Instance Method Details
#close_connections! ⇒ Object
close connections for each client
86 87 88 89 90 91 |
# File 'lib/rmega/conn_pool.rb', line 86 def close_connections! synchronize do clients_store.values.each(&:finish) self.clients_store = {} end end |
#get_client(uri, options) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/rmega/conn_pool.rb', line 55 def get_client(uri, ) synchronize do # refresh the last time a client was used, # this prevents the client from becoming stale self.last_used = Time.now # we use params as a cache key for clients. # 2 connections to the same host but with different # options are going to use different HTTP clients params = [uri.host, uri.port, ] client = clients_store[params] return client if client client = ::Net::HTTP.new(uri.host, uri.port) client.keep_alive_timeout = KEEP_ALIVE_TIMEOUT # set SSL to true if a scheme is https client.use_ssl = uri.scheme == "https" # open connection client.start # cache the client clients_store[params] = client client end end |
#stale? ⇒ Boolean
93 94 95 |
# File 'lib/rmega/conn_pool.rb', line 93 def stale? Time.now - last_used > STALE_AFTER end |