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.



25
26
27
28
29
# File 'lib/sshkit/backends/connection_pool.rb', line 25

def initialize
  self.idle_timeout = 30
  @mutex = Mutex.new
  @pool = {}
end

Instance Attribute Details

#idle_timeoutObject

Returns the value of attribute idle_timeout.



23
24
25
# File 'lib/sshkit/backends/connection_pool.rb', line 23

def idle_timeout
  @idle_timeout
end

Instance Method Details

#checkin(entry) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/sshkit/backends/connection_pool.rb', line 41

def checkin(entry)
  if idle_timeout
    prune_expired
    entry.expires_at = Time.now + idle_timeout
    @mutex.synchronize do
      @pool[entry.key] ||= []
      @pool[entry.key] << entry
    end
  end
end

#checkout(*new_connection_args, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/sshkit/backends/connection_pool.rb', line 31

def checkout(*new_connection_args, &block)
  entry = nil
  key = new_connection_args.to_s
  if idle_timeout
    prune_expired
    entry = find_live_entry(key)
  end
  entry || create_new_entry(new_connection_args, key, &block)
end

#close_connectionsObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/sshkit/backends/connection_pool.rb', line 52

def close_connections
  @mutex.synchronize do
    @pool.values.flatten.map(&:connection).uniq.each do |conn|
      if conn.respond_to?(:closed?) && conn.respond_to?(:close)
        conn.close unless conn.closed?
      end
    end
    @pool.clear
  end
end

#flush_connectionsObject



63
64
65
# File 'lib/sshkit/backends/connection_pool.rb', line 63

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