Class: OFlow::Actors::Balancer

Inherits:
OFlow::Actor show all
Defined in:
lib/oflow/actors/balancer.rb

Overview

Redirects operations to one Task out of all the linked tasks. It uses the Task.backed_up() method to determine which task is the least busy. It also attempts to distribute requests somewhat evenly if Tasks are equally as busy.

Instance Attribute Summary

Attributes inherited from OFlow::Actor

#task

Instance Method Summary collapse

Methods inherited from OFlow::Actor

#busy?, #inputs, #options, #outputs, #set_option, #with_own_thread

Constructor Details

#initialize(task, options) ⇒ Balancer

Returns a new instance of Balancer.



11
12
13
14
15
# File 'lib/oflow/actors/balancer.rb', line 11

def initialize(task, options)
  super
  @cnt = 0
  @called = {}
end

Instance Method Details

#perform(op, box) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/oflow/actors/balancer.rb', line 17

def perform(op, box)
  best = nil
  order = nil
  bbu = nil
  @task.links().each do |dest,lnk|
    t = lnk.target
    next if t.nil?
    bu = t.backed_up()
    if bbu.nil? || bu < bbu || (bbu == bu && @called.fetch(dest, 0) < order)
      best = dest
      bbu = bu
      order = @called.fetch(dest, 0)
    end
  end
  @cnt += 1
  @called[best] = @cnt
  task.ship(best, box)
end