Class: RightSupport::Net::LB::Sticky
- 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
- #bad(endpoint, t0, t1) ⇒ Object
- #good(endpoint, t0, t1) ⇒ Object
- #health_check(endpoint) ⇒ Object
-
#initialize(options = {}) ⇒ Sticky
constructor
A new instance of Sticky.
- #next ⇒ Object
- #set_endpoints(endpoints) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Sticky
Returns a new instance of Sticky.
34 35 36 37 38 |
# File 'lib/right_support/net/lb/sticky.rb', line 34 def initialize( = {}) @health_check = .delete(:health_check) @endpoints = [] @counter = rand(0xffff) end |
Instance Method Details
#bad(endpoint, t0, t1) ⇒ Object
60 61 62 |
# File 'lib/right_support/net/lb/sticky.rb', line 60 def bad(endpoint, t0, t1) @counter += 1 end |
#good(endpoint, t0, t1) ⇒ Object
56 57 58 |
# File 'lib/right_support/net/lb/sticky.rb', line 56 def good(endpoint, t0, t1) #no-op; round robin does not care about failures end |
#health_check(endpoint) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/right_support/net/lb/sticky.rb', line 64 def health_check(endpoint) t0 = Time.now result = @health_check.call(endpoint) t1 = Time.now if result return true else @counter += 1 return false end rescue Exception => e t1 = Time.now @counter += 1 raise end |
#next ⇒ Object
52 53 54 |
# File 'lib/right_support/net/lb/sticky.rb', line 52 def next [ @endpoints[@counter % @endpoints.size], false ] unless @endpoints.empty? end |
#set_endpoints(endpoints) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/right_support/net/lb/sticky.rb', line 40 def set_endpoints(endpoints) unless @endpoints.empty? last_chosen = self.next.first @endpoints = [] if endpoints.include?(last_chosen) @endpoints << last_chosen @counter = 0 end end @endpoints |= endpoints end |