ar_preconnect 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 73b2e902a43eaac8b8c50c79d61510affc1cb90a
4
+ data.tar.gz: cf6608f52194f084e6b20e28e7abdcbb48b23ccf
5
+ SHA512:
6
+ metadata.gz: 73eb0f3fa4b41bad4e95a07d63ce8599926b313e77e6794de0f70814d8049f5a0b46aa770628f1d54b09346b3c08216f39672b41f701df1ed64354c6831a5dfe
7
+ data.tar.gz: a9fcffd2872b72552a97b8882534cd92e8f7d6e6368989cf0c6798636f2292760f5b6385fb61647dd3a0871af0981677bac5f11daf263d905bf0ffed17b4d2ac
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Michael van Rooijen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,116 @@
1
+ ## ActiveRecord Preconnect
2
+
3
+ This library adds the `preconnect!` method to `ActiveRecord::ConnectionAdapters::ConnectionPool`. Use this when you want all of your connections in your connection pool to eagerly connect to the database, rather than lazily.
4
+
5
+
6
+ ### Why
7
+
8
+ When using [Postgres] on [Heroku] with [PGBouncer] you can run into situations where your [process will hang indefinitely]. This issue occurs when using ActiveRecord, Sequel and potentially other database mappers.
9
+
10
+ The solution in Sequel is to set the `preconnect` option to `true`. This option tells Sequel to open all database connection eagerly, rather than lazily, and avoids potential deadlocks. I couldn't find an equivalent option in ActiveRecord so I wrote this small library to provide a method to do so.
11
+
12
+
13
+ ### Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```rb
18
+ gem "ar_preconnect"
19
+ ```
20
+
21
+ ### Usage
22
+
23
+ With [Sidekiq]:
24
+
25
+ ```rb
26
+ # ./config/initializers/sidekiq.rb
27
+ Sidekiq.configure_server do |config|
28
+ ActiveRecord::Base.connection.pool.preconnect!
29
+ end
30
+ ```
31
+
32
+ With [Passenger] in cluster-mode.
33
+
34
+ ```rb
35
+ # ./config/initializers/passenger.rb
36
+ if defined?(PhusionPassenger)
37
+ PhusionPassenger.on_event(:starting_worker_process) do |forked|
38
+ if forked
39
+ ActiveRecord::Base.establish_connection
40
+ ActiveRecord::Base.connection.pool.preconnect!
41
+ end
42
+ end
43
+ end
44
+ ```
45
+
46
+ With [Unicorn] in cluster-mode.
47
+
48
+ ```rb
49
+ # ./config/initializers/unicorn.rb
50
+ before_fork do |server, worker|
51
+ ActiveRecord::Base.connection.disconnect!
52
+ end
53
+
54
+ after_fork do |server, worker|
55
+ ActiveRecord::Base.establish_connection
56
+ ActiveRecord::Base.connection.pool.preconnect!
57
+ end
58
+ ```
59
+
60
+ With [Puma] in cluster-mode.
61
+
62
+ ```rb
63
+ # ./config/initializers/puma.rb
64
+ on_worker_boot do
65
+ ActiveSupport.on_load(:active_record) do
66
+ ActiveRecord::Base.establish_connection
67
+ ActiveRecord::Base.connection.pool.preconnect!
68
+ end
69
+ end
70
+ ```
71
+
72
+ With [Clockwork]:
73
+
74
+ ```rb
75
+ # ./clock.rb
76
+ require "clockwork"
77
+ require_relative "config/boot"
78
+ require_relative "config/environment"
79
+
80
+ ActiveRecord::Base.connection.pool.preconnect!
81
+
82
+ module Clockwork
83
+ every(1.minute, "do something") do
84
+ MyWorker.perform_async
85
+ end
86
+ end
87
+ ```
88
+
89
+ ### Verify
90
+
91
+ Verify that this works:
92
+
93
+ ```rb
94
+ pool = ActiveRecord::Base.connection.pool
95
+
96
+ puts "#{pool.connections.count} connections established."
97
+ pool.preconnect!
98
+ puts "#{pool.connections.count} connections established."
99
+ ```
100
+
101
+
102
+ ### Author / License
103
+
104
+ Released under the [MIT License] by [Michael van Rooijen].
105
+
106
+ [Postgres]: http://www.postgresql.org
107
+ [Heroku]: https://www.heroku.com
108
+ [PGBouncer]: https://pgbouncer.github.io
109
+ [Sidekiq]: http://sidekiq.org
110
+ [Passenger]: https://www.phusionpassenger.com
111
+ [Unicorn]: http://unicorn.bogomips.org
112
+ [Puma]: http://puma.io
113
+ [Clockwork]: https://github.com/tomykaira/clockwork
114
+ [process will hang indefinitely]: https://github.com/heroku/heroku-buildpack-pgbouncer/issues/29
115
+ [MIT License]: https://github.com/meskyanichi/ar_preconnect/blob/master/LICENSE
116
+ [Michael van Rooijen]: http://michael.vanrooijen.io
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ar_preconnect"
8
+ spec.version = "0.1.0"
9
+ spec.authors = ["Michael van Rooijen"]
10
+ spec.email = ["michael@vanrooijen.io"]
11
+
12
+ spec.summary = %q{Adds the `preconnect!` method to `ActiveRecord::ConnectionAdapters::ConnectionPool`.}
13
+ spec.description = spec.summary + " With it you can eagerly connect all connection pool connections to the database, rather than lazily."
14
+ spec.homepage = "https://github.com/meskyanichi/ar_preconnect"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.10"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ end
@@ -0,0 +1,9 @@
1
+ class ActiveRecord::ConnectionAdapters::ConnectionPool
2
+
3
+ def preconnect!
4
+ (size - 1)
5
+ .times
6
+ .map { checkout }
7
+ .each { |c| checkin(c) }
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ar_preconnect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael van Rooijen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Adds the `preconnect!` method to `ActiveRecord::ConnectionAdapters::ConnectionPool`.
42
+ With it you can eagerly connect all connection pool connections to the database,
43
+ rather than lazily.
44
+ email:
45
+ - michael@vanrooijen.io
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - Gemfile
52
+ - LICENSE
53
+ - README.md
54
+ - Rakefile
55
+ - ar_preconnect.gemspec
56
+ - lib/ar_preconnect.rb
57
+ homepage: https://github.com/meskyanichi/ar_preconnect
58
+ licenses: []
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.4.5.1
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Adds the `preconnect!` method to `ActiveRecord::ConnectionAdapters::ConnectionPool`.
80
+ test_files: []