Class: Elastic::Transport::Transport::Connections::Collection
- Inherits:
-
Object
- Object
- Elastic::Transport::Transport::Connections::Collection
- Includes:
- Enumerable
- Defined in:
- lib/elastic/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
-
#selector ⇒ Object
readonly
Returns the value of attribute selector.
Instance Method Summary collapse
-
#add(connections) ⇒ self
Add connection(s) to the collection.
-
#all ⇒ Array
Returns an Array of all connections, both dead and alive.
-
#connections ⇒ Array
(also: #alive)
Returns an Array of alive connections.
-
#dead ⇒ Array
Returns an Array of dead connections.
- #each(&block) ⇒ Object
-
#get_connection(options = {}) ⇒ Connection
Returns a connection.
-
#hosts ⇒ Array
Returns an Array of hosts information in this collection as Hashes.
-
#initialize(arguments = {}) ⇒ Collection
constructor
A new instance of Collection.
-
#remove(connections) ⇒ self
Remove connection(s) from the collection.
- #size ⇒ Object
- #slice(*args) ⇒ Object (also: #[])
Constructor Details
#initialize(arguments = {}) ⇒ Collection
Returns a new instance of Collection.
39 40 41 42 43 |
# File 'lib/elastic/transport/transport/connections/collection.rb', line 39 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
#selector ⇒ Object (readonly)
Returns the value of attribute selector.
33 34 35 |
# File 'lib/elastic/transport/transport/connections/collection.rb', line 33 def selector @selector end |
Instance Method Details
#add(connections) ⇒ self
Add connection(s) to the collection
107 108 109 110 |
# File 'lib/elastic/transport/transport/connections/collection.rb', line 107 def add(connections) @connections += Array(connections).to_a self end |
#all ⇒ Array
Returns an Array of all connections, both dead and alive
74 75 76 |
# File 'lib/elastic/transport/transport/connections/collection.rb', line 74 def all @connections end |
#connections ⇒ Array Also known as: alive
Returns an Array of alive connections.
57 58 59 |
# File 'lib/elastic/transport/transport/connections/collection.rb', line 57 def connections @connections.reject { |c| c.dead? } end |
#dead ⇒ Array
Returns an Array of dead connections.
66 67 68 |
# File 'lib/elastic/transport/transport/connections/collection.rb', line 66 def dead @connections.select { |c| c.dead? } end |
#each(&block) ⇒ Object
89 90 91 |
# File 'lib/elastic/transport/transport/connections/collection.rb', line 89 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.
85 86 87 |
# File 'lib/elastic/transport/transport/connections/collection.rb', line 85 def get_connection(={}) selector.select() || @connections.min_by(&:failures) end |
#hosts ⇒ Array
Returns an Array of hosts information in this collection as Hashes.
49 50 51 |
# File 'lib/elastic/transport/transport/connections/collection.rb', line 49 def hosts @connections.to_a.map { |c| c.host } end |
#remove(connections) ⇒ self
Remove connection(s) from the collection
117 118 119 120 |
# File 'lib/elastic/transport/transport/connections/collection.rb', line 117 def remove(connections) @connections -= Array(connections).to_a self end |
#size ⇒ Object
98 99 100 |
# File 'lib/elastic/transport/transport/connections/collection.rb', line 98 def size connections.size end |
#slice(*args) ⇒ Object Also known as: []
93 94 95 |
# File 'lib/elastic/transport/transport/connections/collection.rb', line 93 def slice(*args) connections.slice(*args) end |