async-pool 0.3.12 → 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: 0b1051396c5d474aee1dc09c05774e39d7f694220f39b70d13ba28296521fbfa
4
- data.tar.gz: d217b6e200c52dba446af0c8677f6f5874ed5114607cccff941ac2c885fe7ad7
3
+ metadata.gz: 90ad67ae4201f83d1a5c9767b425ce1223d0b4d9c068a1046c98948875ec4c15
4
+ data.tar.gz: 84b8d54ad04302b935f65ca5e13c5929bda43bcf95754db9568f4c99ae7d5e92
5
5
  SHA512:
6
- metadata.gz: 0c78ae68e5f37b5eef4e385db3b86d355143aa0fd1bd64aef84da4963b508ca621b4141aced44d4a649b20824203e0acda1474c05cadb456d2d277ed0ff3121e
7
- data.tar.gz: 94b3019c786c2bac9d05ea1725651f35826f84050adb9813fcdfd12850619b349f1edfb229923fd5a9c4964c24b6c9e94d65fb0d6b970bc20559ce274c2928bf
6
+ metadata.gz: 3871fcd341afbc5db9af1f6e70ff6b58aeca070f9f8e89a5c4680e20feb33baa4c5d4c946bf2378fc526fa2dc021648ba0ec2b34b08a0de6cf2205ea6065b88f
7
+ data.tar.gz: 32f66fb6d31ab5413dd104061fd0afc8b3fdfb07d1644a3d315ac40c469cb985d976da393e9b54fb13fd6fff5b5a2680f7c0f00a506c52c735a068e074f890d1
checksums.yaml.gz.sig CHANGED
Binary file
@@ -17,7 +17,16 @@ module Async
17
17
  self.new(block, **options)
18
18
  end
19
19
 
20
- 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
+
21
30
  # All available resources:
22
31
  @resources = {}
23
32
 
@@ -25,24 +34,28 @@ module Async
25
34
  # This list may contain false positives, or resources which were okay but have since entered a state which is unusuable.
26
35
  @available = []
27
36
 
37
+ # Used to signal when a resource has been released:
28
38
  @notification = Async::Notification.new
29
-
30
- @limit = limit
31
-
32
- @constructor = constructor
33
-
34
- # Set the concurrency to be the same as the limit for maximum performance:
35
- if limit
36
- concurrency ||= limit
37
- else
38
- concurrency ||= 1
39
- end
40
-
41
- @guard = Async::Semaphore.new(concurrency)
42
-
43
- @gardener = nil
44
39
  end
45
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
+
46
59
  # @attribute [Hash(Resource, Integer)] all allocated resources, and their associated usage.
47
60
  attr :resources
48
61
 
@@ -98,6 +111,8 @@ module Async
98
111
  if resource.reusable?
99
112
  processed = reuse(resource)
100
113
  end
114
+
115
+ # @policy.released(self, resource)
101
116
  ensure
102
117
  retire(resource) unless processed
103
118
  end
@@ -181,7 +196,15 @@ module Async
181
196
  Async(transient: true, annotation: "#{self.class} Gardener") do |task|
182
197
  @gardener = task
183
198
 
184
- 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
185
208
  ensure
186
209
  @gardener = nil
187
210
  self.close
@@ -193,18 +216,18 @@ module Async
193
216
  end
194
217
 
195
218
  def availability_string
196
- @resources.collect do |resource,usage|
219
+ @resources.collect do |resource, usage|
197
220
  "#{usage}/#{resource.concurrency}#{resource.viable? ? nil : '*'}/#{resource.count}"
198
221
  end.join(";")
199
222
  end
200
223
 
201
- def usage
202
- @resources.count{|resource, usage| usage > 0}
203
- end
204
-
205
- def free
206
- @resources.count{|resource, usage| usage == 0}
207
- 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
208
231
 
209
232
  def reuse(resource)
210
233
  Console.logger.debug(self) {"Reuse #{resource}"}
@@ -255,6 +278,8 @@ module Async
255
278
  end
256
279
  end
257
280
 
281
+ # @policy.created(self, resource)
282
+
258
283
  return resource
259
284
  end
260
285
 
@@ -272,7 +297,9 @@ module Async
272
297
  raise
273
298
  end
274
299
 
275
- private def get_resource
300
+ private
301
+
302
+ def get_resource
276
303
  while resource = @available.last
277
304
  if usage = @resources[resource] and usage < resource.concurrency
278
305
  if resource.viable?
@@ -5,6 +5,6 @@
5
5
 
6
6
  module Async
7
7
  module Pool
8
- VERSION = "0.3.12"
8
+ VERSION = "0.4.0"
9
9
  end
10
10
  end
data/license.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # MIT License
2
2
 
3
- Copyright, 2019-2022, by Samuel Williams.
3
+ Copyright, 2019-2023, by Samuel Williams.
4
4
  Copyright, 2020, by Simon Perepelitsa.
5
5
  Copyright, 2021, by Olle Jonsson.
6
6
 
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.12
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-28 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
@@ -117,14 +117,14 @@ dependencies:
117
117
  requirements:
118
118
  - - "~>"
119
119
  - !ruby/object:Gem::Version
120
- version: '0.12'
120
+ version: '0.15'
121
121
  type: :development
122
122
  prerelease: false
123
123
  version_requirements: !ruby/object:Gem::Requirement
124
124
  requirements:
125
125
  - - "~>"
126
126
  - !ruby/object:Gem::Version
127
- version: '0.12'
127
+ version: '0.15'
128
128
  - !ruby/object:Gem::Dependency
129
129
  name: sus-fixtures-async
130
130
  requirement: !ruby/object:Gem::Requirement
@@ -171,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
171
171
  - !ruby/object:Gem::Version
172
172
  version: '0'
173
173
  requirements: []
174
- rubygems_version: 3.1.6
174
+ rubygems_version: 3.4.7
175
175
  signing_key:
176
176
  specification_version: 4
177
177
  summary: A singleplex and multiplex resource pool for implementing robust clients.
metadata.gz.sig CHANGED
Binary file