Class: Roundrobin

Inherits:
Object
  • Object
show all
Defined in:
lib/roundrobin.rb,
lib/roundrobin/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Instance Method Summary collapse

Constructor Details

#initialize(redis_conn = nil) ⇒ Roundrobin

Returns a new instance of Roundrobin.



7
8
9
# File 'lib/roundrobin.rb', line 7

def initialize(redis_conn = nil)
  @redis = redis_conn.nil? ? Redis.new : Redis.new(url: redis_conn)
end

Instance Method Details

#next(candidates) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/roundrobin.rb', line 11

def next(candidates)
  return nil unless candidates.is_a?(Array)
  identifier = get_hash(candidates)
  iterator = @redis.get(identifier)
  if iterator.nil?
    iterator   = -1 
  else
    iterator = iterator.to_i
  end
  iterator += 1
  iterator = 0 if iterator >= candidates.length
  @redis.set(identifier, iterator)
  candidates[iterator]
end