Skip to content

Commit

Permalink
Handle exceptions propagating out of semaphore.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jun 6, 2021
1 parent 89b76d5 commit 8c6ef4d
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions lib/async/pool/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,36 +265,45 @@ def create_resource

# @returns [Object] An existing resource in a "used" state.
def available_resource
resource = nil

@guard.acquire do
while resource = @available.last
if usage = @resources[resource] and usage < resource.concurrency
if resource.viable?
usage = (@resources[resource] += 1)

if usage == resource.concurrency
# The resource is used up to it's limit:
@available.pop
end

return resource
else
retire(resource)
resource = get_resource
end

return resource
rescue Exception
reuse(resource) if resource
raise
end

private def get_resource
while resource = @available.last
if usage = @resources[resource] and usage < resource.concurrency
if resource.viable?
usage = (@resources[resource] += 1)

if usage == resource.concurrency
# The resource is used up to it's limit:
@available.pop
end

return resource
else
# The resource has been removed already, so skip it and remove it from the availability list.
retire(resource)
@available.pop
end
end

if @limit.nil? or @resources.size < @limit
Console.logger.debug(self) {"No available resources, allocating new one..."}

return create_resource
else
# The resource has been removed already, so skip it and remove it from the availability list.
@available.pop
end
end

return nil
if @limit.nil? or @resources.size < @limit
Console.logger.debug(self) {"No available resources, allocating new one..."}

return create_resource
end
end
end
end
Expand Down

0 comments on commit 8c6ef4d

Please sign in to comment.