async-pool 0.3.11 → 0.4.0

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
  SHA256:
3
- metadata.gz: 2b18ddc4cbc74698b642f3196160fe5a59b93ae0484d25a84da43cd1b2a9c90d
4
- data.tar.gz: 668b970b18cae244620c289909f72d9fcfe1bef70771ddaa9a9a251615fbfd08
3
+ metadata.gz: 90ad67ae4201f83d1a5c9767b425ce1223d0b4d9c068a1046c98948875ec4c15
4
+ data.tar.gz: 84b8d54ad04302b935f65ca5e13c5929bda43bcf95754db9568f4c99ae7d5e92
5
5
  SHA512:
6
- metadata.gz: cb6353d6ca63c11516d67183f722ff50e78428a86112d7e6fbb097f3706b02cbefa0768e0c38de64f662882ee70bdd8555f9c3d9654810a88fecf7284213495b
7
- data.tar.gz: 211e98500aa73c4a1fa6ca0bc944f39e859c3383e08b323a396b0fbd245239fe36804482f9d896aeb555097c8b79eecd4df393f643905964eb136a6a0a98c742
6
+ metadata.gz: 3871fcd341afbc5db9af1f6e70ff6b58aeca070f9f8e89a5c4680e20feb33baa4c5d4c946bf2378fc526fa2dc021648ba0ec2b34b08a0de6cf2205ea6065b88f
7
+ data.tar.gz: 32f66fb6d31ab5413dd104061fd0afc8b3fdfb07d1644a3d315ac40c469cb985d976da393e9b54fb13fd6fff5b5a2680f7c0f00a506c52c735a068e074f890d1
checksums.yaml.gz.sig CHANGED
Binary file
@@ -1,22 +1,8 @@
1
- # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to deal
5
- # in the Software without restriction, including without limitation the rights
6
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- # copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
- #
10
- # The above copyright notice and this permission notice shall be included in
11
- # all copies or substantial portions of the Software.
12
- #
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- # THE SOFTWARE.
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2019-2022, by Samuel Williams.
5
+ # Copyright, 2020, by Simon Perepelitsa.
20
6
 
21
7
  require 'console/logger'
22
8
 
@@ -31,7 +17,16 @@ module Async
31
17
  self.new(block, **options)
32
18
  end
33
19
 
34
- def initialize(constructor, limit: nil, concurrency: nil)
20
+ def initialize(constructor, limit: nil, concurrency: (limit || 1), policy: nil)
21
+ @constructor = constructor
22
+ @limit = limit
23
+
24
+ # This semaphore is used to limit the number of concurrent tasks which are creating new resources.
25
+ @guard = Async::Semaphore.new(concurrency)
26
+
27
+ @policy = policy
28
+ @gardener = nil
29
+
35
30
  # All available resources:
36
31
  @resources = {}
37
32
 
@@ -39,24 +34,28 @@ module Async
39
34
  # This list may contain false positives, or resources which were okay but have since entered a state which is unusuable.
40
35
  @available = []
41
36
 
37
+ # Used to signal when a resource has been released:
42
38
  @notification = Async::Notification.new
43
-
44
- @limit = limit
45
-
46
- @constructor = constructor
47
-
48
- # Set the concurrency to be the same as the limit for maximum performance:
49
- if limit
50
- concurrency ||= limit
51
- else
52
- concurrency ||= 1
53
- end
54
-
55
- @guard = Async::Semaphore.new(concurrency)
56
-
57
- @gardener = nil
58
39
  end
59
40
 
41
+ # @attribute [Proc] The constructor used to create new resources.
42
+ attr :constructor
43
+
44
+ # @attribute [Integer] The maximum number of resources that this pool can have at any given time.
45
+ attr_accessor :limit
46
+
47
+ # @attribute [Integer] The maximum number of concurrent tasks that can be creating a new resource.
48
+ def concurrency
49
+ @guard.limit
50
+ end
51
+
52
+ def concurrency= value
53
+ @guard.limit = value
54
+ end
55
+
56
+ # @attribute [Policy] The pool policy.
57
+ attr_accessor :policy
58
+
60
59
  # @attribute [Hash(Resource, Integer)] all allocated resources, and their associated usage.
61
60
  attr :resources
62
61
 
@@ -112,6 +111,8 @@ module Async
112
111
  if resource.reusable?
113
112
  processed = reuse(resource)
114
113
  end
114
+
115
+ # @policy.released(self, resource)
115
116
  ensure
116
117
  retire(resource) unless processed
117
118
  end
@@ -119,8 +120,15 @@ module Async
119
120
  def close
120
121
  @available.clear
121
122
 
