Skip to content

Commit 1632fd6

Browse files
committed
Add support for pool tags.
1 parent 5441026 commit 1632fd6

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

lib/async/pool/controller.rb

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
require 'traces'
1515
require 'metrics'
16+
require 'metrics/tags'
1617

1718
module Async
1819
module Pool
@@ -29,7 +30,7 @@ def self.wrap(**options, &block)
2930
# @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.
3031
# @parameter concurrency [Integer] The maximum number of concurrent tasks that can be creating a new resource.
3132
# @parameter policy [Policy] The pool policy.
32-
def initialize(constructor, limit: nil, concurrency: (limit || 1), policy: nil)
33+
def initialize(constructor, limit: nil, concurrency: (limit || 1), policy: nil, tags: nil)
3334
@constructor = constructor
3435
@limit = limit
3536

@@ -39,6 +40,8 @@ def initialize(constructor, limit: nil, concurrency: (limit || 1), policy: nil)
3940
@policy = policy
4041
@gardener = nil
4142

43+
@tags = Metrics::Tags.normalize(tags)
44+
4245
# All available resources:
4346
@resources = {}
4447

@@ -96,6 +99,9 @@ def concurrency= value
9699
# @attribute [Hash(Resource, Integer)] all allocated resources, and their associated usage.
97100
attr :resources
98101

102+
# @attribute [Array(String)] The name of the pool.
103+
attr_accessor :tags
104+
99105
# The number of resources in the pool.
100106
def size
101107
@resources.size
@@ -388,6 +394,7 @@ def create_resource(...)
388394
concurrency: @guard.limit,
389395
size: @resources.size,
390396
limit: @limit,
397+
name: @name,
391398
}
392399

393400
Traces.trace('async.pool.create', attributes: attributes) {super}
@@ -396,6 +403,7 @@ def create_resource(...)
396403
def drain(...)
397404
attributes = {
398405
size: @resources.size,
406+
name: @name,
399407
}
400408

401409
Traces.trace('async.pool.drain', attributes: attributes) {super}
@@ -407,21 +415,27 @@ def drain(...)
407415
RELEASE_COUNT = Metrics.metric('async.pool.release', :counter, description: 'Number of times a resource was released.')
408416
RETIRE_COUNT = Metrics.metric('async.pool.retire', :counter, description: 'Number of times a resource was retired.')
409417

418+
def metric_tags
419+
if @name
420+
["name:#{@name}"]
421+
end
422+
end
423+
410424
def acquire(...)
411-
ACQUIRE_COUNT.emit(1)
425+
ACQUIRE_COUNT.emit(1, tags: self.metric_tags)
412426

413427
super
414428
end
415429

416430
def release(...)
417431
super.tap do
418-
RELEASE_COUNT.emit(1)
432+
RELEASE_COUNT.emit(1, tags: self.metric_tags)
419433
end
420434
end
421435

422436
def retire(...)
423437
super.tap do
424-
RETIRE_COUNT.emit(1)
438+
RETIRE_COUNT.emit(1, tags: self.metric_tags)
425439
end
426440
end
427441
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)