Class: RightSupport::Net::LB::Sticky

Inherits:
Base show all
Defined in:
lib/right_support/net/lb/sticky.rb

Overview

Implementation concepts: Create a policy that selects an endpoint and sticks with it.

The policy should:

  • iterate through each endpoint until a valid endpoint is found;

  • continue returning the same endpoint until it is no longer valid;

  • re-iterate through each endpoint when it’s endpoint loses validity;

  • return an Exception if it performs a complete iteration though each endpoint and finds none valid;

Instance Method Summary collapse

Methods inherited from Base

#good, #health_check

Constructor Details

#initialize(options = {}) ⇒ Sticky

Returns a new instance of Sticky.



34
35
36
# File 'lib/right_support/net/lb/sticky.rb', line 34

def initialize(options = {})
  super
end

Instance Method Details

#bad(endpoint, t0, t1) ⇒ Object



56
57
58
59
60
61
# File 'lib/right_support/net/lb/sticky.rb', line 56

def bad(endpoint, t0, t1)
  # increment from bad endpoint index to unstick. this is only meaningful if
  # there are multiple endpoints.
  @counter += 1
  nil
end

#nextObject



51
52
53
54
# File 'lib/right_support/net/lb/sticky.rb', line 51

def next
  # note that counter is not incremented here; hence stickyness.
  [ @endpoints[@counter % @endpoints.size], false ] unless @endpoints.empty?
end

#set_endpoints(endpoints) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/right_support/net/lb/sticky.rb', line 38

def set_endpoints(endpoints)
  unless @endpoints.empty?
    last_chosen = @endpoints[@counter % @endpoints.size]
    @endpoints = []
    if endpoints.include?(last_chosen)
      @endpoints << last_chosen
      @counter = 0
    end
  end
  @endpoints |= endpoints  # a union of endpoints
  true
end