Class: Murakumo::Balancer

Inherits:
Object
  • Object
show all
Defined in:
lib/srv/murakumo_balancer.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash, address, db, logger) ⇒ Balancer

Returns a new instance of Balancer.



7
8
9
10
11
12
# File 'lib/srv/murakumo_balancer.rb', line 7

def initialize(hash, address, db, logger)
  @hash = hash
  @address = address
  @db = db
  @logger = logger
end

Instance Method Details

#sort(records, max_ip_num, name) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/srv/murakumo_balancer.rb', line 14

def sort(records, max_ip_num, name)
  # ハッシュが空ならランダムで
  if @hash.nil? or @hash.empty?
    return random(records, max_ip_num)
  end

  # 宛先を検索
  dest, attrs = @hash.find {|k, v| k =~ name }

  if dest.nil? or attrs.nil?
    # 設定が見つからない場合はとりあえずランダムで
    return random(records, max_ip_num)
  end

  algo = attrs[:algorithm]
  max_ip_num = [(attrs[:max_ip_num] || max_ip_num), records.length].min
  sources = attrs[:sources]

  case algo
  when :random
    random(records, max_ip_num)
  when :fix_by_src
    fix_by_src(records, max_ip_num, sources)
  when :fix_by_src2
    fix_by_src2(records, max_ip_num, sources)
  else
    # 未対応のアルゴリズムの場合はとりあえずランダムで返す
    @logger.warn("distribution setup which is not right: #{[dest, algo].inspect}")
    random(records, max_ip_num)
  end
end