mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Handle multiple fibers waiting for more resources than available
This commit is contained in:
parent
5c1c1ab910
commit
afad416417
1 changed files with 20 additions and 11 deletions
|
@ -63,6 +63,9 @@ module DB
|
|||
|
||||
def checkout : T
|
||||
res = sync do
|
||||
resource = nil
|
||||
|
||||
until resource
|
||||
resource = if @idle.empty?
|
||||
if can_increase_pool?
|
||||
@inflight += 1
|
||||
|
@ -71,11 +74,17 @@ module DB
|
|||
r
|
||||
else
|
||||
unsync { wait_for_available }
|
||||
# The wait for available can unlock
|
||||
# multiple fibers waiting for a resource.
|
||||
# Although only one will pick it due to the lock
|
||||
# in the end of the unsync, the pick_available
|
||||
# will return nil
|
||||
pick_available
|
||||
end
|
||||
else
|
||||
pick_available
|
||||
end
|
||||
end
|
||||
|
||||
@idle.delete resource
|
||||
|
||||
|
@ -194,7 +203,7 @@ module DB
|
|||
end
|
||||
|
||||
private def pick_available
|
||||
@idle.first
|
||||
@idle.first?
|
||||
end
|
||||
|
||||
private def wait_for_available
|
||||
|
|
Loading…
Reference in a new issue