Class: OpenSearch::Transport::Transport::Connections::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/opensearch/transport/transport/connections/collection.rb

Overview

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

Constant Summary collapse

DEFAULT_SELECTOR =
Selector::RoundRobin

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments = {}) ⇒ Collection

Returns a new instance of Collection.

Parameters:

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

    a customizable set of options

Options Hash (arguments):



48
49
50
51
52
# File 'lib/opensearch/transport/transport/connections/collection.rb', line 48

def initialize(arguments = {})
  selector_class = arguments[:selector_class] || DEFAULT_SELECTOR
  @connections   = arguments[:connections]    || []
  @selector      = arguments[:selector]       || selector_class.new(arguments.merge(connections: self))
end

Instance Attribute Details

#selectorObject (readonly)

Returns the value of attribute selector.



42
43
44
# File 'lib/opensearch/transport/transport/connections/collection.rb', line 42

def selector
  @selector
end

Instance Method Details

#add(connections) ⇒ self

Add connection(s) to the collection

Parameters:

  • connections (Connection, Array)

    A connection or an array of connections to add

Returns:

  • (self)


116
117
118
119
# File 'lib/opensearch/transport/transport/connections/collection.rb', line 116

def add(connections)
  @connections += Array(connections).to_a
  self
end

#allArray

Returns an Array of all connections, both dead and alive

Returns:

  • (Array)


83
84
85
# File 'lib/opensearch/transport/transport/connections/collection.rb', line 83

def all
  @connections
end

#connectionsArray Also known as: alive

Returns an Array of alive connections.

Returns:

  • (Array)


66
67
68
# File 'lib/opensearch/transport/transport/connections/collection.rb', line 66

def connections
  @connections.reject(&:dead?)
end

#deadArray

Returns an Array of dead connections.

Returns:

  • (Array)


75
76
77
# File 'lib/opensearch/transport/transport/connections/collection.rb', line 75

def dead
  @connections.select(&:dead?)
end

#each(&block) ⇒ Object



98
99
100
# File 'lib/opensearch/transport/transport/connections/collection.rb', line 98

def each(&block)
  connections.each(&block)
end

#get_connection(options = {}) ⇒ Connection

Returns a connection.

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

Returns:



94
95
96
# File 'lib/opensearch/transport/transport/connections/collection.rb', line 94

def get_connection(options = {})
  selector.select(options) || @connections.min_by(&:failures)
end

#hostsArray

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

Returns:

  • (Array)


58
59
60
# File 'lib/opensearch/transport/transport/connections/collection.rb', line 58

def hosts
  @connections.to_a.map(&:host)
end

#remove(connections) ⇒ self

Remove connection(s) from the collection

Parameters:

  • connections (Connection, Array)

    A connection or an array of connections to remove

Returns:

  • (self)


126
127
128
129
# File 'lib/opensearch/transport/transport/connections/collection.rb', line 126

def remove(connections)
  @connections -= Array(connections).to_a
  self
end

#sizeObject



107
108
109
# File 'lib/opensearch/transport/transport/connections/collection.rb', line 107

def size
  connections.size
end

#slice(*args) ⇒ Object Also known as: []



102
103
104
# File 'lib/opensearch/transport/transport/connections/collection.rb', line 102

def slice(*args)
  connections.slice(*args)
end