Class: RightSupport::Net::LB::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/right_support/net/lb/base.rb

Overview

base class for load balancer policies.

Direct Known Subclasses

HealthCheck, RoundRobin, Sticky

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.

Parameters:

  • options (Hash) (defaults to: {})

    provided for derived policies



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/right_support/net/lb/base.rb', line 29

def initialize(options = {})
  @options = options.dup
  @endpoints = []
  @counter = rand(0xffff)

  # note that not all policies require health-checking but the request
  # balancer will provide the callback by default.
  @health_check = @options.delete(:health_check)
  if @health_check && !@health_check.respond_to?(:call)
    raise ::ArgumentError, ':health_check is invalid.'
  end
end

Instance Method Details

#bad(endpoint, t0, t1) ⇒ TrueClass

override only if statisics are kept

Returns:

  • (TrueClass)

    always true



69
70
71
# File 'lib/right_support/net/lb/base.rb', line 69

def bad(endpoint, t0, t1)
  true
end

#good(endpoint, t0, t1) ⇒ TrueClass

override only if statisics are kept

Returns:

  • (TrueClass)

    always true



62
63
64
# File 'lib/right_support/net/lb/base.rb', line 62

def good(endpoint, t0, t1)
  true
end

#health_check(endpoint) ⇒ TrueClass

performs a basic health-check for given endpoint using functor and updates statistics, if kept.

Parameters:

  • endpoint (String)

    for check

Returns:

  • (TrueClass)

    always true



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/right_support/net/lb/base.rb', line 79

def health_check(endpoint)
  t0 = ::Time.now
  result = @health_check.call(endpoint)
  t1 = ::Time.now
  if result
    good(endpoint, t0, t1)
    return true
  end
  bad(endpoint, t0, t1)
  return false
rescue ::Exception
  t1 = ::Time.now
  bad(endpoint, t0, t1)
  raise
end

#nextObject

override only if endpoint selection is non-trivial



49
50
51
52
53
54
55
56
57
# File 'lib/right_support/net/lb/base.rb', line 49

def next
  result = nil
  unless @endpoints.empty?
    # endpoint, no health-check required.
    result = [ @endpoints[@counter % @endpoints.size], false ]
    @counter += 1
  end
  result
end

#set_endpoints(endpoints) ⇒ Object

override only if endpoint selection is non-trivial



43
44
45
46
# File 'lib/right_support/net/lb/base.rb', line 43

def set_endpoints(endpoints)
  @endpoints = endpoints
  true
end