lib/elasticsearch/transport/transport/connections/selector.rb in elasticsearch-transport-7.1.0 vs lib/elasticsearch/transport/transport/connections/selector.rb in elasticsearch-transport-7.2.0
- old
+ new
@@ -59,21 +59,33 @@
# "Round-robin" selector strategy (default).
#
class RoundRobin
include Base
+ # @option arguments [Connections::Collection] :connections Collection with connections.
+ #
+ def initialize(arguments = {})
+ super
+ @mutex = Mutex.new
+ @current = nil
+ end
+
# Returns the next connection from the collection, rotating them in round-robin fashion.
#
# @return [Connections::Connection]
#
def select(options={})
- # On Ruby 1.9, Array#rotate could be used instead
- @current = !defined?(@current) || @current.nil? ? 0 : @current+1
- @current = 0 if @current >= connections.size
- connections[@current]
+ @mutex.synchronize do
+ conns = connections
+ if @current && (@current < conns.size-1)
+ @current += 1
+ else
+ @current = 0
+ end
+ conns[@current]
+ end
end
end
-
end
end
end
end
end