async-pool 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/async/pool/controller.rb +69 -12
- data/lib/async/pool/resource.rb +3 -3
- data/lib/async/pool/version.rb +1 -1
- data/lib/async/pool.rb +2 -2
- data.tar.gz.sig +0 -0
- metadata +30 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 958468bc2e159bc6304f603d522ccc08fe967469bd463a992484f237e880ddf3
|
4
|
+
data.tar.gz: 2fcd90d3589cb71a9395b0e9b9810dc860bda4971c538e754d7a4113e1628ff0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a03eb3421cd11bc52ec995ddadd67c9e717763a69dbed5d8d27248f0b93ec9e824765eef5a8d02719968fc20f974d4b7690ec8a1e66fa6cc5d103f95f56ce0d
|
7
|
+
data.tar.gz: f4f19064995d171e013e5d80c90343387c733e9f13a4acd846c1534aee573ff7f75c5d3d41907919bdd83ff09bd1112e6a9d7f063ed66e4e072e91648220ae2b
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
@@ -5,11 +5,14 @@
|
|
5
5
|
# Copyright, 2020, by Simon Perepelitsa.
|
6
6
|
# Copyright, 2024, by Thomas Morgan.
|
7
7
|
|
8
|
-
require
|
8
|
+
require "console/logger"
|
9
9
|
|
10
|
-
require
|
11
|
-
require
|
12
|
-
require
|
10
|
+
require "async"
|
11
|
+
require "async/notification"
|
12
|
+
require "async/semaphore"
|
13
|
+
|
14
|
+
require "traces"
|
15
|
+
require "metrics"
|
13
16
|
|
14
17
|
module Async
|
15
18
|
module Pool
|
@@ -26,7 +29,7 @@ module Async
|
|
26
29
|
# @parameter limit [Integer | Nil] The maximum number of resources that this pool can have at any given time. If nil, the pool can have an unlimited number of resources.
|
27
30
|
# @parameter concurrency [Integer] The maximum number of concurrent tasks that can be creating a new resource.
|
28
31
|
# @parameter policy [Policy] The pool policy.
|
29
|
-
def initialize(constructor, limit: nil, concurrency: (limit || 1), policy: nil)
|
32
|
+
def initialize(constructor, limit: nil, concurrency: (limit || 1), policy: nil, tags: nil)
|
30
33
|
@constructor = constructor
|
31
34
|
@limit = limit
|
32
35
|
|
@@ -36,6 +39,8 @@ module Async
|
|
36
39
|
@policy = policy
|
37
40
|
@gardener = nil
|
38
41
|
|
42
|
+
@tags = Metrics::Tags.normalize(tags)
|
43
|
+
|
39
44
|
# All available resources:
|
40
45
|
@resources = {}
|
41
46
|
|
@@ -93,6 +98,9 @@ module Async
|
|
93
98
|
# @attribute [Hash(Resource, Integer)] all allocated resources, and their associated usage.
|
94
99
|
attr :resources
|
95
100
|
|
101
|
+
# @attribute [Array(String)] The name of the pool.
|
102
|
+
attr_accessor :tags
|
103
|
+
|
96
104
|
# The number of resources in the pool.
|
97
105
|
def size
|
98
106
|
@resources.size
|
@@ -154,7 +162,9 @@ module Async
|
|
154
162
|
retire(resource) unless processed
|
155
163
|
end
|
156
164
|
|
157
|
-
|
165
|
+
def drain
|
166
|
+
Console.debug(self, "Draining pool...", size: @resources.size)
|
167
|
+
|
158
168
|
# Enumerate all existing resources and retire them:
|
159
169
|
while resource = acquire_existing_resource
|
160
170
|
retire(resource)
|
@@ -262,6 +272,7 @@ module Async
|
|
262
272
|
|
263
273
|
def reuse(resource)
|
264
274
|
Console.debug(self) {"Reuse #{resource}"}
|
275
|
+
|
265
276
|
usage = @resources[resource]
|
266
277
|
|
267
278
|
if usage.nil? || usage.zero?
|
@@ -286,11 +297,7 @@ module Async
|
|
286
297
|
@notification.wait
|
287
298
|
end
|
288
299
|
|
289
|
-
|
290
|
-
|
291
|
-
# if resource.concurrency > 1
|
292
|
-
# @notification.signal
|
293
|
-
# end
|
300
|
+
# Be careful not to context switch or fail here.
|
294
301
|
|
295
302
|
return resource
|
296
303
|
end
|
@@ -318,12 +325,16 @@ module Async
|
|
318
325
|
def available_resource
|
319
326
|
resource = nil
|
320
327
|
|
328
|
+
Console.debug(self, "Acquiring concurrency guard...", blocking: @guard.blocking?)
|
329
|
+
|
321
330
|
@guard.acquire do
|
331
|
+
Console.debug(self, "Acquired concurrency guard.")
|
332
|
+
|
322
333
|
resource = acquire_or_create_resource
|
323
334
|
end
|
324
335
|
|
325
336
|
return resource
|
326
|
-
rescue Exception
|
337
|
+
rescue Exception => error
|
327
338
|
reuse(resource) if resource
|
328
339
|
raise
|
329
340
|
end
|
@@ -375,6 +386,52 @@ module Async
|
|
375
386
|
return create_resource
|
376
387
|
end
|
377
388
|
end
|
389
|
+
|
390
|
+
Traces::Provider(self) do
|
391
|
+
def create_resource(...)
|
392
|
+
attributes = {
|
393
|
+
concurrency: @guard.limit,
|
394
|
+
size: @resources.size,
|
395
|
+
limit: @limit,
|
396
|
+
tags: @tags,
|
397
|
+
}
|
398
|
+
|
399
|
+
Traces.trace("async.pool.create", attributes: attributes) {super}
|
400
|
+
end
|
401
|
+
|
402
|
+
def drain(...)
|
403
|
+
attributes = {
|
404
|
+
size: @resources.size,
|
405
|
+
tags: @tags,
|
406
|
+
}
|
407
|
+
|
408
|
+
Traces.trace("async.pool.drain", attributes: attributes) {super}
|
409
|
+
end
|
410
|
+
end
|
411
|
+
|
412
|
+
Metrics::Provider(self) do
|
413
|
+
ACQUIRE_COUNT = Metrics.metric("async.pool.acquire", :counter, description: "Number of times a resource was invoked.")
|
414
|
+
RELEASE_COUNT = Metrics.metric("async.pool.release", :counter, description: "Number of times a resource was released.")
|
415
|
+
RETIRE_COUNT = Metrics.metric("async.pool.retire", :counter, description: "Number of times a resource was retired.")
|
416
|
+
|
417
|
+
def acquire(...)
|
418
|
+
ACQUIRE_COUNT.emit(1, tags: @tags)
|
419
|
+
|
420
|
+
super
|
421
|
+
end
|
422
|
+
|
423
|
+
def release(...)
|
424
|
+
super.tap do
|
425
|
+
RELEASE_COUNT.emit(1, tags: @tags)
|
426
|
+
end
|
427
|
+
end
|
428
|
+
|
429
|
+
def retire(...)
|
430
|
+
super.tap do
|
431
|
+
RETIRE_COUNT.emit(1, tags: @tags)
|
432
|
+
end
|
433
|
+
end
|
434
|
+
end
|
378
435
|
end
|
379
436
|
end
|
380
437
|
end
|
data/lib/async/pool/resource.rb
CHANGED
@@ -3,10 +3,10 @@
|
|
3
3
|
# Released under the MIT License.
|
4
4
|
# Copyright, 2019-2024, by Samuel Williams.
|
5
5
|
|
6
|
-
require
|
6
|
+
require "console/logger"
|
7
7
|
|
8
|
-
require
|
9
|
-
require
|
8
|
+
require "async/notification"
|
9
|
+
require "async/semaphore"
|
10
10
|
|
11
11
|
module Async
|
12
12
|
module Pool
|
data/lib/async/pool/version.rb
CHANGED
data/lib/async/pool.rb
CHANGED
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.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
@@ -40,7 +40,7 @@ cert_chain:
|
|
40
40
|
Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
|
41
41
|
voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
|
42
42
|
-----END CERTIFICATE-----
|
43
|
-
date: 2024-
|
43
|
+
date: 2024-10-12 00:00:00.000000000 Z
|
44
44
|
dependencies:
|
45
45
|
- !ruby/object:Gem::Dependency
|
46
46
|
name: async
|
@@ -56,6 +56,34 @@ dependencies:
|
|
56
56
|
- - ">="
|
57
57
|
- !ruby/object:Gem::Version
|
58
58
|
version: '1.25'
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: metrics
|
61
|
+
requirement: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - "~>"
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0.11'
|
66
|
+
type: :runtime
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - "~>"
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0.11'
|
73
|
+
- !ruby/object:Gem::Dependency
|
74
|
+
name: traces
|
75
|
+
requirement: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
type: :runtime
|
81
|
+
prerelease: false
|
82
|
+
version_requirements: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
59
87
|
description:
|
60
88
|
email:
|
61
89
|
executables: []
|
metadata.gz.sig
CHANGED
Binary file
|