122
- @resources.each_key(&:close)
123
- @resources.clear
123
+ while pair = @resources.shift
124
+ resource, usage = pair
125
+
126
+ if usage > 0
127
+ Console.logger.warn(self, resource: resource, usage: usage) {"Closing resource while still in use!"}
128
+ end
129
+
130
+ resource.close
131
+ end
124
132
 
125
133
  @gardener&.stop
126
134
  end
@@ -139,12 +147,14 @@ module Async
139
147
  def prune(retain = 0)
140
148
  unused = []
141
149
 
150
+ # This code must not context switch:
142
151
  @resources.each do |resource, usage|
143
152
  if usage.zero?
144
153
  unused << resource
145
154
  end
146
155
  end
147
156
 
157
+ # It's okay for this to context switch:
148
158
  unused.each do |resource|
149
159
  if block_given?
150
160
  yield resource
@@ -186,7 +196,15 @@ module Async
186
196
  Async(transient: true, annotation: "#{self.class} Gardener") do |task|
187
197
  @gardener = task
188
198
 
189
- Task.yield
199
+ while true
200
+ if @policy
201
+ @policy.call(self)
202
+ else
203
+ Task.yield
204
+ end
205
+
206
+ self.wait
207
+ end
190
208
  ensure
191
209
  @gardener = nil
192
210
  self.close
@@ -198,18 +216,18 @@ module Async
198
216
  end
199
217
 
200
218
  def availability_string
201
- @resources.collect do |resource,usage|
219
+ @resources.collect do |resource, usage|
202
220
  "#{usage}/#{resource.concurrency}#{resource.viable? ? nil : '*'}/#{resource.count}"
203
221
  end.join(";")
204
222
  end
205
223
 
206
- def usage
207
- @resources.count{|resource, usage| usage > 0}
208
- end
209
-
210
- def free
211
- @resources.count{|resource, usage| usage == 0}
212
- end
224
+ # def usage
225
+ # @resources.count{|resource, usage| usage > 0}
226
+ # end
227
+ #
228
+ # def free
229
+ # @resources.count{|resource, usage| usage == 0}
230
+ # end
213
231
 
214
232
  def reuse(resource)
215
233
  Console.logger.debug(self) {"Reuse #{resource}"}
@@ -260,6 +278,8 @@ module Async
260
278
  end
261
279
  end
262
280
 
281
+ # @policy.created(self, resource)
282
+
263
283
  return resource
264
284
  end
265
285
 
@@ -277,7 +297,9 @@ module Async
277
297
  raise
278
298
  end
279
299
 
280
- private def get_resource
300
+ private
301
+
302
+ def get_resource
281
303
  while resource = @available.last
282
304
  if usage = @resources[resource] and usage < resource.concurrency
283
305
  if resource.viable?
@@ -1,22 +1,7 @@
1
- # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to deal
5
- # in the Software without restriction, including without limitation the rights
6
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- # copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
- #
10
- # The above copyright notice and this permission notice shall be included in
11
- # all copies or substantial portions of the Software.
12
- #
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- # THE SOFTWARE.
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2019-2022, by Samuel Williams.
20
5
 
21
6
  require 'console/logger'
22
7
 
@@ -1,25 +1,10 @@
1
- # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to deal
5
- # in the Software without restriction, including without limitation the rights
6
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- # copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
- #
10
- # The above copyright notice and this permission notice shall be included in
11
- # all copies or substantial portions of the Software.
12
- #
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- # THE SOFTWARE.
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2019-2022, by Samuel Williams.
20
5
 
21
6
  module Async
22
7
  module Pool
23
- VERSION = "0.3.11"
8
+ VERSION = "0.4.0"
24
9
  end
25
10
  end
data/lib/async/pool.rb CHANGED
@@ -1,23 +1,7 @@
1
- # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to deal
5
- # in the Software without restriction, including without limitation the rights
6
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- # copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
- #
10
- # The above copyright notice and this permission notice shall be included in
11
- # all copies or substantial portions of the Software.
12
- #
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- # THE SOFTWARE.
1
+ # frozen_string_literal: true
20
2
 
21
- require_relative 'pool/version'
3
+ # Released under the MIT License.
4
+ # Copyright, 2019-2022, by Samuel Williams.
22
5
 
6
+ require_relative 'pool/version'
23
7
  require_relative 'pool/controller'
