Skip to content

Commit

Permalink
Add support for pool tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Oct 12, 2024
1 parent 5441026 commit 1632fd6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/async/pool/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

require 'traces'
require 'metrics'
require 'metrics/tags'

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

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

@tags = Metrics::Tags.normalize(tags)

# All available resources:
@resources = {}

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

# @attribute [Array(String)] The name of the pool.
attr_accessor :tags

# The number of resources in the pool.
def size
@resources.size
Expand Down Expand Up @@ -388,6 +394,7 @@ def create_resource(...)
concurrency: @guard.limit,
size: @resources.size,
limit: @limit,
name: @name,
}

Traces.trace('async.pool.create', attributes: attributes) {super}
Expand All @@ -396,6 +403,7 @@ def create_resource(...)
def drain(...)
attributes = {
size: @resources.size,
name: @name,
}

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

def metric_tags
if @name
["name:#{@name}"]
end
end

def acquire(...)
ACQUIRE_COUNT.emit(1)
ACQUIRE_COUNT.emit(1, tags: self.metric_tags)

super
end

def release(...)
super.tap do
RELEASE_COUNT.emit(1)
RELEASE_COUNT.emit(1, tags: self.metric_tags)
end
end

def retire(...)
super.tap do
RETIRE_COUNT.emit(1)
RETIRE_COUNT.emit(1, tags: self.metric_tags)
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/async/pool/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@
end
end

with "tags" do
let(:pool) {subject.new(Async::Pool::Resource, tags: {a: 1, b: 2})}

it "can assign tags to the pool" do
expect(pool.tags).to be == ["a:1", "b:2"]
end
end

with 'a limited pool' do
let(:pool) {subject.new(Async::Pool::Resource, limit: 1)}

Expand Down

0 comments on commit 1632fd6

Please sign in to comment.