class Makara::Strategies::RoundRobin

Public Instance Methods

connection_added(wrapper) click to toggle source
# File lib/makara/strategies/round_robin.rb, line 9
def connection_added(wrapper)
  # the weight results in N references to the connection, not N connections
  wrapper._makara_weight.times{ @weighted_connections << wrapper }

  if should_shuffle?
    # randomize the connections so we don't get peaks and valleys of load
    @weighted_connections.shuffle!
    # then start at a random spot in the list
    @current_idx = rand(@weighted_connections.length)
  end
end
current() click to toggle source
# File lib/makara/strategies/round_robin.rb, line 21
def current
  safe_value(@current_idx)
end
init() click to toggle source
# File lib/makara/strategies/round_robin.rb, line 4
def init
  @current_idx = 0
  @weighted_connections = []
end
next() click to toggle source
# File lib/makara/strategies/round_robin.rb, line 25
def next
  return safe_value(0, true) if single_one?

  idx = @current_idx

  begin
    idx = next_index(idx)

    # if we've looped all the way around, return our safe value
    return safe_value(idx, true) if idx == @current_idx

  # while our current safe value is dangerous
  end while safe_value(idx).nil?

  # store our current spot and return our safe value
  safe_value(idx, true)
end
next_index(idx) click to toggle source

next index within the bounds of the connections array loop around when the end is hit

# File lib/makara/strategies/round_robin.rb, line 45
def next_index(idx)
  idx = idx + 1
  idx = 0 if idx >= @weighted_connections.length
  idx
end
safe_value(idx, stick = false) click to toggle source

return the connection if it's not blacklisted otherwise return nil optionally, store the position and context we're returning

# File lib/makara/strategies/round_robin.rb, line 54
def safe_value(idx, stick = false)
  con = @weighted_connections[idx]
  return nil unless con
  return nil if con._makara_blacklisted?

  if stick
    @current_idx = idx
  end

  con
end
should_shuffle?() click to toggle source

stub in test mode to ensure consistency

# File lib/makara/strategies/round_robin.rb, line 67
def should_shuffle?
  true
end
single_one?() click to toggle source
# File lib/makara/strategies/round_robin.rb, line 71
def single_one?
  false
end