Class: SSHKit::Backend::ConnectionPool
- Inherits:
-
Object
- Object
- SSHKit::Backend::ConnectionPool
- Defined in:
- lib/sshkit/backends/connection_pool.rb
Defined Under Namespace
Classes: Entry
Instance Attribute Summary collapse
-
#idle_timeout ⇒ Object
Returns the value of attribute idle_timeout.
Instance Method Summary collapse
- #checkin(entry) ⇒ Object
- #checkout(*new_connection_args, &block) ⇒ Object
- #flush_connections ⇒ Object
-
#initialize ⇒ ConnectionPool
constructor
A new instance of ConnectionPool.
Constructor Details
#initialize ⇒ ConnectionPool
Returns a new instance of ConnectionPool.
11 12 13 14 15 |
# File 'lib/sshkit/backends/connection_pool.rb', line 11 def initialize self.idle_timeout = 30 @mutex = Mutex.new @pool = {} end |
Instance Attribute Details
#idle_timeout ⇒ Object
Returns the value of attribute idle_timeout.
9 10 11 |
# File 'lib/sshkit/backends/connection_pool.rb', line 9 def idle_timeout @idle_timeout end |
Instance Method Details
#checkin(entry) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/sshkit/backends/connection_pool.rb', line 25 def checkin(entry) entry.expires_at = Time.now + idle_timeout if idle_timeout @mutex.synchronize do @pool[entry.key] ||= [] @pool[entry.key] << entry end end |
#checkout(*new_connection_args, &block) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/sshkit/backends/connection_pool.rb', line 17 def checkout(*new_connection_args, &block) # Optimization: completely bypass the pool if idle_timeout is zero. key = new_connection_args.to_s return create_new_entry(new_connection_args, key, &block) if idle_timeout == 0 find_live_entry(key) || create_new_entry(new_connection_args, key, &block) end |
#flush_connections ⇒ Object
33 34 35 |
# File 'lib/sshkit/backends/connection_pool.rb', line 33 def flush_connections @mutex.synchronize { @pool.clear } end |