ebisu_connection 0.0.2 → 0.0.3
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/README.md +11 -9
- data/lib/ebisu_connection.rb +1 -0
- data/lib/ebisu_connection/conf_file.rb +63 -0
- data/lib/ebisu_connection/connection_manager.rb +22 -53
- data/lib/ebisu_connection/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d18980741ce133172b0fb8ab9bbaa1e171a1190
|
4
|
+
data.tar.gz: 4e44601e1a9137721c63b69be36604155b8b7e0e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61ce15decbf4956cb90a81ac924bc62e89758bf2c290f1a73c0684350d7cd90192d3086ea268e1a0ca8951a73b64eff15e36d0c10b72002682089c507880b1e3
|
7
|
+
data.tar.gz: 88d8e8e397020242d01dd69c7d386e1a0f09a1a6295f51cf2d3dbb48faeae9f64fc2af19a5d693f64421724e5956f8e0e07d1c7467502156ac83b369a877e541
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# EbisuConnection
|
1
|
+
# EbisuConnection
|
2
2
|
|
3
3
|
EbisuConnection supports to connect with Mysql slave servers. It doesn't need Load Balancer.
|
4
4
|
You can assign a performance weight to each slave server. And slave config is reflected dynamic.
|
@@ -45,11 +45,12 @@ Others will use the master setting. If you want to change, write in the slave.
|
|
45
45
|
|
46
46
|
Config of each slave server fill out config/slave.yaml
|
47
47
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
48
|
+
production:
|
49
|
+
- "slave1, 10"
|
50
|
+
- "slave2, 20"
|
51
|
+
-
|
52
|
+
host: "slave3"
|
53
|
+
weight: 30
|
53
54
|
|
54
55
|
config/slave.yaml is checked by end of action. If config changed, it's reflected dynamic. Application doesn't need restart.
|
55
56
|
|
@@ -59,17 +60,18 @@ String format is it. You can write config with hash.
|
|
59
60
|
|
60
61
|
### Only master models
|
61
62
|
|
62
|
-
config/initializers/
|
63
|
+
config/initializers/ebisu_connection.rb
|
63
64
|
|
64
|
-
|
65
|
+
EbisuConnection::ConnectionManager.ignore_models = %w|Model1 Model2|
|
65
66
|
|
66
|
-
If models that ignore access to slave servers is exist, You can write model name at
|
67
|
+
If models that ignore access to slave servers is exist, You can write model name at EbisuConnection::ConnectionManager.ignore models.
|
67
68
|
|
68
69
|
## Usage
|
69
70
|
|
70
71
|
Read query will be access to slave server.
|
71
72
|
|
72
73
|
Article.where(:id => 1)
|
74
|
+
Article.count
|
73
75
|
|
74
76
|
If you want to access to master saver, use readonly(false).
|
75
77
|
|
data/lib/ebisu_connection.rb
CHANGED
@@ -0,0 +1,63 @@
|
|
1
|
+
module EbisuConnection
|
2
|
+
class ConfFile
|
3
|
+
class << self
|
4
|
+
attr_writer :slaves_file, :check_interval
|
5
|
+
attr_accessor :slave_type
|
6
|
+
|
7
|
+
def if_modify
|
8
|
+
if time_to_check? && modify?
|
9
|
+
yield
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def conf_clear!
|
14
|
+
@slaves_conf = nil
|
15
|
+
@spec = nil
|
16
|
+
end
|
17
|
+
|
18
|
+
def slaves_conf
|
19
|
+
@slaves_conf ||= get_slaves_conf
|
20
|
+
end
|
21
|
+
|
22
|
+
def spec
|
23
|
+
@spec ||= get_spec
|
24
|
+
end
|
25
|
+
|
26
|
+
def slaves_file
|
27
|
+
@slaves_file || File.join(Rails.root, "config/slave.yaml")
|
28
|
+
end
|
29
|
+
|
30
|
+
def check_interval
|
31
|
+
@check_interval || 1.minute
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def time_to_check?
|
37
|
+
now = Time.now
|
38
|
+
@check_time ||= now
|
39
|
+
|
40
|
+
return false if now - @check_time < check_interval
|
41
|
+
|
42
|
+
@check_time = now
|
43
|
+
true
|
44
|
+
end
|
45
|
+
|
46
|
+
def modify?
|
47
|
+
@file_mtime != File.mtime(slaves_file)
|
48
|
+
end
|
49
|
+
|
50
|
+
def get_slaves_conf
|
51
|
+
@file_mtime = File.mtime(slaves_file)
|
52
|
+
conf = YAML.load_file(slaves_file)
|
53
|
+
conf = conf[Rails.env.to_s] if conf.is_a?(Hash)
|
54
|
+
slave_type ? conf[slave_type.to_s] : conf
|
55
|
+
end
|
56
|
+
|
57
|
+
def get_spec
|
58
|
+
ret = ActiveRecord::Base.configurations[Rails.env]
|
59
|
+
ret.merge(ret["slave"] || {})
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -2,19 +2,19 @@ require 'yaml'
|
|
2
2
|
|
3
3
|
module EbisuConnection
|
4
4
|
class ConnectionManager
|
5
|
-
CHECK_INTERVAL = 1.minute
|
6
|
-
|
7
5
|
class << self
|
8
|
-
|
9
|
-
|
6
|
+
delegate :slave_file, :slave_file=, :check_interval, :check_interval=,
|
7
|
+
:slave_type, :slave_type=, :to => EbisuConnection::ConfFile
|
10
8
|
|
11
|
-
|
12
|
-
@slaves_file || File.join(Rails.root, "config/slave.yaml")
|
13
|
-
end
|
9
|
+
delegate :ignore_models=, :to => FreshConnection::SlaveConnection
|
14
10
|
end
|
15
11
|
|
12
|
+
delegate :if_modify, :conf_clear!, :slaves_conf, :spec,
|
13
|
+
:to => EbisuConnection::ConfFile
|
14
|
+
|
16
15
|
def initialize
|
17
16
|
@mutex = Mutex.new
|
17
|
+
@slaves = {}
|
18
18
|
end
|
19
19
|
|
20
20
|
def slave_connection
|
@@ -23,63 +23,51 @@ module EbisuConnection
|
|
23
23
|
|
24
24
|
def put_aside!
|
25
25
|
return if check_own_connection
|
26
|
-
return unless @file_mtime
|
27
|
-
|
28
|
-
now = Time.now
|
29
|
-
@check_time ||= now
|
30
|
-
return if now - @check_time < CHECK_INTERVAL
|
31
|
-
@check_time = now
|
32
26
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
check_own_connection
|
27
|
+
if_modify do
|
28
|
+
reserve_release_all_connection
|
29
|
+
check_own_connection
|
30
|
+
end
|
38
31
|
end
|
39
32
|
|
40
33
|
def clear_all_connection!
|
41
34
|
@mutex.synchronize do
|
42
|
-
@slaves ||= {}
|
43
35
|
@slaves.values.each do |s|
|
44
36
|
s.all_disconnect!
|
45
37
|
end
|
46
38
|
|
47
|
-
@slaves =
|
48
|
-
|
49
|
-
@spec = nil
|
39
|
+
@slaves = {}
|
40
|
+
conf_clear!
|
50
41
|
end
|
51
42
|
end
|
52
43
|
|
53
44
|
private
|
54
45
|
|
55
46
|
def check_own_connection
|
56
|
-
ret = false
|
57
47
|
@mutex.synchronize do
|
58
|
-
@slaves
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
48
|
+
s = @slaves[current_thread_id]
|
49
|
+
|
50
|
+
if s && s.reserved_release?
|
51
|
+
s.all_disconnect!
|
52
|
+
@slaves.delete(current_thread_id)
|
53
|
+
true
|
54
|
+
else
|
55
|
+
false
|
64
56
|
end
|
65
57
|
end
|
66
|
-
ret
|
67
58
|
end
|
68
59
|
|
69
60
|
def reserve_release_all_connection
|
70
61
|
@mutex.synchronize do
|
71
|
-
@slaves ||= {}
|
72
62
|
@slaves.values.each do |s|
|
73
63
|
s.reserve_release_connection!
|
74
64
|
end
|
75
|
-
|
76
|
-
@spec = nil
|
65
|
+
conf_clear!
|
77
66
|
end
|
78
67
|
end
|
79
68
|
|
80
69
|
def slaves
|
81
70
|
@mutex.synchronize do
|
82
|
-
@slaves ||= {}
|
83
71
|
@slaves[current_thread_id] ||= get_slaves
|
84
72
|
end
|
85
73
|
end
|
@@ -88,25 +76,6 @@ module EbisuConnection
|
|
88
76
|
EbisuConnection::Slaves.new(slaves_conf, spec)
|
89
77
|
end
|
90
78
|
|
91
|
-
def slaves_conf
|
92
|
-
@slaves_conf ||= get_slaves_conf
|
93
|
-
end
|
94
|
-
|
95
|
-
def spec
|
96
|
-
@spec ||= get_spec
|
97
|
-
end
|
98
|
-
|
99
|
-
def get_slaves_conf
|
100
|
-
@file_mtime = File.mtime(self.class.slaves_file)
|
101
|
-
conf = YAML.load_file(self.class.slaves_file)
|
102
|
-
self.class.slave_type ? conf[self.class.slave_type] : conf
|
103
|
-
end
|
104
|
-
|
105
|
-
def get_spec
|
106
|
-
ret = ActiveRecord::Base.configurations[Rails.env]
|
107
|
-
ret.merge(ret["slave"] || {})
|
108
|
-
end
|
109
|
-
|
110
79
|
def current_thread_id
|
111
80
|
Thread.current.object_id
|
112
81
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- tsukasaoishi
|
@@ -81,6 +81,7 @@ files:
|
|
81
81
|
- Rakefile
|
82
82
|
- ebisu_connection.gemspec
|
83
83
|
- lib/ebisu_connection.rb
|
84
|
+
- lib/ebisu_connection/conf_file.rb
|
84
85
|
- lib/ebisu_connection/connection_manager.rb
|
85
86
|
- lib/ebisu_connection/slaves.rb
|
86
87
|
- lib/ebisu_connection/version.rb
|