Class: Makara::Strategies::PriorityFailover

Inherits:
Abstract
  • Object
show all
Defined in:
lib/makara/strategies/priority_failover.rb

Instance Attribute Summary

Attributes inherited from Abstract

#pool

Instance Method Summary collapse

Methods inherited from Abstract

#initialize

Constructor Details

This class inherits a constructor from Makara::Strategies::Abstract

Instance Method Details

#connection_added(wrapper) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/makara/strategies/priority_failover.rb', line 9

def connection_added(wrapper)
  # insert in weighted order
  @weighted_connections.each_with_index do |con, index|
    if wrapper._makara_weight > con._makara_weight
      @weighted_connections.insert(index, wrapper)
      return
    end
  end

  # else at end
  @weighted_connections << wrapper
end

#currentObject



22
23
24
# File 'lib/makara/strategies/priority_failover.rb', line 22

def current
  safe_value(@current_idx)
end

#initObject



4
5
6
7
# File 'lib/makara/strategies/priority_failover.rb', line 4

def init
  @current_idx = 0
  @weighted_connections = []
end

#nextObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/makara/strategies/priority_failover.rb', line 26

def next
  @weighted_connections.each_with_index do |con, index|
    check = safe_value(index)
    next unless check

    @current_idx = index
    return check
  end

  nil
end

#safe_value(idx) ⇒ Object

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



41
42
43
44
45
46
47
# File 'lib/makara/strategies/priority_failover.rb', line 41

def safe_value(idx)
  con = @weighted_connections[idx]
  return nil unless con
  return nil if con._makara_blacklisted?

  con
end