Class: Weave::ConnectionPool
- Inherits:
-
Object
- Object
- Weave::ConnectionPool
- Defined in:
- lib/weave.rb
Overview
A pool of SSH connections. Operations over the pool may be performed in serial or in parallel.
Instance Method Summary collapse
-
#disconnect! ⇒ Object
Disconnect all open connections.
-
#execute(options = {}, &block) ⇒ Object
Run a command over the connection pool.
-
#initialize(host_list) ⇒ ConnectionPool
constructor
A new instance of ConnectionPool.
Constructor Details
#initialize(host_list) ⇒ ConnectionPool
Returns a new instance of ConnectionPool.
56 57 58 |
# File 'lib/weave.rb', line 56 def initialize(host_list) @connections = host_list.reduce({}) { |pool, host| pool.merge(host => LazyConnection.new(host)) } end |
Instance Method Details
#disconnect! ⇒ Object
Disconnect all open connections.
86 |
# File 'lib/weave.rb', line 86 def disconnect!() @connections.each_value(&:disconnect) end |
#execute(options = {}, &block) ⇒ Object
Run a command over the connection pool. The block is evaluated in the context of LazyConnection.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/weave.rb', line 68 def execute( = {}, &block) [:num_threads] ||= DEFAULT_THREAD_POOL_SIZE if [:serial] @connections.each_key { |host| @connections[host].self_eval &block } elsif [:batch_by] @connections.each_key.each_slice([:batch_by]) do |batch| Weave.with_thread_pool(batch, [:num_threads]) do |host, mutex| @connections[host].self_eval mutex, &block end end else Weave.with_thread_pool(@connections.keys, [:num_threads]) do |host, mutex| @connections[host].self_eval mutex, &block end end end |