mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Use select to distinguish between timeout and resource availability
This commit is contained in:
parent
47e7d826e8
commit
d3606831f3
1 changed files with 18 additions and 24 deletions
|
@ -125,28 +125,23 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
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)
|
||||||
inc_waiting_resource
|
inc_waiting_resource
|
||||||
|
|
||||||
timeout.start
|
timeout.start
|
||||||
# if there are no available resources, sleep until one is available
|
|
||||||
@availability_channel.receive
|
|
||||||
if timeout.timeout_reached?
|
|
||||||
dec_waiting_resource
|
|
||||||
raise DB::PoolTimeout.new
|
|
||||||
end
|
|
||||||
|
|
||||||
# double check there is something available to be checkedout
|
|
||||||
while @available.empty?
|
|
||||||
@availability_channel.receive
|
|
||||||
if timeout.timeout_reached?
|
|
||||||
dec_waiting_resource
|
|
||||||
raise DB::PoolTimeout.new
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
# TODO update to select keyword for crystal 0.19
|
||||||
|
index, _ = Channel.select(@availability_channel.receive_op, timeout.select_action)
|
||||||
|
case index
|
||||||
|
when 0
|
||||||
timeout.cancel
|
timeout.cancel
|
||||||
dec_waiting_resource
|
dec_waiting_resource
|
||||||
|
when 1
|
||||||
|
dec_waiting_resource
|
||||||
|
raise DB::PoolTimeout.new
|
||||||
|
else
|
||||||
|
raise DB::Error.new
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private def inc_waiting_resource
|
private def inc_waiting_resource
|
||||||
|
@ -168,17 +163,20 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
class TimeoutHelper
|
class TimeoutHelper
|
||||||
def initialize(@timeout : Float64, @tick : Proc(Nil))
|
def initialize(@timeout : Float64)
|
||||||
@abort_timeout = false
|
@abort_timeout = false
|
||||||
@should_timeout = false
|
@timeout_channel = Channel(Nil).new
|
||||||
|
end
|
||||||
|
|
||||||
|
def select_action
|
||||||
|
@timeout_channel.receive_op
|
||||||
end
|
end
|
||||||
|
|
||||||
def start
|
def start
|
||||||
spawn do
|
spawn do
|
||||||
sleep @timeout
|
sleep @timeout
|
||||||
unless @abort_timeout
|
unless @abort_timeout
|
||||||
@should_timeout = true
|
@timeout_channel.send nil
|
||||||
@tick.call
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -186,10 +184,6 @@ module DB
|
||||||
def cancel
|
def cancel
|
||||||
@abort_timeout = true
|
@abort_timeout = true
|
||||||
end
|
end
|
||||||
|
|
||||||
def timeout_reached?
|
|
||||||
@should_timeout
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue