Class: HawatelTlb::Client

Inherits:
Object
  • Object
show all
Includes:
WatchDog
Defined in:
lib/hawatel_tlb/client.rb

Constant Summary collapse

WORKING_MODE =
{
    'dynamicratio' => Mode::DynamicRatio,
    'roundrobin' => Mode::RoundRobin,
    'fastest' => Mode::Fastest,
    'ratio' => Mode::Ratio,
    'weighted' => Mode::Weighted
}
DEFAULT_STATE =
'enable'
DEFAULT_PORT =
80
DEFAULT_WEIGHT =
1
DEFAULT_TIMEOUT =
5
DEFAULT_INTERVAL =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from WatchDog

#watcher

Constructor Details

#initializeClient

Returns a new instance of Client.



24
25
26
27
28
# File 'lib/hawatel_tlb/client.rb', line 24

def initialize
  @group    = Array.new
  @interval = DEFAULT_INTERVAL
  watchdog_thread()
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



9
10
11
# File 'lib/hawatel_tlb/client.rb', line 9

def group
  @group
end

#intervalObject (readonly)

Returns the value of attribute interval.



9
10
11
# File 'lib/hawatel_tlb/client.rb', line 9

def interval
  @interval
end

#modeObject (readonly)

Returns the value of attribute mode.



9
10
11
# File 'lib/hawatel_tlb/client.rb', line 9

def mode
  @mode
end

Instance Method Details

#add(args) ⇒ Integer

Add host to TLB group

Examples:

client.add({:value => 'example'})

Parameters:

  • args (Hash)

Options Hash (args):

  • :host (String)

    hostname or ip address

  • :port (Integer)

    port number

  • :timeout (Integer)

    default 5s.

  • :state (String)

    Available options: enable(default), disable.

  • :wight (Integer)

    priority of host, higher value is equal lower priority

Returns:

  • (Integer)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hawatel_tlb/client.rb', line 43

def add(args)
  host    = args[:host]
  port    = args[:port]    || DEFAULT_PORT
  weight  = args[:weight]  || DEFAULT_WEIGHT
  timeout = args[:timeout] || DEFAULT_TIMEOUT
  state   = args[:state]   || DEFAULT_STATE

  status = validate_host_settings({:host => host, :port => port, :weight => weight, :timeout => timeout,
                                   :state => state})
  if status == 'success'
    host_cfg = OpenStruct.new(:id => host_id.to_i, :host => host, :port => port, :weight => weight,
                              :timeout => timeout, :state => state, :status => {:time => 0, :state => 'offline',
                                                                                :respond_time => 0})
    @group.push(host_cfg)
  end
  @mode.refresh(@group) if @mode
  return status
end

#configure(args) ⇒ Integer

Set settings for group

Examples:

client.configure({:mode => 'roundrobin'})

Parameters:

  • args (Hash)

Options Hash (args):

  • :mode (String)

    failover, roundrobin

Returns:

  • (Integer)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/hawatel_tlb/client.rb', line 103

def configure(args)
  if @group[0]
    watcher if @group[0].status[:time] == 0
  end

  mode = args[:mode] || 'roundrobin'
  @interval if args[:interval]
  return 'invalid mode' if !WORKING_MODE.key?(mode)
  if @mode
    @mode.destroy if @mode.respond_to?(:destroy)
    @mode = nil
  else
    @mode = nil
  end
  @mode = WORKING_MODE[mode].new(@group)
end

#del(id) ⇒ String

Delete host from TLB group

Examples:

client.del(2)

Parameters:

  • id (Integer)

    host id

Returns:

  • (String)


69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hawatel_tlb/client.rb', line 69

def del(id)
  if id.is_a?(Fixnum)
    if @group[id-1]
      @group.delete(id-1)
      @mode.refresh(@group) if @mode
      return 'host successful deleted'
    else
      return 'invalid host id'
    end
  else
    'invalid value'
  end
end

#listArray<String>

Return hosts list

Examples:

client.add({:host => 'example.com', :port => '80', weight => '5'})
client.list

Returns:

  • (Array<String>)


91
92
93
# File 'lib/hawatel_tlb/client.rb', line 91

def list
  return @group
end

#nodeInteger

Description

Examples:

counte({:value => 'example'})

Parameters:

  • args (Hash)

Returns:

  • (Integer)


130
131
132
133
# File 'lib/hawatel_tlb/client.rb', line 130

def node
  return @mode.node if @mode
  false
end

#statusInteger

Description

Examples:

counte({:value => 'example'})

Parameters:

  • args (Hash)

Returns:

  • (Integer)


144
145
146
147
# File 'lib/hawatel_tlb/client.rb', line 144

def status
  @group.each do |node|
  end
end