mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
surround waiting_resource counter with a mutex for future thread support.
This commit is contained in:
parent
421996b952
commit
b8cabee956
1 changed files with 22 additions and 3 deletions
|
@ -12,6 +12,7 @@ module DB
|
||||||
|
|
||||||
@availability_channel = Channel(Nil).new
|
@availability_channel = Channel(Nil).new
|
||||||
@waiting_resource = 0
|
@waiting_resource = 0
|
||||||
|
@mutex = Mutex.new
|
||||||
end
|
end
|
||||||
|
|
||||||
# close all resources in the pool
|
# close all resources in the pool
|
||||||
|
@ -38,7 +39,7 @@ module DB
|
||||||
def release(resource : T) : Nil
|
def release(resource : T) : Nil
|
||||||
if can_increase_idle_pool
|
if can_increase_idle_pool
|
||||||
@available << resource
|
@available << resource
|
||||||
@availability_channel.send nil if @waiting_resource > 0
|
@availability_channel.send nil if are_waiting_for_resource?
|
||||||
else
|
else
|
||||||
resource.close
|
resource.close
|
||||||
@total.delete(resource)
|
@total.delete(resource)
|
||||||
|
@ -66,7 +67,7 @@ module DB
|
||||||
|
|
||||||
private def wait_for_available
|
private def wait_for_available
|
||||||
timeout = TimeoutHelper.new(@checkout_timeout.to_f64, ->{ @availability_channel.send nil })
|
timeout = TimeoutHelper.new(@checkout_timeout.to_f64, ->{ @availability_channel.send nil })
|
||||||
@waiting_resource += 1
|
inc_waiting_resource
|
||||||
|
|
||||||
timeout.start
|
timeout.start
|
||||||
# if there are no available resources, sleep until one is available
|
# if there are no available resources, sleep until one is available
|
||||||
|
@ -80,8 +81,26 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
timeout.cancel
|
timeout.cancel
|
||||||
|
dec_waiting_resource
|
||||||
|
end
|
||||||
|
|
||||||
|
private def inc_waiting_resource
|
||||||
|
@mutex.synchronize do
|
||||||
|
@waiting_resource += 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private def dec_waiting_resource
|
||||||
|
@mutex.synchronize do
|
||||||
@waiting_resource -= 1
|
@waiting_resource -= 1
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private def are_waiting_for_resource?
|
||||||
|
@mutex.synchronize do
|
||||||
|
@waiting_resource > 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class TimeoutHelper
|
class TimeoutHelper
|
||||||
def initialize(@timeout : Float64, @tick : Proc(Nil))
|
def initialize(@timeout : Float64, @tick : Proc(Nil))
|
||||||
|
|
Loading…
Reference in a new issue