Class: Makara::Strategies::RoundRobin
- Inherits:
-
Abstract
- Object
- Abstract
- Makara::Strategies::RoundRobin
show all
- Defined in:
- lib/makara/strategies/round_robin.rb
Instance Attribute Summary
Attributes inherited from Abstract
#pool
Instance Method Summary
collapse
Methods inherited from Abstract
#initialize
Instance Method Details
#connection_added(wrapper) ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/makara/strategies/round_robin.rb', line 9
def connection_added(wrapper)
wrapper._makara_weight.times{ @weighted_connections << wrapper }
if should_shuffle?
@weighted_connections.shuffle!
@current_idx = rand(@weighted_connections.length)
end
end
|
#current ⇒ Object
21
22
23
|
# File 'lib/makara/strategies/round_robin.rb', line 21
def current
safe_value(@current_idx)
end
|
#init ⇒ Object
4
5
6
7
|
# File 'lib/makara/strategies/round_robin.rb', line 4
def init
@current_idx = 0
@weighted_connections = []
end
|
#next ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# 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)
return safe_value(idx, true) if idx == @current_idx
end while safe_value(idx).nil?
safe_value(idx, true)
end
|
#next_index(idx) ⇒ Object
next index within the bounds of the connections array loop around when the end is hit
45
46
47
48
49
|
# 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) ⇒ Object
return the connection if it’s not blacklisted otherwise return nil optionally, store the position and context we’re returning
54
55
56
57
58
59
60
61
62
63
64
|
# 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? ⇒ Boolean
stub in test mode to ensure consistency
67
68
69
|
# File 'lib/makara/strategies/round_robin.rb', line 67
def should_shuffle?
true
end
|
#single_one? ⇒ Boolean
71
72
73
|
# File 'lib/makara/strategies/round_robin.rb', line 71
def single_one?
false
end
|