Class: HawatelTlb::Mode::RoundRobin

Inherits:
Object
  • Object
show all
Defined in:
lib/hawatel_tlb/mode/roundrobin.rb

Overview

Overview

Details

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group) ⇒ RoundRobin

Returns a new instance of RoundRobin.



13
14
15
16
# File 'lib/hawatel_tlb/mode/roundrobin.rb', line 13

def initialize(group)
  @group = group
  @round = Array.new
end

Instance Attribute Details

#nameType

Returns description.

Returns:

  • (Type)

    description



11
12
13
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
45
46
47
48
# File 'lib/hawatel_tlb/mode/roundrobin.rb', line 11

class RoundRobin

  def initialize(group)
    @group = group
    @round = Array.new
  end

  # Refresh group table after delete or add host
  #
  # @param group [Array<Hash>]
  def refresh(group)
    @group = group
  end

  # Return ip address based on Round Robin algorithm
  #
  # @return [Hash] hostname/ip address and port number
  def node
    counter    = 0
    first_item = ''
    @group.each do |node|
      if !node.status.empty?
        if node.status[:state] == 'online' && node.state == 'enable'
          first_item = {:host => node.host, :port => node.port} if counter == 0
          if !@round.include?(node.id)
            @round.push(node.id)
            return {:host => node.host, :port => node.port}
          end
          counter += 1
        end
      end
    end
    @round = Array.new
    return first_item if !first_item.empty?
    false
  end

end

Instance Method Details

#nodeHash

Return ip address based on Round Robin algorithm

Returns:

  • (Hash)

    hostname/ip address and port number



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/hawatel_tlb/mode/roundrobin.rb', line 28

def node
  counter    = 0
  first_item = ''
  @group.each do |node|
    if !node.status.empty?
      if node.status[:state] == 'online' && node.state == 'enable'
        first_item = {:host => node.host, :port => node.port} if counter == 0
        if !@round.include?(node.id)
          @round.push(node.id)
          return {:host => node.host, :port => node.port}
        end
        counter += 1
      end
    end
  end
  @round = Array.new
  return first_item if !first_item.empty?
  false
end

#refresh(group) ⇒ Object

Refresh group table after delete or add host

Parameters:

  • group (Array<Hash>)


21
22
23
# File 'lib/hawatel_tlb/mode/roundrobin.rb', line 21

def refresh(group)
  @group = group
end