Class: SSHKit::Backend::ConnectionPool

Inherits:
Object
  • Object
show all
Defined in:
lib/sshkit/backends/connection_pool.rb

Defined Under Namespace

Classes: Entry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnectionPool

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_timeoutObject

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_connectionsObject



33
34
35
# File 'lib/sshkit/backends/connection_pool.rb', line 33

def flush_connections
  @mutex.synchronize { @pool.clear }
end