with_connection 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.3-p327@with_connection
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'rails', '~> 3.0.15'
4
+
5
+ group :development do
6
+ gem "rdoc", "~> 3.12"
7
+ gem "jeweler", "~> 1.8.4"
8
+ gem "rspec", ">= 0"
9
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,94 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.20)
6
+ actionpack (= 3.0.20)
7
+ mail (~> 2.2.19)
8
+ actionpack (3.0.20)
9
+ activemodel (= 3.0.20)
10
+ activesupport (= 3.0.20)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.5.0)
14
+ rack (~> 1.2.5)
15
+ rack-mount (~> 0.6.14)
16
+ rack-test (~> 0.5.7)
17
+ tzinfo (~> 0.3.23)
18
+ activemodel (3.0.20)
19
+ activesupport (= 3.0.20)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.5.0)
22
+ activerecord (3.0.20)
23
+ activemodel (= 3.0.20)
24
+ activesupport (= 3.0.20)
25
+ arel (~> 2.0.10)
26
+ tzinfo (~> 0.3.23)
27
+ activeresource (3.0.20)
28
+ activemodel (= 3.0.20)
29
+ activesupport (= 3.0.20)
30
+ activesupport (3.0.20)
31
+ arel (2.0.10)
32
+ builder (2.1.2)
33
+ diff-lcs (1.2.4)
34
+ erubis (2.6.6)
35
+ abstract (>= 1.0.0)
36
+ git (1.2.5)
37
+ i18n (0.5.0)
38
+ jeweler (1.8.4)
39
+ bundler (~> 1.0)
40
+ git (>= 1.2.5)
41
+ rake
42
+ rdoc
43
+ json (1.8.0)
44
+ mail (2.2.20)
45
+ activesupport (>= 2.3.6)
46
+ i18n (>= 0.4.0)
47
+ mime-types (~> 1.16)
48
+ treetop (~> 1.4.8)
49
+ mime-types (1.23)
50
+ polyglot (0.3.3)
51
+ rack (1.2.8)
52
+ rack-mount (0.6.14)
53
+ rack (>= 1.0.0)
54
+ rack-test (0.5.7)
55
+ rack (>= 1.0)
56
+ rails (3.0.20)
57
+ actionmailer (= 3.0.20)
58
+ actionpack (= 3.0.20)
59
+ activerecord (= 3.0.20)
60
+ activeresource (= 3.0.20)
61
+ activesupport (= 3.0.20)
62
+ bundler (~> 1.0)
63
+ railties (= 3.0.20)
64
+ railties (3.0.20)
65
+ actionpack (= 3.0.20)
66
+ activesupport (= 3.0.20)
67
+ rake (>= 0.8.7)
68
+ rdoc (~> 3.4)
69
+ thor (~> 0.14.4)
70
+ rake (10.0.4)
71
+ rdoc (3.12.2)
72
+ json (~> 1.4)
73
+ rspec (2.13.0)
74
+ rspec-core (~> 2.13.0)
75
+ rspec-expectations (~> 2.13.0)
76
+ rspec-mocks (~> 2.13.0)
77
+ rspec-core (2.13.1)
78
+ rspec-expectations (2.13.0)
79
+ diff-lcs (>= 1.1.3, < 2.0)
80
+ rspec-mocks (2.13.1)
81
+ thor (0.14.6)
82
+ treetop (1.4.12)
83
+ polyglot
84
+ polyglot (>= 0.3.1)
85
+ tzinfo (0.3.37)
86
+
87
+ PLATFORMS
88
+ ruby
89
+
90
+ DEPENDENCIES
91
+ jeweler (~> 1.8.4)
92
+ rails (~> 3.0.15)
93
+ rdoc (~> 3.12)
94
+ rspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Doug Youch
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = with_connection
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to with_connection
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2013 Doug Youch. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "with_connection"
18
+ gem.homepage = "http://github.com/dyouch5@yahoo.com/with_connection"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Generic connection pool for fibers and threads}
21
+ gem.description = %Q{Uses active record connection pool logic with the fiber_patch}
22
+ gem.email = "doug@sessionm.com"
23
+ gem.authors = ["Doug Youch"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'rdoc/task'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "sized_list #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,7 @@
1
+ require 'rails/all'
2
+
3
+ module WithConnection
4
+ extend ActiveSupport::Autoload
5
+
6
+ autoload :ConnectionPool
7
+ end
@@ -0,0 +1,62 @@
1
+ module WithConnection
2
+ class ConnectionPool < ActiveRecord::ConnectionAdapters::ConnectionPool
3
+ attr_reader :name
4
+
5
+ def initialize(name, spec)
6
+ super spec
7
+ @name = name
8
+ @disable_warning = !! spec.config[:disable_warning]
9
+ @debug_with_connection = !! spec.config[:debug_with_connection]
10
+ ConnectionManagement.connection_pools << self
11
+ end
12
+
13
+ def has_connection?
14
+ !! @reserved_connections[current_connection_id]
15
+ end
16
+
17
+ def with_connection_with_debug(&block)
18
+ @using_with_connection = true
19
+ with_connection_without_debug(&block).tap { @using_with_connection = false }
20
+ end
21
+ alias_method_chain :with_connection, :debug
22
+
23
+ def checkout_with_debug
24
+ if @debug_with_connection && ! @using_with_connection
25
+ Rails.logger.warn "#{name} not using with_connection, backtrace: #{caller.inspect}"
26
+ end
27
+ checkout_without_debug
28
+ end
29
+ alias_method_chain :checkout, :debug
30
+
31
+ def release_connection_with_warning
32
+ Rails.logger.warn "#{@name} connection was held by the request" if ! @disable_warning && has_connection?
33
+ release_connection
34
+ end
35
+
36
+ private
37
+ def new_connection
38
+ spec.adapter_method.call spec.config
39
+ end
40
+ end
41
+
42
+ class ConnectionManagement
43
+ @@connection_pools = []
44
+ def self.connection_pools
45
+ @@connection_pools
46
+ end
47
+
48
+ def initialize(app)
49
+ @app = app
50
+ end
51
+
52
+ def call(env)
53
+ @app.call(env)
54
+ ensure
55
+ # Don't return connection (and perform implicit rollback) if
56
+ # this request is a part of integration test
57
+ unless env.key?("rack.test")
58
+ self.class.connection_pools.each(&:release_connection_with_warning)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,137 @@
1
+ # Necessary monkeypatching to make AR fiber-friendly.
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters
5
+
6
+ def self.fiber_pools
7
+ @fiber_pools ||= []
8
+ end
9
+ def self.register_fiber_pool(fp)
10
+ fiber_pools << fp
11
+ end
12
+
13
+ class FiberedMonitor
14
+ class Queue
15
+ def initialize
16
+ @queue = []
17
+ end
18
+
19
+ def wait(timeout)
20
+ t = timeout || 5
21
+ fiber = Fiber.current
22
+ x = EM::Timer.new(t) do
23
+ @queue.delete(fiber)
24
+ fiber.resume(false)
25
+ end
26
+ @queue << fiber
27
+ Fiber.yield.tap do
28
+ x.cancel
29
+ end
30
+ end
31
+
32
+ def signal
33
+ fiber = @queue.pop
34
+ fiber.resume(true) if fiber
35
+ end
36
+ end
37
+
38
+ def synchronize
39
+ yield
40
+ end
41
+
42
+ def new_cond
43
+ Queue.new
44
+ end
45
+ end
46
+
47
+ # ActiveRecord's connection pool is based on threads. Since we are working
48
+ # with EM and a single thread, multiple fiber design, we need to provide
49
+ # our own connection pool that keys off of Fiber.current so that different
50
+ # fibers running in the same thread don't try to use the same connection.
51
+ class ConnectionPool
52
+ def initialize(spec)
53
+ @spec = spec
54
+
55
+ # The cache of reserved connections mapped to threads
56
+ @reserved_connections = {}
57
+
58
+ # The mutex used to synchronize pool access
59
+ @connection_mutex = FiberedMonitor.new
60
+ @queue = @connection_mutex.new_cond
61
+
62
+ # default 5 second timeout unless on ruby 1.9
63
+ @timeout = spec.config[:wait_timeout] || 5
64
+
65
+ # default max pool size to 5
66
+ @size = (spec.config[:pool] && spec.config[:pool].to_i) || 5
67
+
68
+ @connections = []
69
+ @checked_out = []
70
+ @automatic_reconnect = true
71
+ @tables = {}
72
+
73
+ @columns = Hash.new do |h, table_name|
74
+ h[table_name] = with_connection do |conn|
75
+
76
+ # Fetch a list of columns
77
+ conn.columns(table_name, "#{table_name} Columns").tap do |columns|
78
+
79
+ # set primary key information
80
+ columns.each do |column|
81
+ column.primary = column.name == primary_keys[table_name]
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ @columns_hash = Hash.new do |h, table_name|
88
+ h[table_name] = Hash[columns[table_name].map { |col|
89
+ [col.name, col]
90
+ }]
91
+ end
92
+
93
+ @primary_keys = Hash.new do |h, table_name|
94
+ h[table_name] = with_connection do |conn|
95
+ table_exists?(table_name) ? conn.primary_key(table_name) : 'id'
96
+ end
97
+ end
98
+ end
99
+
100
+ def clear_stale_cached_connections!
101
+ cache = @reserved_connections
102
+ keys = Set.new(cache.keys)
103
+
104
+ ActiveRecord::ConnectionAdapters.fiber_pools.each do |pool|
105
+ pool.busy_fibers.each_pair do |object_id, fiber|
106
+ keys.delete(object_id)
107
+ end
108
+ end
109
+
110
+ keys.each do |key|
111
+ next unless cache.has_key?(key)
112
+ checkin cache[key]
113
+ cache.delete(key)
114
+ end
115
+ end
116
+
117
+ private
118
+
119
+ def current_connection_id #:nodoc:
120
+ Fiber.current.object_id
121
+ end
122
+
123
+ def checkout_and_verify(c)
124
+ @checked_out << c
125
+ begin
126
+ c.run_callbacks :checkout
127
+ c.verify!
128
+ rescue Exception => e
129
+ @checked_out.delete c
130
+ raise e
131
+ end
132
+ c
133
+ end
134
+ end
135
+
136
+ end
137
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: with_connection
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Doug Youch
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.15
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.15
30
+ - !ruby/object:Gem::Dependency
31
+ name: rdoc
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '3.12'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3.12'
46
+ - !ruby/object:Gem::Dependency
47
+ name: jeweler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.8.4
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.4
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Uses active record connection pool logic with the fiber_patch
79
+ email: doug@sessionm.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files:
83
+ - LICENSE.txt
84
+ - README.rdoc
85
+ files:
86
+ - .document
87
+ - .rvmrc
88
+ - Gemfile
89
+ - Gemfile.lock
90
+ - LICENSE.txt
91
+ - README.rdoc
92
+ - Rakefile
93
+ - VERSION
94
+ - lib/with_connection.rb
95
+ - lib/with_connection/connection_pool.rb
96
+ - lib/with_connection/fiber_patches.rb
97
+ homepage: http://github.com/dyouch5@yahoo.com/with_connection
98
+ licenses:
99
+ - MIT
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ segments:
111
+ - 0
112
+ hash: -1538544343472523699
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.23
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Generic connection pool for fibers and threads
125
+ test_files: []