connection_pool 0.0.3 → 0.1.0
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.
- data/Changes.md +5 -0
- data/README.md +7 -1
- data/lib/connection_pool.rb +11 -14
- data/lib/connection_pool/version.rb +2 -2
- data/lib/{connection_pool/timed_queue.rb → timed_queue.rb} +3 -2
- metadata +5 -6
data/Changes.md
ADDED
data/README.md
CHANGED
@@ -5,6 +5,12 @@ Generic connection pooling for Ruby.
|
|
5
5
|
|
6
6
|
MongoDB has its own connection pool. ActiveRecord has its own connection pool. This is a generic connection pool that can be used with anything, e.g. Redis, Dalli and other Ruby network clients.
|
7
7
|
|
8
|
+
Requirements
|
9
|
+
--------------
|
10
|
+
|
11
|
+
connection_pool requires Ruby 1.9 because it uses BasicObject.
|
12
|
+
|
13
|
+
|
8
14
|
Install
|
9
15
|
------------
|
10
16
|
|
@@ -27,4 +33,4 @@ Then use the pool in your application:
|
|
27
33
|
Author
|
28
34
|
--------------
|
29
35
|
|
30
|
-
Mike Perham, [@mperham](https://twitter.com/mperham), <http://mikeperham.com>
|
36
|
+
Mike Perham, [@mperham](https://twitter.com/mperham), <http://mikeperham.com>
|
data/lib/connection_pool.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'timed_queue'
|
2
2
|
|
3
3
|
# Generic connection pool class for e.g. sharing a limited number of network connections
|
4
4
|
# among many threads. Note: Connections are eager created.
|
@@ -23,18 +23,18 @@ require 'connection_pool/timed_queue'
|
|
23
23
|
# - :size - number of connections to pool, defaults to 5
|
24
24
|
# - :timeout - amount of time to wait for a connection if none currently available, defaults to 5 seconds
|
25
25
|
#
|
26
|
-
class ConnectionPool
|
26
|
+
class ConnectionPool < BasicObject
|
27
27
|
DEFAULTS = { :size => 5, :timeout => 5 }
|
28
28
|
|
29
|
-
def initialize(options={})
|
30
|
-
raise ArgumentError, 'Connection pool requires a block' unless
|
29
|
+
def initialize(options={}, &block)
|
30
|
+
::Kernel.raise ::ArgumentError, 'Connection pool requires a block' unless block
|
31
31
|
|
32
|
-
@available = TimedQueue.new
|
32
|
+
@available = ::TimedQueue.new
|
33
|
+
@oid = @available.object_id
|
33
34
|
@options = DEFAULTS.merge(options)
|
34
35
|
@options[:size].times do
|
35
|
-
@available <<
|
36
|
+
@available << block.call
|
36
37
|
end
|
37
|
-
@busy = []
|
38
38
|
end
|
39
39
|
|
40
40
|
def with(&block)
|
@@ -53,18 +53,15 @@ class ConnectionPool
|
|
53
53
|
private
|
54
54
|
|
55
55
|
def checkout
|
56
|
-
Thread.current[:"current-#{
|
57
|
-
|
58
|
-
@busy << conn
|
59
|
-
conn
|
56
|
+
::Thread.current[:"current-#{@oid}"] ||= begin
|
57
|
+
@available.timed_pop(@options[:timeout])
|
60
58
|
end
|
61
59
|
end
|
62
60
|
|
63
61
|
def checkin
|
64
|
-
conn = Thread.current[:"current-#{
|
65
|
-
Thread.current[:"current-#{
|
62
|
+
conn = ::Thread.current[:"current-#{@oid}"]
|
63
|
+
::Thread.current[:"current-#{@oid}"] = nil
|
66
64
|
return unless conn
|
67
|
-
@busy.delete(conn)
|
68
65
|
@available << conn
|
69
66
|
nil
|
70
67
|
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION = "0.0
|
1
|
+
class ConnectionPool
|
2
|
+
VERSION = "0.1.0"
|
3
3
|
end
|
@@ -21,8 +21,9 @@ class TimedQueue
|
|
21
21
|
@mutex.synchronize do
|
22
22
|
loop do
|
23
23
|
return @que.shift unless @que.empty?
|
24
|
-
|
25
|
-
|
24
|
+
to_wait = deadline - Time.now
|
25
|
+
raise Timeout::Error, "Waited #{timeout} sec" if to_wait <= 0
|
26
|
+
@resource.wait(@mutex, to_wait)
|
26
27
|
end
|
27
28
|
end
|
28
29
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: connection_pool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0
|
5
|
+
version: 0.1.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Mike Perham
|
@@ -10,8 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-09-
|
14
|
-
default_executable:
|
13
|
+
date: 2011-09-19 00:00:00 Z
|
15
14
|
dependencies: []
|
16
15
|
|
17
16
|
description: Generic connection pool for Ruby
|
@@ -25,17 +24,17 @@ extra_rdoc_files: []
|
|
25
24
|
|
26
25
|
files:
|
27
26
|
- .gitignore
|
27
|
+
- Changes.md
|
28
28
|
- Gemfile
|
29
29
|
- LICENSE
|
30
30
|
- README.md
|
31
31
|
- Rakefile
|
32
32
|
- connection_pool.gemspec
|
33
33
|
- lib/connection_pool.rb
|
34
|
-
- lib/connection_pool/timed_queue.rb
|
35
34
|
- lib/connection_pool/version.rb
|
35
|
+
- lib/timed_queue.rb
|
36
36
|
- test/helper.rb
|
37
37
|
- test/test_connection_pool.rb
|
38
|
-
has_rdoc: true
|
39
38
|
homepage: ""
|
40
39
|
licenses: []
|
41
40
|
|
@@ -59,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
58
|
requirements: []
|
60
59
|
|
61
60
|
rubyforge_project:
|
62
|
-
rubygems_version: 1.
|
61
|
+
rubygems_version: 1.8.10
|
63
62
|
signing_key:
|
64
63
|
specification_version: 3
|
65
64
|
summary: Generic connection pool for Ruby
|