activerecord-refresh_connection 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 52a01f4786ed07aa416bbd7e9bdb9af656fd2b77
4
- data.tar.gz: f213332480d2531baaea3352bfca9547573b825c
3
+ metadata.gz: e259517a798ec5e215eb057c367e1cc465998953
4
+ data.tar.gz: 1b1f7509fad904e1408d78aaaa100f3614d408af
5
5
  SHA512:
6
- metadata.gz: 056a7d5b883e637ecf7fb75bed37bb279ab9f4b3f5b75453628b6c1b8dfa96a86dcc8461e579696de83f56dbb705bd8d94ddff36532f4cd646f255e6e7f5b89d
7
- data.tar.gz: 85514a7826551be9fe120d49a8ddc343bdfe109aba2f1bb485bb94c4728ed79101295508c2b6fb5b892f2087e7c0c2f1c532a56e3ad2456f843577e6a948663d
6
+ metadata.gz: 6d592cb7813e276c1292378533238e8fd675bc07519c0b736da72c92bec9bf54c0622518199440c686c19caf3a5d17613e09980418a05f7342f354590d82959e
7
+ data.tar.gz: f0b89ad7852982561fb7a6f9084ff90205e011650ef635682133561c2a49b12248b92032cb205b8e498208670d2131bcab916f9f6da8eebbb3051090c3606416
@@ -1,3 +1,9 @@
1
+ ## 0.0.2
2
+
3
+ Enhancements:
4
+
5
+ * Add `max_requests` option (thanks to @ryotarai)
6
+
1
7
  ## 0.0.1
2
8
 
3
9
  First version
data/Gemfile CHANGED
@@ -1,5 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in sql-maker.gemspec
4
3
  gemspec
5
4
  gem 'coveralls', :require => false
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
- To Be Written
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
@@ -9,6 +9,6 @@ task :default => :spec
9
9
 
10
10
  desc 'Open an irb session preloaded with the gem library'
11
11
  task :console do
12
- sh 'irb -rubygems -I lib -r sql-maker'
12
+ sh 'irb -rubygems -I lib -r activerecord-refresh_connection'
13
13
  end
14
14
  task :c => :console
@@ -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.1"
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
- def initialize(app)
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! unless testing
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! unless testing
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.1
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-06-24 00:00:00.000000000 Z
11
+ date: 2014-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord