Class
used to manage timeout handlers across multiple threads.
Timeout
handlers should be managed by using the class methods which are synchronized.
id = TimeoutHandler.register(10, Timeout::Error) begin sleep 20 puts 'foo' ensure TimeoutHandler.cancel(id) end
will raise Timeout::Error
id = TimeoutHandler.register(10, Timeout::Error) begin sleep 5 puts 'foo' ensure TimeoutHandler.cancel(id) end
will print ‘foo’
Constants
Mutex used to synchronize access across threads
Class Methods
::
lib/webrick/utils.rb
View on GitHub
# File tmp/rubies/ruby-2.3.8/lib/webrick/utils.rb, line 144
def TimeoutHandler.cancel(id)
instance.cancel(Thread.current, id)
end
Cancels the timeout handler id
::
lib/webrick/utils.rb
View on GitHub
# File tmp/rubies/ruby-2.3.8/lib/webrick/utils.rb, line 151
def initialize
TimeoutMutex.synchronize{
@timeout_info = Hash.new
}
@queue = Queue.new
@watcher = Thread.start{
to_interrupt = []
while true
now = Time.now
wakeup = nil
to_interrupt.clear
TimeoutMutex.synchronize{
@timeout_info.each {|thread, ary|
next unless ary
ary.each{|info|
time, exception = *info
if time < now
to_interrupt.push [thread, info.object_id, exception]
elsif !wakeup || time < wakeup
wakeup = time
end
}
}
}
to_interrupt.each {|arg| interrupt(*arg)}
if !wakeup
@queue.pop
elsif (wakeup -= now) > 0
begin
(th = Thread.start {@queue.pop}).join(wakeup)
ensure
th&.kill&.join
end
end
@queue.clear
end
}
end
Creates a new TimeoutHandler
. You should use ::register
and ::cancel
instead of creating the timeout handler directly.
lib/webrick/utils.rb
View on GitHub
# File tmp/rubies/ruby-2.3.8/lib/webrick/utils.rb, line 138
def TimeoutHandler.register(seconds, exception)
instance.register(Thread.current, Time.now + seconds, exception)
end
Instance Methods
lib/webrick/utils.rb
View on GitHub
# File tmp/rubies/ruby-2.3.8/lib/webrick/utils.rb, line 215
def cancel(thread, id)
TimeoutMutex.synchronize{
if ary = @timeout_info[thread]
ary.delete_if{|info| info.object_id == id }
if ary.empty?
@timeout_info.delete(thread)
end
return true
end
return false
}
end
Cancels the timeout handler id
lib/webrick/utils.rb
View on GitHub
# File tmp/rubies/ruby-2.3.8/lib/webrick/utils.rb, line 192
def interrupt(thread, id, exception)
if cancel(thread, id) && thread.alive?
thread.raise(exception, "execution timeout")
end
end
Interrupts the timeout handler id
and raises exception
lib/webrick/utils.rb
View on GitHub
# File tmp/rubies/ruby-2.3.8/lib/webrick/utils.rb, line 203
def register(thread, time, exception)
info = nil
TimeoutMutex.synchronize{
@timeout_info[thread] ||= Array.new
@timeout_info[thread] << (info = [time, exception])
}
@queue.push nil
return info.object_id
end