fresh_connection 0.0.1 → 0.0.2
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.
- data/History.txt +5 -0
- data/README.rdoc +11 -8
- data/fresh_connection.gemspec +5 -6
- data/lib/fresh_connection/slave_connection.rb +17 -19
- data/lib/fresh_connection.rb +1 -1
- data/rails/initializers/active_record_base.rb +16 -9
- metadata +8 -8
data/History.txt
CHANGED
data/README.rdoc
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
= FreshConnection
|
2
2
|
|
3
|
-
|
3
|
+
* https://github.com/tsukasaoishi/fresh_connection
|
4
4
|
|
5
5
|
== Description
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
FreshConnection supports of connect with Mysql slave servers via Load Balancers.
|
8
|
+
Currently, I have only tested with Rails2.3.14.
|
9
|
+
In the future, I plan to support Rails3.
|
10
10
|
|
11
|
-
|
11
|
+
All connections will be disconnected every time at the end of the action.
|
12
12
|
|
13
13
|
== How to use at Rails
|
14
14
|
|
15
15
|
=== Gemfile
|
16
|
-
gem "fresh_connection", "=0.0.
|
16
|
+
gem "fresh_connection", "=0.0.2"
|
17
17
|
|
18
18
|
=== config/database.yml
|
19
19
|
production:
|
@@ -31,10 +31,8 @@
|
|
31
31
|
username: slave
|
32
32
|
password: slave
|
33
33
|
host: slave
|
34
|
-
max_connection: 5
|
35
34
|
|
36
35
|
slave is config to connect to slave servers.
|
37
|
-
max_connection is max connection number to connect to slave.
|
38
36
|
Others will use the master setting. If you want to change, write in the slave.
|
39
37
|
|
40
38
|
|
@@ -42,6 +40,11 @@ Others will use the master setting. If you want to change, write in the slave.
|
|
42
40
|
require 'fresh_connection'
|
43
41
|
ActionController::Dispatcher.middleware.swap ActiveRecord::ConnectionAdapters::ConnectionManagement, FreshConnection::Rack::ConnectionManagement
|
44
42
|
|
43
|
+
=== config/initializers/fresh_connection.rb
|
44
|
+
FreshConnection::SlaveConnection.ignore_models = %w|Model1 Model2|
|
45
|
+
|
46
|
+
If models that ignore access to slave servers is exist, You can write model name at FreshConnection::SlaveConnection.ignore_models.
|
47
|
+
|
45
48
|
== Synopis
|
46
49
|
Read query will be access to slave server.
|
47
50
|
Article.where(:id => 1)
|
data/fresh_connection.gemspec
CHANGED
@@ -2,12 +2,12 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{fresh_connection}
|
5
|
-
s.version = "0.0.
|
5
|
+
s.version = "0.0.2"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Tsukasa OISHI"]
|
9
9
|
s.date = %q{2010-10-16}
|
10
|
-
s.description = %q{
|
10
|
+
s.description = %q{FreshConnection supports of connect with Mysql slave servers via Load Balancers.}
|
11
11
|
s.email = ["tsukasa.oishi@gmail.com"]
|
12
12
|
s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.rdoc"]
|
13
13
|
s.files = %w|
|
@@ -19,11 +19,10 @@ Gem::Specification.new do |s|
|
|
19
19
|
rails/initializers/active_record_base.rb
|
20
20
|
|
|
21
21
|
s.has_rdoc = true
|
22
|
-
s.homepage = %q{
|
23
|
-
s.post_install_message = %q{PostInstall.txt}
|
22
|
+
s.homepage = %q{https://github.com/tsukasaoishi/fresh_connection}
|
24
23
|
s.rdoc_options = ["--main", "README.rdoc"]
|
25
24
|
s.require_paths = ["lib"]
|
26
|
-
s.rubyforge_project = %q{
|
25
|
+
s.rubyforge_project = %q{fresh_connection}
|
27
26
|
s.rubygems_version = %q{1.3.1}
|
28
|
-
s.summary = %q{
|
27
|
+
s.summary = %q{FreshConnection supports of connect with Mysql slave servers via Load Balancers.}
|
29
28
|
end
|
@@ -2,51 +2,49 @@ module FreshConnection
|
|
2
2
|
class SlaveConnection
|
3
3
|
class << self
|
4
4
|
def connection
|
5
|
-
|
5
|
+
slave_connection
|
6
6
|
end
|
7
7
|
|
8
8
|
def clear_all_connections!
|
9
9
|
if @slave_connections.present?
|
10
|
-
@slave_connections.
|
10
|
+
@slave_connections.each_value{|c| c && c.disconnect! rescue nil}
|
11
11
|
end
|
12
12
|
@slave_connections = {}
|
13
13
|
end
|
14
14
|
|
15
15
|
def slave_access_in
|
16
|
-
Thread.current[:
|
16
|
+
Thread.current[:fresh_connection_slave_access] = true
|
17
17
|
end
|
18
18
|
|
19
19
|
def slave_access_out
|
20
|
-
Thread.current[:
|
20
|
+
Thread.current[:fresh_connection_slave_access] = false
|
21
21
|
end
|
22
22
|
|
23
23
|
def slave_access?
|
24
|
-
Thread.current[:
|
24
|
+
Thread.current[:fresh_connection_slave_access] ||= false
|
25
25
|
end
|
26
26
|
|
27
|
-
def
|
28
|
-
|
29
|
-
Thread.current[:slave_pointer] = 0 if slave_pointer > max_slave_pointer
|
27
|
+
def ignore_models=(model_names)
|
28
|
+
@ignore_models = model_names
|
30
29
|
end
|
31
|
-
|
32
|
-
private
|
33
30
|
|
34
|
-
def
|
35
|
-
|
31
|
+
def ignore_model?(model_name)
|
32
|
+
(@ignore_models || []).include?(model_name)
|
36
33
|
end
|
37
34
|
|
38
|
-
|
35
|
+
private
|
36
|
+
|
37
|
+
def slave_connection
|
39
38
|
@slave_connections ||= {}
|
40
|
-
@slave_connections[current_thread_id] ||=
|
39
|
+
@slave_connections[current_thread_id] ||= new_connection
|
41
40
|
end
|
42
41
|
|
43
|
-
|
44
|
-
|
45
|
-
Thread.current[:slave_pointer] ||= 0
|
42
|
+
def clear_slave_connection
|
43
|
+
@slave_connections[current_thread_id] = nil
|
46
44
|
end
|
47
45
|
|
48
|
-
def
|
49
|
-
|
46
|
+
def new_connection
|
47
|
+
ActiveRecord::Base.send("#{spec["adapter"]}_connection", spec)
|
50
48
|
end
|
51
49
|
|
52
50
|
def spec
|
data/lib/fresh_connection.rb
CHANGED
@@ -64,27 +64,34 @@ module ActiveRecord
|
|
64
64
|
private
|
65
65
|
|
66
66
|
def run_on_db(options)
|
67
|
-
in_run_on_db
|
68
|
-
|
69
|
-
|
70
|
-
in_run_on_db[:go_slave] ? run_on_readonly_db{yield} : yield
|
67
|
+
in_run_on_db(options)
|
68
|
+
run_on_db_status[:go_slave] ? run_on_readonly_db{yield} : yield
|
71
69
|
ensure
|
72
|
-
|
70
|
+
out_run_on_db
|
71
|
+
end
|
72
|
+
|
73
|
+
def in_run_on_db(options)
|
74
|
+
run_on_db_status[:go_slave] = go_slave?(options) if run_on_db_status[:count] == 0
|
75
|
+
run_on_db_status[:count] += 1
|
76
|
+
end
|
77
|
+
|
78
|
+
def out_run_on_db
|
79
|
+
run_on_db_status[:count] -= 1
|
73
80
|
end
|
74
81
|
|
75
|
-
def
|
76
|
-
Thread.current[:
|
82
|
+
def run_on_db_status
|
83
|
+
Thread.current[:run_on_db_status] ||= {:count => 0, :go_slave => false}
|
77
84
|
end
|
78
85
|
|
79
86
|
def go_slave?(options)
|
80
|
-
|
87
|
+
!FreshConnection::SlaveConnection.ignore_model?(self.name) && connection.open_transactions == 0 &&
|
88
|
+
(!options.is_a?(Hash) || !options.key?(:readonly) || options[:readonly].nil? || options[:readonly])
|
81
89
|
end
|
82
90
|
|
83
91
|
def run_on_readonly_db
|
84
92
|
FreshConnection::SlaveConnection.slave_access_in
|
85
93
|
yield
|
86
94
|
ensure
|
87
|
-
FreshConnection::SlaveConnection.shift_slave_pointer
|
88
95
|
FreshConnection::SlaveConnection.slave_access_out
|
89
96
|
end
|
90
97
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fresh_connection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Tsukasa OISHI
|
@@ -18,7 +18,7 @@ cert_chain: []
|
|
18
18
|
date: 2010-10-16 00:00:00 Z
|
19
19
|
dependencies: []
|
20
20
|
|
21
|
-
description:
|
21
|
+
description: FreshConnection supports of connect with Mysql slave servers via Load Balancers.
|
22
22
|
email:
|
23
23
|
- tsukasa.oishi@gmail.com
|
24
24
|
executables: []
|
@@ -39,10 +39,10 @@ files:
|
|
39
39
|
- History.txt
|
40
40
|
- Manifest.txt
|
41
41
|
- README.rdoc
|
42
|
-
homepage:
|
42
|
+
homepage: https://github.com/tsukasaoishi/fresh_connection
|
43
43
|
licenses: []
|
44
44
|
|
45
|
-
post_install_message:
|
45
|
+
post_install_message:
|
46
46
|
rdoc_options:
|
47
47
|
- --main
|
48
48
|
- README.rdoc
|
@@ -68,10 +68,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
68
|
version: "0"
|
69
69
|
requirements: []
|
70
70
|
|
71
|
-
rubyforge_project:
|
71
|
+
rubyforge_project: fresh_connection
|
72
72
|
rubygems_version: 1.7.2
|
73
73
|
signing_key:
|
74
74
|
specification_version: 3
|
75
|
-
summary:
|
75
|
+
summary: FreshConnection supports of connect with Mysql slave servers via Load Balancers.
|
76
76
|
test_files: []
|
77
77
|
|