activerecord-refresh_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.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e259517a798ec5e215eb057c367e1cc465998953
|
4
|
+
data.tar.gz: 1b1f7509fad904e1408d78aaaa100f3614d408af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d592cb7813e276c1292378533238e8fd675bc07519c0b736da72c92bec9bf54c0622518199440c686c19caf3a5d17613e09980418a05f7342f354590d82959e
|
7
|
+
data.tar.gz: f0b89ad7852982561fb7a6f9084ff90205e011650ef635682133561c2a49b12248b92032cb205b8e498208670d2131bcab916f9f6da8eebbb3051090c3606416
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -22,7 +22,7 @@ $ bundle
|
|
22
22
|
|
23
23
|
## How to Use
|
24
24
|
|
25
|
-
This gem provides a rack middleware `ActiveRecord::ConnectionAdapters::RefreshConnectionManagement` which disconnects all connections in each rack request.
|
25
|
+
This gem provides a rack middleware `ActiveRecord::ConnectionAdapters::RefreshConnectionManagement` which disconnects all connections in each rack request, which results in refreshing all connections in each rack request.
|
26
26
|
|
27
27
|
### Rails
|
28
28
|
|
@@ -30,12 +30,14 @@ Swap the default rails ConnectionManagement.
|
|
30
30
|
|
31
31
|
```ruby
|
32
32
|
# config/application.rb
|
33
|
-
require 'activerecord-refresh_connection'
|
34
|
-
|
35
33
|
class Application < Rails::Application
|
36
|
-
config.autoload_paths += %W(#{config.root}/lib)
|
37
34
|
config.middleware.swap ActiveRecord::ConnectionAdapters::ConnectionManagement,
|
38
35
|
"ActiveRecord::ConnectionAdapters::RefreshConnectionManagement"
|
36
|
+
|
37
|
+
## If you would like to clear connections after 5 requests:
|
38
|
+
# config.middleware.insert_before ActiveRecord::ConnectionAdapters::ConnectionManagement,
|
39
|
+
# "ActiveRecord::ConnectionAdapters::RefreshConnectionManagement", max_requests: 5
|
40
|
+
# config.middleware.delete ActiveRecord::ConnectionAdapters::ConnectionManagement
|
39
41
|
end
|
40
42
|
```
|
41
43
|
|
@@ -47,7 +49,21 @@ bundle exec rake middleware
|
|
47
49
|
|
48
50
|
### Sinatra
|
49
51
|
|
50
|
-
|
52
|
+
```ruby
|
53
|
+
# config.ru
|
54
|
+
require 'activerecord-refresh_connection'
|
55
|
+
|
56
|
+
use ActiveRecord::ConnectionAdapters::RefreshConnectionManagement
|
57
|
+
|
58
|
+
## If you would like to clear connections after 5 requests:
|
59
|
+
# use ActiveRecord::ConnectionAdapters::RefreshConnectionManagement, max_requests: 5
|
60
|
+
|
61
|
+
run App
|
62
|
+
```
|
63
|
+
|
64
|
+
## See Also
|
65
|
+
|
66
|
+
* [例えば ActiveRecord の connection_pool を止める - sonots:blog](http://blog.livedoor.jp/sonots/archives/38797925.html) (Japanese)
|
51
67
|
|
52
68
|
## ChangeLog
|
53
69
|
|
data/Rakefile
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "activerecord-refresh_connection"
|
7
|
-
spec.version = "0.0.
|
7
|
+
spec.version = "0.0.2"
|
8
8
|
spec.authors = ["Naotoshi Seo"]
|
9
9
|
spec.email = ["sonots@gmail.com"]
|
10
10
|
spec.summary = %q{Refresh ActiveRecord connection on each rack request}
|
@@ -1,24 +1,54 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module ConnectionAdapters
|
3
3
|
class RefreshConnectionManagement
|
4
|
-
|
4
|
+
DEFAULT_OPTIONS = {max_requests: 1}
|
5
|
+
|
6
|
+
def initialize(app, options)
|
5
7
|
@app = app
|
8
|
+
@options = DEFAULT_OPTIONS.merge(options)
|
9
|
+
@mutex = Mutex.new
|
10
|
+
|
11
|
+
reset_remain_count
|
6
12
|
end
|
7
13
|
|
8
14
|
def call(env)
|
9
15
|
testing = env.key?('rack.test')
|
10
16
|
|
11
17
|
response = @app.call(env)
|
18
|
+
|
19
|
+
clear_connections = should_clear_connections? && !testing
|
20
|
+
|
12
21
|
response[2] = ::Rack::BodyProxy.new(response[2]) do
|
13
22
|
# disconnect all connections on the connection pool
|
14
|
-
ActiveRecord::Base.clear_all_connections!
|
23
|
+
ActiveRecord::Base.clear_all_connections! if clear_connections
|
15
24
|
end
|
16
25
|
|
17
26
|
response
|
18
27
|
rescue Exception
|
19
|
-
ActiveRecord::Base.clear_all_connections!
|
28
|
+
ActiveRecord::Base.clear_all_connections! if clear_connections
|
20
29
|
raise
|
21
30
|
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def should_clear_connections?
|
35
|
+
return true if max_requests <= 1
|
36
|
+
|
37
|
+
@mutex.synchronize do
|
38
|
+
@remain_count -= 1
|
39
|
+
(@remain_count <= 0).tap do |clear|
|
40
|
+
reset_remain_count if clear
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def reset_remain_count
|
46
|
+
@remain_count = max_requests
|
47
|
+
end
|
48
|
+
|
49
|
+
def max_requests
|
50
|
+
@options[:max_requests]
|
51
|
+
end
|
22
52
|
end
|
23
53
|
end
|
24
54
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-refresh_connection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Naotoshi Seo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|