data/license.md ADDED
@@ -0,0 +1,23 @@
1
+ # MIT License
2
+
3
+ Copyright, 2019-2023, by Samuel Williams.
4
+ Copyright, 2020, by Simon Perepelitsa.
5
+ Copyright, 2021, by Olle Jonsson.
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
data/readme.md ADDED
@@ -0,0 +1,54 @@
1
+ # Async::Pool
2
+
3
+ Provides support for connection pooling both singleplex and multiplex resources.
4
+
5
+ [![Development Status](https://github.com/socketry/async-pool/workflows/Test/badge.svg)](https://github.com/socketry/async-pool/actions?workflow=Test)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ``` ruby
12
+ gem 'async-pool'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ``` bash
18
+ $ bundle
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ``` bash
24
+ $ gem install async-pool
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ `Async::Pool::Controller` provides support for both singleplex (one stream at a time) and multiplex resources (multiple streams at a time).
30
+
31
+ `Async::Pool::Resource` is provided as an interface and to document how to use the pools. However, you wouldn't need to use this in practice and just implement the appropriate interface on your own objects.
32
+
33
+ ``` ruby
34
+ pool = Async::Pool::Controller.new(Async::Pool::Resource)
35
+
36
+ pool.acquire do |resource|
37
+ # resource is implicitly released when exiting the block.
38
+ end
39
+
40
+ resource = pool.acquire
41
+
42
+ # Return the resource back to the pool:
43
+ pool.release(resource)
44
+ ```
45
+
46
+ ## Contributing
47
+
48
+ We welcome contributions to this project.
49
+
50
+ 1. Fork it.
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`).
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`).
53
+ 4. Push to the branch (`git push origin my-new-feature`).
54
+ 5. Create new Pull Request.
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async-pool
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.11
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -39,7 +39,7 @@ cert_chain:
39
39
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
40
40
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
41
41
  -----END CERTIFICATE-----
42
- date: 2022-08-17 00:00:00.000000000 Z
42
+ date: 2023-03-12 00:00:00.000000000 Z
43
43
  dependencies:
44
44
  - !ruby/object:Gem::Dependency
45
45
  name: async
@@ -56,19 +56,33 @@ dependencies:
56
56
  - !ruby/object:Gem::Version
57
57
  version: '1.25'
58
58
  - !ruby/object:Gem::Dependency
59
- name: async-rspec
59
+ name: bake-test
60
60
  requirement: !ruby/object:Gem::Requirement
61
61
  requirements:
62
- - - "~>"
62
+ - - ">="
63
63
  - !ruby/object:Gem::Version
64
- version: '1.1'
64
+ version: '0'
65
65
  type: :development
66
66
  prerelease: false
67
67
  version_requirements: !ruby/object:Gem::Requirement
68
68
  requirements:
69
- - - "~>"
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ - !ruby/object:Gem::Dependency
73
+ name: bake-test-external
74
+ requirement: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
70
84
  - !ruby/object:Gem::Version
71
- version: '1.1'
85
+ version: '0'
72
86
  - !ruby/object:Gem::Dependency
73
87
  name: bundler
74
88
  requirement: !ruby/object:Gem::Requirement
@@ -98,19 +112,33 @@ dependencies:
98
112
  - !ruby/object:Gem::Version
99
113
  version: '0'
100
114
  - !ruby/object:Gem::Dependency
101
- name: rspec
115
+ name: sus
102
116
  requirement: !ruby/object:Gem::Requirement
103
117
  requirements:
104
118
  - - "~>"
105
119
  - !ruby/object:Gem::Version
106
- version: '3.6'
120
+ version: '0.15'
107
121
  type: :development
108
122
  prerelease: false
109
123
  version_requirements: !ruby/object:Gem::Requirement
110
124
  requirements:
111
125
  - - "~>"
112
126
  - !ruby/object:Gem::Version
113
- version: '3.6'
127
+ version: '0.15'
128
+ - !ruby/object:Gem::Dependency
129
+ name: sus-fixtures-async
130
+ requirement: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ type: :development
136
+ prerelease: false
137
+ version_requirements: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
114
142
  description:
115
143
  email:
116
144
  executables: []
@@ -121,6 +149,8 @@ files:
121
149
  - lib/async/pool/controller.rb
122
150
  - lib/async/pool/resource.rb
123
151
  - lib/async/pool/version.rb
152
+ - license.md
153
+ - readme.md
124
154
  homepage: https://github.com/socketry/async-pool
125
155
  licenses:
126
156
  - MIT
@@ -141,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
171
  - !ruby/object:Gem::Version
142
172
  version: '0'
143
173
  requirements: []
144
- rubygems_version: 3.1.6
174
+ rubygems_version: 3.4.7
145
175
  signing_key:
146
176
  specification_version: 4
147
177
  summary: A singleplex and multiplex resource pool for implementing robust clients.
metadata.gz.sig CHANGED
Binary file