ebisu_connection 0.0.3 → 0.0.4
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.
- checksums.yaml +4 -4
- data/ebisu_connection.gemspec +1 -1
- data/lib/ebisu_connection/connection_manager.rb +20 -10
- data/lib/ebisu_connection/slaves.rb +25 -10
- data/lib/ebisu_connection/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: acec29bfafab4c90b03cf8594363584b07918948
|
4
|
+
data.tar.gz: c4659e218081594a681fbf92e8443018560239b1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5226bfef7a1027d4ff43dd59d61d9b53dc569cc96e4d12b96cd49bf857adb5111600a73cee38e94d4bbceca1e8e7e71722a8a6c97de5771b7a304da6091c8929
|
7
|
+
data.tar.gz: e1a8426b0365a3132464b062e541d0835e887025b74a3a82536301011113dfa74916d2b2d8f4f0969b95031ffb1050c05163f65a89731a0fe0499b8d2c37be0e
|
data/ebisu_connection.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
21
|
spec.add_dependency 'activerecord', '>= 3.2.0'
|
22
|
-
spec.add_dependency 'fresh_connection', '>= 0.1.
|
22
|
+
spec.add_dependency 'fresh_connection', '>= 0.1.7'
|
23
23
|
|
24
24
|
spec.add_development_dependency "bundler", "~> 1.3"
|
25
25
|
spec.add_development_dependency "rake"
|
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'yaml'
|
2
|
+
require 'fresh_connection/abstract_connection_manager'
|
2
3
|
|
3
4
|
module EbisuConnection
|
4
|
-
class ConnectionManager
|
5
|
+
class ConnectionManager < FreshConnection::AbstractConnectionManager
|
5
6
|
class << self
|
6
7
|
delegate :slave_file, :slave_file=, :check_interval, :check_interval=,
|
7
8
|
:slave_type, :slave_type=, :to => EbisuConnection::ConfFile
|
@@ -13,7 +14,7 @@ module EbisuConnection
|
|
13
14
|
:to => EbisuConnection::ConfFile
|
14
15
|
|
15
16
|
def initialize
|
16
|
-
|
17
|
+
super
|
17
18
|
@slaves = {}
|
18
19
|
end
|
19
20
|
|
@@ -30,8 +31,21 @@ module EbisuConnection
|
|
30
31
|
end
|
31
32
|
end
|
32
33
|
|
34
|
+
def recovery(failure_connection, exception)
|
35
|
+
if recoverable? && slave_down_message?(exception.message)
|
36
|
+
slaves.remove_connection(failure_connection)
|
37
|
+
true
|
38
|
+
else
|
39
|
+
false
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def recoverable?
|
44
|
+
true
|
45
|
+
end
|
46
|
+
|
33
47
|
def clear_all_connection!
|
34
|
-
|
48
|
+
synchronize do
|
35
49
|
@slaves.values.each do |s|
|
36
50
|
s.all_disconnect!
|
37
51
|
end
|
@@ -44,7 +58,7 @@ module EbisuConnection
|
|
44
58
|
private
|
45
59
|
|
46
60
|
def check_own_connection
|
47
|
-
|
61
|
+
synchronize do
|
48
62
|
s = @slaves[current_thread_id]
|
49
63
|
|
50
64
|
if s && s.reserved_release?
|
@@ -58,7 +72,7 @@ module EbisuConnection
|
|
58
72
|
end
|
59
73
|
|
60
74
|
def reserve_release_all_connection
|
61
|
-
|
75
|
+
synchronize do
|
62
76
|
@slaves.values.each do |s|
|
63
77
|
s.reserve_release_connection!
|
64
78
|
end
|
@@ -67,7 +81,7 @@ module EbisuConnection
|
|
67
81
|
end
|
68
82
|
|
69
83
|
def slaves
|
70
|
-
|
84
|
+
synchronize do
|
71
85
|
@slaves[current_thread_id] ||= get_slaves
|
72
86
|
end
|
73
87
|
end
|
@@ -75,9 +89,5 @@ module EbisuConnection
|
|
75
89
|
def get_slaves
|
76
90
|
EbisuConnection::Slaves.new(slaves_conf, spec)
|
77
91
|
end
|
78
|
-
|
79
|
-
def current_thread_id
|
80
|
-
Thread.current.object_id
|
81
|
-
end
|
82
92
|
end
|
83
93
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
module EbisuConnection
|
2
2
|
class Slaves
|
3
|
+
class AllSlavesHasGoneError < StandardError; end
|
4
|
+
|
3
5
|
class Slave
|
4
6
|
attr_reader :hostname, :weight
|
5
7
|
|
@@ -45,25 +47,27 @@ module EbisuConnection
|
|
45
47
|
end
|
46
48
|
|
47
49
|
def initialize(slaves_conf, spec)
|
48
|
-
weight_list = []
|
49
50
|
@slaves = slaves_conf.map do |conf|
|
50
|
-
|
51
|
-
weight_list << s.weight
|
52
|
-
s
|
51
|
+
Slave.new(conf, spec)
|
53
52
|
end
|
54
53
|
|
55
|
-
|
56
|
-
gcd = get_gcd(weight_list)
|
57
|
-
weight_list.each_with_index do |w, index|
|
58
|
-
weight = w / gcd
|
59
|
-
@roulette.concat([index] * weight)
|
60
|
-
end
|
54
|
+
recalc_roulette
|
61
55
|
end
|
62
56
|
|
63
57
|
def sample
|
58
|
+
raise AllSlavesHasGoneError if @slaves.blang?
|
64
59
|
@slaves[@roulette.sample]
|
65
60
|
end
|
66
61
|
|
62
|
+
def remove_connection(connection)
|
63
|
+
return unless s = @slaves.detect{|s| s.connection == connection}
|
64
|
+
s.disconnect! rescue nil
|
65
|
+
@slaves.delete(s)
|
66
|
+
raise AllSlavesHasGoneError if @slaves.blank?
|
67
|
+
recalc_roulette
|
68
|
+
nil
|
69
|
+
end
|
70
|
+
|
67
71
|
def all_disconnect!
|
68
72
|
@reserve_release = nil
|
69
73
|
@slaves.each {|s| s.disconnect!}
|
@@ -79,6 +83,17 @@ module EbisuConnection
|
|
79
83
|
|
80
84
|
private
|
81
85
|
|
86
|
+
def recalc_roulette
|
87
|
+
weight_list = @slaves.map {|s| s.weight }
|
88
|
+
|
89
|
+
@roulette = []
|
90
|
+
gcd = get_gcd(weight_list)
|
91
|
+
weight_list.each_with_index do |w, index|
|
92
|
+
weight = w / gcd
|
93
|
+
@roulette.concat([index] * weight)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
82
97
|
def get_gcd(list)
|
83
98
|
list = list.sort.uniq
|
84
99
|
n = list.shift
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ebisu_connection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- tsukasaoishi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-12-
|
11
|
+
date: 2013-12-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.1.
|
33
|
+
version: 0.1.7
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0.1.
|
40
|
+
version: 0.1.7
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|