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
  @connections = {}
  @monitor = Monitor.new
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

#create_or_reuse_connection(*new_connection_args, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sshkit/backends/connection_pool.rb', line 17

def create_or_reuse_connection(*new_connection_args, &block)
  # Optimization: completely bypass the pool if idle_timeout is zero.
  return yield(*new_connection_args) if idle_timeout == 0

  key = new_connection_args.to_s
  entry = find_and_reject_invalid(key) { |e| e.expired? || e.closed? }

  if entry.nil?
    entry = store_entry(key, yield(*new_connection_args))
  end

  entry.expires_at = Time.now + idle_timeout if idle_timeout
  entry.connection
end