Class: Weave::ConnectionPool

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(host_list) ⇒ ConnectionPool

Returns a new instance of ConnectionPool.

Parameters:

  • host_list (Array)

    the array of hosts, of the form user@host



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.

Parameters:

  • options (Hash) (defaults to: {})

    the various knobs

Options Hash (options):

  • :num_threads (Fixnum)

    the number of concurrent threads to use to process this command. Defaults to DEFAULT_THREAD_POOL_SIZE.

  • :serial (Boolean)

    whether to process the command for each connection one at a time.

  • :batch_by (Fixnum)

    if set, group the connections into batches of no more than this value and fully process each batch before starting the next one.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/weave.rb', line 68

def execute(options = {}, &block)
  options[:num_threads] ||= DEFAULT_THREAD_POOL_SIZE
  if options[:serial]
    @connections.each_key { |host| @connections[host].self_eval &block }
  elsif options[:batch_by]
    @connections.each_key.each_slice(options[:batch_by]) do |batch|
      Weave.with_thread_pool(batch, options[:num_threads]) do |host, mutex|
        @connections[host].self_eval mutex, &block
      end
    end
  else
    Weave.with_thread_pool(@connections.keys, options[:num_threads]) do |host, mutex|
      @connections[host].self_eval mutex, &block
    end
  end
end