Skip to content

Commit 061817e

Browse files
committed
Add support for pool tags.
1 parent 5441026 commit 061817e

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

async-pool.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ Gem::Specification.new do |spec|
2626
spec.required_ruby_version = ">= 3.1"
2727

2828
spec.add_dependency "async", ">= 1.25"
29-
spec.add_dependency "metrics"
29+
spec.add_dependency "metrics", "~> 0.11"
3030
spec.add_dependency "traces"
3131
end

lib/async/pool/controller.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def self.wrap(**options, &block)
2929
# @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.
3030
# @parameter concurrency [Integer] The maximum number of concurrent tasks that can be creating a new resource.
3131
# @parameter policy [Policy] The pool policy.
32-
def initialize(constructor, limit: nil, concurrency: (limit || 1), policy: nil)
32+
def initialize(constructor, limit: nil, concurrency: (limit || 1), policy: nil, tags: nil)
3333
@constructor = constructor
3434
@limit = limit
3535

@@ -39,6 +39,8 @@ def initialize(constructor, limit: nil, concurrency: (limit || 1), policy: nil)
3939
@policy = policy
4040
@gardener = nil
4141

42+
@tags = Metrics::Tags.normalize(tags)
43+
4244
# All available resources:
4345
@resources = {}
4446

@@ -96,6 +98,9 @@ def concurrency= value
9698
# @attribute [Hash(Resource, Integer)] all allocated resources, and their associated usage.
9799
attr :resources
98100

101+
# @attribute [Array(String)] The name of the pool.
102+
attr_accessor :tags
103+
99104
# The number of resources in the pool.
100105
def size
101106
@resources.size
@@ -388,6 +393,7 @@ def create_resource(...)
388393
concurrency: @guard.limit,
389394
size: @resources.size,
390395
limit: @limit,
396+
tags: @tags,
391397
}
392398

393399
Traces.trace('async.pool.create', attributes: attributes) {super}
@@ -396,6 +402,7 @@ def create_resource(...)
396402
def drain(...)
397403
attributes = {
398404
size: @resources.size,
405+
tags: @tags,
399406
}
400407

401408
Traces.trace('async.pool.drain', attributes: attributes) {super}
@@ -408,20 +415,20 @@ def drain(...)
408415
RETIRE_COUNT = Metrics.metric('async.pool.retire', :counter, description: 'Number of times a resource was retired.')
409416

410417
def acquire(...)
411-
ACQUIRE_COUNT.emit(1)
418+
ACQUIRE_COUNT.emit(1, tags: @tags)
412419

413420
super
414421
end
415422

416423
def release(...)
417424
super.tap do
418-
RELEASE_COUNT.emit(1)
425+
RELEASE_COUNT.emit(1, tags: @tags)
419426
end
420427
end
421428

422429
def retire(...)
423430
super.tap do
424-
RETIRE_COUNT.emit(1)
431+
RETIRE_COUNT.emit(1, tags: @tags)
425432
end
426433
end
427434
end

test/async/pool/controller.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@
4646
end
4747
end
4848

49+
with "tags" do
50+
let(:pool) {subject.new(Async::Pool::Resource, tags: {a: 1, b: 2})}
51+
52+
it "can assign tags to the pool" do
53+
expect(pool.tags).to be == ["a:1", "b:2"]
54+
end
55+
end
56+
4957
with 'a limited pool' do
5058
let(:pool) {subject.new(Async::Pool::Resource, limit: 1)}
5159

0 commit comments

Comments
 (0)