fresh_connection 0.1.1 → 0.1.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.
- checksums.yaml +4 -4
- data/README.md +20 -5
- data/lib/fresh_connection/connection_manager.rb +34 -0
- data/lib/fresh_connection/rack/connection_management.rb +3 -14
- data/lib/fresh_connection/slave_connection.rb +12 -28
- data/lib/fresh_connection/version.rb +1 -1
- data/lib/fresh_connection.rb +1 -0
- 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: 5020cb29611bf24a7ee6063ce70c1df0c58fc241
|
4
|
+
data.tar.gz: 2c4e322e659d9cb8cc96c71a670c8cfff55bcbc7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a9e952072b5549d5b36fb2702f9ddee6a0299b9e8360c9efb668afd7b5684d5d214d79f17d82fc619185de642668a03cdd74d046a119afebe4f3c75c274d7afe
|
7
|
+
data.tar.gz: 886a5c6650c1fc07cdce4191b0b3d00b808dc3219cb1169f62b0a2027d4c72b529b7779241e64085d0122d283b5bc4d85597067501429d8e5f3ffa1c05714ccb
|
data/README.md
CHANGED
@@ -5,7 +5,7 @@ All connections will be disconnected every time at the end of the action.
|
|
5
5
|
|
6
6
|
## Installation
|
7
7
|
|
8
|
-
### For Rails3
|
8
|
+
### For Rails3 and 4
|
9
9
|
Add this line to your application's Gemfile:
|
10
10
|
|
11
11
|
gem "fresh_connection"
|
@@ -23,7 +23,7 @@ Or install it yourself as:
|
|
23
23
|
$ gem install fresh_connection -v 0.0.7
|
24
24
|
|
25
25
|
## Config
|
26
|
-
|
26
|
+
### config/database.yml
|
27
27
|
|
28
28
|
production:
|
29
29
|
adapter: mysql2
|
@@ -50,10 +50,24 @@ Others will use the master setting. If you want to change, write in the slave.
|
|
50
50
|
|
51
51
|
If models that ignore access to slave servers is exist, You can write model name at FreshConnection::SlaveConnection.ignore models.
|
52
52
|
|
53
|
-
###
|
53
|
+
### Slave Connection Manager
|
54
|
+
Default slave connection manager is FreshConnection::ConnectionManager.
|
55
|
+
If you would like to change slave connection manager, assign yourself slave connection manager.
|
54
56
|
|
55
|
-
|
56
|
-
|
57
|
+
#### config/initializers/fresh_connection.rb
|
58
|
+
|
59
|
+
FreshConnection::SlaveConnection.connection_manager = MySlaveConnection
|
60
|
+
|
61
|
+
|
62
|
+
Yourself Slave Connection Manager is required any instance methods.
|
63
|
+
|
64
|
+
def slave_connection
|
65
|
+
# must return object of ActiveRecord::ConnectionAdapters::Mysql2Adapter
|
66
|
+
end
|
67
|
+
|
68
|
+
def put_aside!
|
69
|
+
# called when end of Rails controller action
|
70
|
+
end
|
57
71
|
|
58
72
|
## Usage
|
59
73
|
Read query will be access to slave server.
|
@@ -71,6 +85,7 @@ In transaction, Always will be access to master server.
|
|
71
85
|
end
|
72
86
|
|
73
87
|
|
88
|
+
|
74
89
|
## Contributing
|
75
90
|
|
76
91
|
1. Fork it
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module FreshConnection
|
2
|
+
class ConnectionManager
|
3
|
+
def slave_connection
|
4
|
+
@slave_connections ||= {}
|
5
|
+
@slave_connections[current_thread_id] ||= new_connection
|
6
|
+
end
|
7
|
+
|
8
|
+
def put_aside!
|
9
|
+
if @slave_connections.present?
|
10
|
+
@slave_connections.each_value{|c| c && c.disconnect! rescue nil}
|
11
|
+
end
|
12
|
+
@slave_connections = {}
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def new_connection
|
18
|
+
ActiveRecord::Base.send("#{spec["adapter"]}_connection", spec)
|
19
|
+
end
|
20
|
+
|
21
|
+
def spec
|
22
|
+
@spec ||= get_spec
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_spec
|
26
|
+
ret = ActiveRecord::Base.configurations[Rails.env]
|
27
|
+
ret.merge(ret["slave"] || {})
|
28
|
+
end
|
29
|
+
|
30
|
+
def current_thread_id
|
31
|
+
Thread.current.object_id
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -1,21 +1,10 @@
|
|
1
1
|
module FreshConnection
|
2
2
|
module Rack
|
3
|
-
class ConnectionManagement
|
4
|
-
def initialize(app)
|
5
|
-
@app = app
|
6
|
-
end
|
7
|
-
|
3
|
+
class ConnectionManagement < ActiveRecord::ConnectionAdapters::ConnectionManagement
|
8
4
|
def call(env)
|
9
|
-
|
5
|
+
super
|
10
6
|
ensure
|
11
|
-
unless env.key?("rack.test")
|
12
|
-
if FreshConnection::SlaveConnection.master_clear_connection?
|
13
|
-
ActiveRecord::Base.clear_all_connections!
|
14
|
-
else
|
15
|
-
ActiveRecord::Base.clear_active_connections!
|
16
|
-
end
|
17
|
-
FreshConnection::SlaveConnection.clear_all_connections!
|
18
|
-
end
|
7
|
+
FreshConnection::SlaveConnection.put_aside! unless env.key?("rack.test")
|
19
8
|
end
|
20
9
|
end
|
21
10
|
end
|
@@ -4,17 +4,18 @@ module FreshConnection
|
|
4
4
|
TARGET = :fresh_connection_access_target
|
5
5
|
|
6
6
|
class << self
|
7
|
-
attr_writer :ignore_models, :ignore_configure_connection
|
7
|
+
attr_writer :ignore_models, :ignore_configure_connection
|
8
8
|
|
9
9
|
def raw_connection
|
10
10
|
slave_connection.raw_connection
|
11
11
|
end
|
12
12
|
|
13
|
-
def
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
def slave_connection
|
14
|
+
connection_manager.slave_connection
|
15
|
+
end
|
16
|
+
|
17
|
+
def put_aside!
|
18
|
+
connection_manager.put_aside!
|
18
19
|
end
|
19
20
|
|
20
21
|
def manage_access(model_name, go_slave, &block)
|
@@ -43,8 +44,8 @@ module FreshConnection
|
|
43
44
|
!!@ignore_configure_connection
|
44
45
|
end
|
45
46
|
|
46
|
-
def
|
47
|
-
@
|
47
|
+
def connection_manager=(manager)
|
48
|
+
@connection_manager_class = manager
|
48
49
|
end
|
49
50
|
|
50
51
|
private
|
@@ -70,26 +71,9 @@ module FreshConnection
|
|
70
71
|
end
|
71
72
|
end
|
72
73
|
|
73
|
-
def
|
74
|
-
@
|
75
|
-
|
76
|
-
end
|
77
|
-
|
78
|
-
def new_connection
|
79
|
-
ActiveRecord::Base.send("#{spec["adapter"]}_connection", spec)
|
80
|
-
end
|
81
|
-
|
82
|
-
def spec
|
83
|
-
@spec ||= get_spec
|
84
|
-
end
|
85
|
-
|
86
|
-
def get_spec
|
87
|
-
ret = ActiveRecord::Base.configurations[Rails.env]
|
88
|
-
ret.merge(ret["slave"] || {})
|
89
|
-
end
|
90
|
-
|
91
|
-
def current_thread_id
|
92
|
-
Thread.current.object_id
|
74
|
+
def connection_manager
|
75
|
+
@connection_manager ||=
|
76
|
+
(@connection_manager_class || FreshConnection::ConnectionManager).new
|
93
77
|
end
|
94
78
|
end
|
95
79
|
end
|
data/lib/fresh_connection.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fresh_connection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tsukasa OISHI
|
@@ -68,6 +68,7 @@ files:
|
|
68
68
|
- lib/fresh_connection/active_record/abstract_adapter.rb
|
69
69
|
- lib/fresh_connection/active_record/mysql2_adapter.rb
|
70
70
|
- lib/fresh_connection/active_record/relation.rb
|
71
|
+
- lib/fresh_connection/connection_manager.rb
|
71
72
|
- lib/fresh_connection/rack/connection_management.rb
|
72
73
|
- lib/fresh_connection/railtie.rb
|
73
74
|
- lib/fresh_connection/slave_connection.rb
|