Skip to content

Commit

Permalink
Add support for pool name.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Oct 12, 2024
1 parent 5441026 commit bfe10df
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/async/pool/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,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, name: nil)
@constructor = constructor
@limit = limit

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

@name = name

# All available resources:
@resources = {}

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

# @attribute [String] The name of the pool.
attr_accessor :name

# The number of resources in the pool.
def size
@resources.size
Expand Down Expand Up @@ -388,6 +393,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 +402,7 @@ def create_resource(...)
def drain(...)
attributes = {
size: @resources.size,
name: @name,
}

Traces.trace('async.pool.drain', attributes: attributes) {super}
Expand All @@ -407,21 +414,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

0 comments on commit bfe10df

Please sign in to comment.