hawatel_tlb 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ module HawatelTlb
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,38 @@
1
+ require 'socket'
2
+ require 'timeout'
3
+ require 'bigdecimal'
4
+
5
+ module HawatelTlb
6
+ module WatchDog
7
+
8
+ # Check node status
9
+ #
10
+ def watcher
11
+ @group.each do |node|
12
+ if node.state == 'enable'
13
+ start = Time.now()
14
+ port_open?(node) ? port_state = 'online' : port_state = 'offline'
15
+ stop = Time.now() - start
16
+ node.status = {:time => Time.now.to_i, :state => port_state, :respond_time => stop.round(4)}
17
+ end
18
+ end
19
+ end
20
+
21
+ private
22
+ # Check if port is open
23
+ #
24
+ def port_open?(node)
25
+ Timeout::timeout(node.timeout) do
26
+ begin
27
+ TCPSocket.new(node.host, node.port).close
28
+ true
29
+ rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
30
+ false
31
+ end
32
+ end
33
+ rescue Timeout::Error, SocketError
34
+ false
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ describe HawatelTlb::Client do
4
+
5
+ let(:client) { HawatelTlb::Client.new }
6
+
7
+
8
+ describe 'Validate_host_settings?' do
9
+
10
+ it 'Add valid host' do
11
+ result = client.add({:host => '8.8.8.3', :port => 443, :weight => 0})
12
+ expect(result).to eq('success')
13
+ end
14
+
15
+ it 'Add host with invalid ip' do
16
+ result = client.add({:host => '8.256.2.3', :port => 80, :weight => 0})
17
+ expect(result).to eq('incorrect host name or ip address, ')
18
+ end
19
+
20
+ it 'Add host with invalid port number' do
21
+ result = client.add({:host => '8.3.2.3', :port => 0, :weight => 0})
22
+ expect(result).to eq('incorrect port number, ')
23
+ end
24
+
25
+ it 'Add host with invalid domainame' do
26
+ result = client.add({:host => 'example.23sd', :port => 80, :weight => 0})
27
+ expect(result).to eq('incorrect host name or ip address, ')
28
+ end
29
+
30
+ it 'Duplicate host' do
31
+ client.add({:host => 'example.com', :port => 80, :weight => 0})
32
+ result_2 = client.add({:host => 'example.com', :port => 80, :weight => 0})
33
+ expect(result_2).to eq('host with the same configuration already exists in the group, ')
34
+ end
35
+
36
+ end
37
+
38
+ describe 'Delete host' do
39
+
40
+ it 'Delete non exist id' do
41
+ result = client.del(22)
42
+ expect(result).to eq('invalid host id')
43
+ end
44
+
45
+ it 'String instead Fixnumer' do
46
+ result = client.del('asdasd')
47
+ expect(result).to eq('invalid value')
48
+ end
49
+
50
+ it 'Delete host' do
51
+ id = client.add({:host => 'example.com', :port => 80, :weight => 0})
52
+ expect(client.del(1)).to eq('host successful deleted')
53
+ end
54
+
55
+ end
56
+
57
+
58
+ describe 'Configure group' do
59
+
60
+ it 'Configure' do
61
+ result = client.configure({:mode => 'RR'})
62
+ expect(result).to eq('invalid mode')
63
+ end
64
+
65
+ end
66
+
67
+ it "Get current list" do
68
+ client.add({:host => 'example.com', :port => 443, :weight => 0})
69
+ client.add({:host => 'example2.com', :port => 443, :weight => 0})
70
+ client.add({:host => 'example3.com', :port => 443, :weight => 0})
71
+ expect(client.list[0][:id]).to eq(1)
72
+ expect(client.list[0][:host]).to eq('example.com')
73
+ expect(client.list[0][:port]).to eq(443)
74
+ expect(client.list[0][:weight]).to eq(0)
75
+ end
76
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe HawatelTlb::Mode::DynamicRatio do
4
+
5
+ let(:client) { HawatelTlb::Client.new }
6
+ before do
7
+ stub_const("HawatelTlb::Mode::DynamicRatio::RECALC_WEIGHT_INTERVAL", 1)
8
+ end
9
+
10
+ context 'dynamic weights' do
11
+ let(:loop_count) { 1000 }
12
+ it 'weight is not in init state' do
13
+ setup_dynamicratio_mode
14
+ exec_node_method(loop_count)
15
+ verify_weights(client.list)
16
+ verify_weights(client.list)
17
+
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def verify_weights(nodes)
24
+ nodes.each do |node|
25
+ expect(node.weight).to be > 1
26
+ end
27
+ end
28
+
29
+ def setup_dynamicratio_mode
30
+ client.add({:host => 'example.com', :port => 80})
31
+ client.add({:host => 'example2.com', :port => 80})
32
+ client.add({:host => 'example3.com', :port => 80})
33
+ client.configure(:mode => 'dynamicratio')
34
+ client.mode.debug = 0
35
+ end
36
+
37
+ def exec_node_method(count)
38
+ (0..count-1).each do
39
+ client.node
40
+ end
41
+ end
42
+
43
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe HawatelTlb::Mode::RoundRobin do
4
+ let(:client) { HawatelTlb::Client.new }
5
+
6
+ it 'Fastest respond server' do
7
+ client.add({:host => 'example.com', :port => 443, :weight => 0})
8
+ client.add({:host => 'google.com', :port => 443, :weight => 0})
9
+ client.add({:host => 'hawatel.com', :port => 443, :weight => 0})
10
+ client.configure({:mode => 'fastest', :interval => 5})
11
+ expect(client.node[:host]).to be_a_kind_of(String)
12
+ expect(client.node[:port]).to be_a_kind_of(Fixnum)
13
+ end
14
+
15
+ it 'Lack of statistics for one node' do
16
+ client.add({:host => 'example.com', :port => 443, :weight => 0})
17
+ client.configure({:mode => 'fastest'})
18
+ client.add({:host => 'google.com', :port => 443, :weight => 0})
19
+ expect(client.node[:host]).to eq('example.com')
20
+ expect(client.node[:port]).to eq(443)
21
+ end
22
+
23
+
24
+ it 'Available only one node' do
25
+ client.add({:host => 'example.com', :port => 443, :weight => 0,})
26
+ client.add({:host => 'thisdomainshouldexist2.com', :port => 443, :weight => 0 })
27
+ client.configure({:mode => 'fastest'})
28
+ expect(client.node[:host]).to eq('example.com')
29
+ expect(client.node[:port]).to eq(443)
30
+ end
31
+
32
+ it 'All nodes are disabled' do
33
+ client.add({:host => 'example.com', :port => 443, :weight => 0, :state => 'disable'})
34
+ client.add({:host => 'google.com', :port => 443, :weight => 0, :state => 'disable'})
35
+ client.configure({:mode => 'fastest'})
36
+ expect(client.node).to eq(false)
37
+ end
38
+
39
+ it 'All nodes are offline' do
40
+ client.add({:host => 'thisdomainshouldexist1.com', :port => 443, :weight => 0,})
41
+ client.add({:host => 'thisdomainshouldexist2.com', :port => 443, :weight => 0 })
42
+ client.configure({:mode => 'fastest'})
43
+ expect(client.node).to eq(false)
44
+ end
45
+
46
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ describe HawatelTlb::Mode::Ratio do
4
+
5
+ let(:client) { HawatelTlb::Client.new }
6
+
7
+ context "the most important node" do
8
+ let(:loop_count) { 1000 }
9
+
10
+ it "the first one" do
11
+ setup_ratio_mode(2,1,1)
12
+ exec_node_method(loop_count)
13
+
14
+ nodes = client.list
15
+
16
+ expect(nodes[0].ratio[:traffic]).to eq(500)
17
+ expect(nodes[1].ratio[:traffic]).to eq(250)
18
+ expect(nodes[2].ratio[:traffic]).to eq(250)
19
+
20
+ expect(sum_traffic(nodes)).to eq(loop_count)
21
+ end
22
+
23
+ it "the second one" do
24
+ setup_ratio_mode(2,40,10)
25
+ exec_node_method(loop_count)
26
+
27
+ nodes = client.list
28
+
29
+ expect(nodes[0].ratio[:traffic]).to eq(39)
30
+ expect(nodes[1].ratio[:traffic]).to eq(769)
31
+ expect(nodes[2].ratio[:traffic]).to eq(192)
32
+
33
+ expect(sum_traffic(nodes)).to eq(loop_count)
34
+ end
35
+
36
+ it "the third one" do
37
+ setup_ratio_mode(0,60,1000)
38
+ exec_node_method(loop_count)
39
+
40
+ nodes = client.list
41
+
42
+ expect(nodes[0].ratio[:traffic]).to eq(1)
43
+ expect(nodes[1].ratio[:traffic]).to eq(57)
44
+ expect(nodes[2].ratio[:traffic]).to eq(942)
45
+
46
+ expect(sum_traffic(nodes)).to eq(loop_count)
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def setup_ratio_mode(w1, w2, w3)
53
+ client.add({:host => 'example.com', :port => 80, :weight => w1})
54
+ client.add({:host => 'example2.com', :port => 80, :weight => w2})
55
+ client.add({:host => 'example3.com', :port => 80, :weight => w3})
56
+ client.configure(:mode => 'ratio')
57
+ client.mode.debug = 0
58
+ end
59
+
60
+ def exec_node_method(count)
61
+ (0..count-1).each do
62
+ client.node
63
+ end
64
+ end
65
+
66
+ def sum_traffic(nodes)
67
+ traffic = 0
68
+ nodes.each do |node|
69
+ traffic += node.ratio[:traffic]
70
+ end
71
+ traffic
72
+ end
73
+
74
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe HawatelTlb::Mode::RoundRobin do
4
+ let(:client) { HawatelTlb::Client.new }
5
+
6
+ it 'all nodes are enabled' do
7
+ client.add({:host => 'example.com', :port => 443, :weight => 0})
8
+ client.add({:host => 'example2.com', :port => 443, :weight => 0})
9
+ client.configure({:mode => 'roundrobin'})
10
+ expect(client.node[:host]).to eq('example.com')
11
+ end
12
+
13
+ it 'all nodes are disabled' do
14
+ client.add({:host => 'example.com', :port => 443, :weight => 0, :state => 'disable'})
15
+ client.add({:host => 'example2.com', :port => 443, :weight => 0, :state => 'disable'})
16
+ client.configure({:mode => 'roundrobin'})
17
+ expect(client.node).to eq(false)
18
+ end
19
+
20
+ it 'overloop nodes' do
21
+ client.add({:host => 'example.com', :port => 443, :weight => 0, :state => 'enable'})
22
+ client.add({:host => 'example2.com', :port => 443, :weight => 0, :state => 'enable'})
23
+ client.add({:host => 'github.com', :port => 80, :weight => 0, :state => 'enable'})
24
+ client.configure({:mode => 'roundrobin'})
25
+ ip_1 = client.node[:host]
26
+ ip_2 = client.node[:host]
27
+ ip_3 = client.node[:host]
28
+ ip_4 = client.node[:host]
29
+ expect(ip_1).to eq('example.com')
30
+ expect(ip_2).to eq('example2.com')
31
+ expect(ip_3).to eq('github.com')
32
+ expect(ip_4).to eq('example.com')
33
+ end
34
+
35
+ it 'all nodes are offline' do
36
+ client.add({:host => 'thisdomainshouldexist1.com', :port => 443, :weight => 0,})
37
+ client.add({:host => 'thisdomainshouldexist2.com', :port => 443, :weight => 0 })
38
+ client.configure({:mode => 'roundrobin'})
39
+ expect(client.node).to eq(false)
40
+ end
41
+
42
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe HawatelTlb::Mode::Weighted do
4
+ let(:client) { HawatelTlb::Client.new }
5
+
6
+ it 'Find host with highest weight' do
7
+ client.add({:host => 'example.com', :port => 80, :weight => 0})
8
+ client.add({:host => 'wp.pl', :port => 80, :weight => 5})
9
+ client.add({:host => 'onet.pl', :port => 80, :weight => 3})
10
+ client.configure({:mode => 'weighted'})
11
+ stat = client.node
12
+ expect(stat[:host]).to eq('wp.pl')
13
+ expect(stat[:port]).to eq(80)
14
+ end
15
+
16
+ it 'Two hosts has equal range of weight ' do
17
+ client.add({:host => 'example.com', :port => 80, :weight => 0})
18
+ client.add({:host => 'wp.pl', :port => 80, :weight => 5})
19
+ client.add({:host => 'onet.pl', :port => 80, :weight => 5})
20
+ client.configure({:mode => 'weighted'})
21
+ stat = client.node
22
+ expect(stat[:host]).to eq('wp.pl')
23
+ expect(stat[:port]).to eq(80)
24
+ end
25
+
26
+ it 'All hosts are disable' do
27
+ client.add({:host => 'example.com', :port => 80, :weight => 0, :state => 'disable'})
28
+ client.add({:host => 'wp.pl', :port => 80, :weight => 5, :state => 'disable'})
29
+ client.configure({:mode => 'weighted'})
30
+ expect(client.node).to eq(false)
31
+ end
32
+
33
+ it 'All hosts are offline' do
34
+ client.add({:host => 'example.com', :port => 1, :weight => 0})
35
+ client.add({:host => 'wp.pl', :port => 1, :weight => 5})
36
+ client.configure({:mode => 'weighted'})
37
+ expect(client.node).to eq(false)
38
+ end
39
+
40
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'hawatel_tlb'
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hawatel_tlb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Iwaniuk
8
+ - Przemyslaw Mantaj
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-04-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.10'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.10'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ description: Hawatel_tlb is a ruby version load balancing which the purpose is to
57
+ dynamic return selected address IP/domainame based on specified algorithm
58
+ email:
59
+ - daniel.iwaniuk@hawatel.com
60
+ - przemyslaw.mantaj@hawatel.com
61
+ executables:
62
+ - console
63
+ - setup
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - ".gitignore"
68
+ - ".rspec"
69
+ - ".travis.yml"
70
+ - CONTRIBUTING.md
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - bin/console
76
+ - bin/setup
77
+ - hawatel_tlb.gemspec
78
+ - lib/hawatel_tlb.rb
79
+ - lib/hawatel_tlb/client.rb
80
+ - lib/hawatel_tlb/mode.rb
81
+ - lib/hawatel_tlb/mode/dynamicratio.rb
82
+ - lib/hawatel_tlb/mode/fastest.rb
83
+ - lib/hawatel_tlb/mode/ratio.rb
84
+ - lib/hawatel_tlb/mode/roundrobin.rb
85
+ - lib/hawatel_tlb/mode/weighted.rb
86
+ - lib/hawatel_tlb/version.rb
87
+ - lib/hawatel_tlb/watchdog.rb
88
+ - spec/client_spec.rb
89
+ - spec/modes/dynamicratio_spec.rb
90
+ - spec/modes/fastest_spec.rb
91
+ - spec/modes/ratio_spec.rb
92
+ - spec/modes/roundrobin_spec.rb
93
+ - spec/modes/weighted_spec.rb
94
+ - spec/spec_helper.rb
95
+ homepage: http://github.com/hawatel/hawatel_tlb
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '1.9'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.4.8
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Ruby gem for failover detection and balancing hosts group
119
+ test_files:
120
+ - spec/client_spec.rb
121
+ - spec/modes/dynamicratio_spec.rb
122
+ - spec/modes/fastest_spec.rb
123
+ - spec/modes/ratio_spec.rb
124
+ - spec/modes/roundrobin_spec.rb
125
+ - spec/modes/weighted_spec.rb
126
+ - spec/spec_helper.rb
127
+ has_rdoc: