|
12 | 12 | require "async" |
13 | 13 | require "async/semaphore" |
14 | 14 |
|
| 15 | +require "set" |
15 | 16 | require "thread" |
16 | 17 |
|
17 | 18 | module Async |
@@ -41,12 +42,12 @@ def initialize(constructor, limit: nil, concurrency: 1, policy: nil, tags: nil) |
41 | 42 |
|
42 | 43 | @tags = tags |
43 | 44 |
|
44 | | - # All available resources: |
45 | | - @resources = {} |
| 45 | + # All allocated resources. Each resource is tracked by identity, irrespective of any value equality it may define: |
| 46 | + @resources = {}.compare_by_identity |
46 | 47 |
|
47 | | - # Resources which may be available to be acquired: |
48 | | - # This list may contain false positives, or resources which were okay but have since entered a state which is unusuable. |
49 | | - @available = [] |
| 48 | + # Resources which may be available to be acquired. Resources are compared by identity and acquired in insertion order. Adding an existing resource preserves its position; a fully utilized resource is removed and reinserted at the end when capacity becomes available again. |
| 49 | + # This set may contain false positives, or resources which were okay but have since entered a state which is unusable. |
| 50 | + @available = Set.new.compare_by_identity |
50 | 51 |
|
51 | 52 | # Used to signal when a resource has been released: |
52 | 53 | @mutex = Thread::Mutex.new |
@@ -171,10 +172,7 @@ def release(resource) |
171 | 172 | end |
172 | 173 |
|
173 | 174 | if resource.reusable? |
174 | | - # If the resource was fully utilized, it now becomes available: |
175 | | - if usage == resource.concurrency - 1 |
176 | | - @available.push(resource) |
177 | | - end |
| 175 | + @available.add(resource) |
178 | 176 | else |
179 | 177 | # The resource must not be acquired again, but it cannot be retired until all existing users have released it: |
180 | 178 | @available.delete(resource) |
@@ -319,7 +317,7 @@ def create_resource |
319 | 317 |
|
320 | 318 | # Make the resource available if it can be used multiple times: |
321 | 319 | if resource.concurrency > 1 |
322 | | - @available.push(resource) |
| 320 | + @available.add(resource) |
323 | 321 | end |
324 | 322 | end |
325 | 323 |
|
@@ -371,24 +369,24 @@ def acquire_existing_resource |
371 | 369 | end |
372 | 370 |
|
373 | 371 | def acquire_or_create_resource |
374 | | - while resource = @available.last |
| 372 | + while resource = @available.first |
375 | 373 | if usage = @resources[resource] and usage < resource.concurrency |
376 | 374 | if resource.viable? |
377 | 375 | usage = (@resources[resource] += 1) |
378 | 376 |
|
379 | 377 | if usage == resource.concurrency |
380 | 378 | # The resource is used up to it's limit: |
381 | | - @available.pop |
| 379 | + @available.delete(resource) |
382 | 380 | end |
383 | 381 |
|
384 | 382 | return resource |
385 | 383 | else |
386 | | - @available.pop |
| 384 | + @available.delete(resource) |
387 | 385 | retire(resource) if usage.zero? |
388 | 386 | end |
389 | 387 | else |
390 | 388 | # The resource has been removed already, so skip it and remove it from the availability list. |
391 | | - @available.pop |
| 389 | + @available.delete(resource) |
392 | 390 | end |
393 | 391 | end |
394 | 392 |
|
|
0 commit comments