Class: LoadBalancer

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/logstash/utils/load_balancer.rb

Defined Under Namespace

Classes: HostState

Instance Method Summary collapse

Constructor Details

#initialize(host_infos, cool_off: 60) ⇒ LoadBalancer

Creates a new Router with the provided downstream_infos that ignores errors older than the cool_off period

Parameters:

  • host_infos (Enumerable<HostSate>)

    : a list of downstream hosts to include in routing

  • cool_off (Integer) (defaults to: 60)

    : The cool_off period in seconds in which downstreams with recent errors are de-prioritized (default: 60)



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/logstash/utils/load_balancer.rb', line 16

def initialize(host_infos, cool_off: 60)
  super() # to initialize MonitorMixin

  fail ArgumentError, "Non-empty `host_infos` hosts required." unless host_infos&.any?
  fail ArgumentError, "`cool_off` requires integer value." unless cool_off.kind_of?(Integer)

  @cool_off = cool_off
  @host_states = host_infos.map do |host_info|
    HostState.new(host_info)
  end
end

Instance Method Details

#select { ... } ⇒ Object

Yields the block with a HostState, prioritizing hosts that are less concurrently-used and which have not errored recently.

Yields:

  • param selected [HostState]



33
34
35
36
37
38
39
40
41
# File 'lib/logstash/utils/load_balancer.rb', line 33

def select
  selected = synchronize { pick_one.tap(&:increment) }
  yield selected.uri
rescue
  synchronize { selected.mark_error }
  raise
ensure
  synchronize { selected.decrement }
end