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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3707472745859247112e68f0b348876811dcea97
4
- data.tar.gz: 032461a6457f8ff4b5f566ef54e4ace7160aeebd
3
+ metadata.gz: 5020cb29611bf24a7ee6063ce70c1df0c58fc241
4
+ data.tar.gz: 2c4e322e659d9cb8cc96c71a670c8cfff55bcbc7
5
5
  SHA512:
6
- metadata.gz: 504d3589083726b6403c1e828dbe5d4b692e6b0bd695c21422a23daad8f6427dc8151fb9f00dd1dd18c4929189dbbf621d736e7fc176b52fa9fe8f7d17e87649
7
- data.tar.gz: ead47efa46d780b2a1e0586c2a5be2347c1f4e8bf7a47323d38cd8b90d9f33af344d874642792a8c59ba01b3444627f24bca04ad33825b7a1c67a708c359076f
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
- #### config/database.yml
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
- ### use config/environment.rb if rails2.3
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
- require 'fresh_connection'
56
- ActionController::Dispatcher.middleware.swap ActiveRecord::ConnectionAdapters::ConnectionManagement, FreshConnection::Rack::ConnectionManagement
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
- @app.call(env)
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, :master_clear_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 clear_all_connections!
14
- if @slave_connections.present?
15
- @slave_connections.each_value{|c| c && c.disconnect! rescue nil}
16
- end
17
- @slave_connections = {}
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 master_clear_connection?
47
- @master_clear_connection || false
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 slave_connection
74
- @slave_connections ||= {}
75
- @slave_connections[current_thread_id] ||= new_connection
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
@@ -1,4 +1,4 @@
1
1
  module FreshConnection
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
4
4
 
@@ -1,4 +1,5 @@
1
1
  require "fresh_connection/version"
2
+ require "fresh_connection/connection_manager"
2
3
  require "fresh_connection/slave_connection"
3
4
  require "fresh_connection/rack/connection_management"
4
5
  require "fresh_connection/active_record/relation"
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.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