class Elasticsearch::Transport::Transport::Connections::Collection

Wraps the collection of connections for the transport object as an Enumerable object.

@see Base#connections @see Selector::Base#select @see Connection

Constants

DEFAULT_SELECTOR

Attributes

selector[R]

Public Class Methods

new(arguments={}) click to toggle source

@option arguments [Array] :connections An array of {Connection} objects. @option arguments [Constant] :selector_class The class to be used as a connection selector strategy. @option arguments [Object] :selector The selector strategy object.

# File lib/elasticsearch/transport/transport/connections/collection.rb, line 23
def initialize(arguments={})
  selector_class = arguments[:selector_class] || DEFAULT_SELECTOR
  @connections   = arguments[:connections]    || []
  @selector      = arguments[:selector]       || selector_class.new(arguments.merge(:connections => self))
end

Public Instance Methods

[](*args)
Alias for: slice
add(connections) click to toggle source

Add connection(s) to the collection

@param connections [Connection,Array] A connection or an array of connections to add @return [self]

# File lib/elasticsearch/transport/transport/connections/collection.rb, line 94
def add(connections)
  @connections += Array(connections).to_a
  self
end
alive()
Alias for: connections
all() click to toggle source

Returns an Array of all connections, both dead and alive

@return [Array]

# File lib/elasticsearch/transport/transport/connections/collection.rb, line 58
def all
  @connections
end
connections() click to toggle source

Returns an Array of alive connections.

@return [Array]

# File lib/elasticsearch/transport/transport/connections/collection.rb, line 41
def connections
  @connections.reject { |c| c.dead? }
end
Also aliased as: alive
dead() click to toggle source

Returns an Array of dead connections.

@return [Array]

# File lib/elasticsearch/transport/transport/connections/collection.rb, line 50
def dead
  @connections.select { |c| c.dead? }
end
each(&block) click to toggle source
# File lib/elasticsearch/transport/transport/connections/collection.rb, line 76
def each(&block)
  connections.each(&block)
end
get_connection(options={}) click to toggle source

Returns a connection.

If there are no alive connections, resurrects a connection with least failures. Delegates to selector’s ‘#select` method to get the connection.

@return [Connection]

# File lib/elasticsearch/transport/transport/connections/collection.rb, line 69
def get_connection(options={})
  if connections.empty? && dead_connection = dead.sort { |a,b| a.failures <=> b.failures }.first
    dead_connection.alive!
  end
  selector.select(options)
end
hosts() click to toggle source

Returns an Array of hosts information in this collection as Hashes.

@return [Array]

# File lib/elasticsearch/transport/transport/connections/collection.rb, line 33
def hosts
  @connections.to_a.map { |c| c.host }
end
remove(connections) click to toggle source

Remove connection(s) from the collection

@param connections [Connection,Array] A connection or an array of connections to remove @return [self]

# File lib/elasticsearch/transport/transport/connections/collection.rb, line 104
def remove(connections)
  @connections -= Array(connections).to_a
  self
end
size() click to toggle source
# File lib/elasticsearch/transport/transport/connections/collection.rb, line 85
def size
  connections.size
end
slice(*args) click to toggle source
# File lib/elasticsearch/transport/transport/connections/collection.rb, line 80
def slice(*args)
  connections.slice(*args)
end
Also aliased as: []