connection_pool 2.2.1 → 2.2.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: 7f87d3feec72672723a367bfa62719c8b7af8a35
4
- data.tar.gz: 51632684b4d5b93d6de0215180dc0f4e8a2ab922
3
+ metadata.gz: cf8177bf07f6c8e68b13f28416f6ccc0c64803af
4
+ data.tar.gz: ff9c836ce5576d4d0cb1edf8d858a761f982e437
5
5
  SHA512:
6
- metadata.gz: b3912e67f7b3f01f6e89047a56eb1f08f80820f5a154d0acde2d05f977d7566a33eacce3cee792ce2a52485b3218b9d718e4bacb89aafa050791166fb8d6e337
7
- data.tar.gz: 0d2e0ee5ad0c23db00b32737b82f0693cb70e2f5de0f7a5afa03c5ae7ac22765d13b0073086940c12edf6b5a00605e8db1a1ddecc80d373417448ba67e0bcfca
6
+ metadata.gz: 73bda4d8d0b3cc9daef5c93bb98b7649d811bd5c8f467e25c3aaf7d3353541225e7b30e1efd481a14e45f5c56b5fc846d49dabdd9a167702a71c4a9631a9b6a2
7
+ data.tar.gz: a331275f67b3635e09792e646113e32bdc74254a2cdff032e972c6f2ebedd6391a1e508c36be7c4e33b40e2a07efe029a2ad0267c3ba4dc20b22813df86427bb
@@ -3,12 +3,8 @@ sudo: false
3
3
  cache: bundler
4
4
  language: ruby
5
5
  rvm:
6
- - 1.9.3
7
- - 2.0.0
8
- - 2.1.6
9
- - 2.2.2
6
+ - 2.2.9
7
+ - 2.3.6
8
+ - 2.4.3
9
+ - 2.5.0
10
10
  - jruby
11
- - rbx-2
12
- notifications:
13
- email:
14
- - drbrain@segment7.net
data/Changes.md CHANGED
@@ -1,6 +1,12 @@
1
1
  connection\_pool changelog
2
2
  ---------------------------
3
3
 
4
+ 2.2.2
5
+ ------
6
+
7
+ - Add pool `size` and `available` accessors for metrics and monitoring
8
+ purposes [#97, robholland]
9
+
4
10
  2.2.1
5
11
  ------
6
12
 
@@ -51,6 +51,7 @@ class ConnectionPool
51
51
 
52
52
  @available = TimedStack.new(@size, &block)
53
53
  @key = :"current-#{@available.object_id}"
54
+ @key_count = :"current-#{@available.object_id}-count"
54
55
  end
55
56
 
56
57
  if Thread.respond_to?(:handle_interrupt)
@@ -84,20 +85,26 @@ else
84
85
  end
85
86
 
86
87
  def checkout(options = {})
87
- conn = if stack.empty?
88
- timeout = options[:timeout] || @timeout
89
- @available.pop(timeout: timeout)
88
+ if ::Thread.current[@key]
89
+ ::Thread.current[@key_count]+= 1
90
+ ::Thread.current[@key]
90
91
  else
91
- stack.last
92
+ ::Thread.current[@key_count]= 1
93
+ ::Thread.current[@key]= @available.pop(options[:timeout] || @timeout)
92
94
  end
93
-
94
- stack.push conn
95
- conn
96
95
  end
97
96
 
98
97
  def checkin
99
- conn = pop_connection # mutates stack, must be on its own line
100
- @available.push(conn) if stack.empty?
98
+ if ::Thread.current[@key]
99
+ if ::Thread.current[@key_count] == 1
100
+ @available.push(::Thread.current[@key])
101
+ ::Thread.current[@key]= nil
102
+ else
103
+ ::Thread.current[@key_count]-= 1
104
+ end
105
+ else
106
+ raise ConnectionPool::Error, 'no connections are checked out'
107
+ end
101
108
 
102
109
  nil
103
110
  end
@@ -106,20 +113,18 @@ end
106
113
  @available.shutdown(&block)
107
114
  end
108
115
 
109
- private
110
-
111
- def pop_connection
112
- if stack.empty?
113
- raise ConnectionPool::Error, 'no connections are checked out'
114
- else
115
- stack.pop
116
- end
116
+ # Size of this connection pool
117
+ def size
118
+ @size
117
119
  end
118
120
 
119
- def stack
120
- ::Thread.current[@key] ||= []
121
+ # Number of pool entries available for checkout at this instant.
122
+ def available
123
+ @available.length
121
124
  end
122
125
 
126
+ private
127
+
123
128
  class Wrapper < ::BasicObject
124
129
  METHODS = [:with, :pool_shutdown]
125
130
 
@@ -135,6 +140,14 @@ end
135
140
  @pool.shutdown(&block)
136
141
  end
137
142
 
143
+ def pool_size
144
+ @pool.size
145
+ end
146
+
147
+ def pool_available
148
+ @pool.available
149
+ end
150
+
138
151
  def respond_to?(id, *args)
139
152
  METHODS.include?(id) || with { |c| c.respond_to?(id, *args) }
140
153
  end
@@ -28,6 +28,7 @@ class ConnectionPool::PoolShuttingDownError < RuntimeError; end
28
28
  # #=> raises Timeout::Error after 5 seconds
29
29
 
30
30
  class ConnectionPool::TimedStack
31
+ attr_reader :max
31
32
 
32
33
  ##
33
34
  # Creates a new pool with +size+ connections that are created from the given
@@ -1,3 +1,3 @@
1
1
  class ConnectionPool
2
- VERSION = "2.2.1"
2
+ VERSION = "2.2.2"
3
3
  end
@@ -498,4 +498,19 @@ class TestConnectionPool < Minitest::Test
498
498
 
499
499
  assert_equal ['with', 'wrapped'], recorder.calls
500
500
  end
501
+
502
+ def test_stats_without_active_connection
503
+ pool = ConnectionPool.new(size: 2) { NetworkConnection.new }
504
+
505
+ assert_equal(2, pool.size)
506
+ assert_equal(2, pool.available)
507
+ end
508
+
509
+ def test_stats_with_active_connection
510
+ pool = ConnectionPool.new(size: 2) { NetworkConnection.new }
511
+
512
+ pool.with do
513
+ assert_equal(1, pool.available)
514
+ end
515
+ end
501
516
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: connection_pool
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 2.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Perham
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-11-12 00:00:00.000000000 Z
12
+ date: 2018-05-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -96,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
96
  version: '0'
97
97
  requirements: []
98
98
  rubyforge_project:
99
- rubygems_version: 2.5.1
99
+ rubygems_version: 2.6.13
100
100
  signing_key:
101
101
  specification_version: 4
102
102
  summary: Generic connection pool for Ruby
@@ -104,4 +104,3 @@ test_files:
104
104
  - test/helper.rb
105
105
  - test/test_connection_pool.rb
106
106
  - test/test_connection_pool_timed_stack.rb
107
- has_rdoc: