Bug #4819 » 0001-Add-documentation-for-WEBrick-Utils.patch
| lib/webrick/utils.rb | ||
|---|---|---|
|
module WEBrick
|
||
|
module Utils
|
||
|
##
|
||
|
# Sets IO operations on +io+ to be non-blocking
|
||
|
def set_non_blocking(io)
|
||
|
flag = File::NONBLOCK
|
||
|
if defined?(Fcntl::F_GETFL)
|
||
| ... | ... | |
|
end
|
||
|
module_function :set_non_blocking
|
||
|
##
|
||
|
# Sets the close on exec flag for +io+
|
||
|
def set_close_on_exec(io)
|
||
|
if defined?(Fcntl::FD_CLOEXEC)
|
||
|
io.fcntl(Fcntl::FD_CLOEXEC, 1)
|
||
| ... | ... | |
|
end
|
||
|
module_function :set_close_on_exec
|
||
|
##
|
||
|
# Changes the process's uid and gid to the ones of +user+
|
||
|
def su(user)
|
||
|
if defined?(Etc)
|
||
|
pw = Etc.getpwnam(user)
|
||
| ... | ... | |
|
end
|
||
|
module_function :su
|
||
|
##
|
||
|
# The server hostname
|
||
|
def getservername
|
||
|
host = Socket::gethostname
|
||
|
begin
|
||
| ... | ... | |
|
end
|
||
|
module_function :getservername
|
||
|
##
|
||
|
# Creates a TCP server socket bound to +address+:+port+ and returns it
|
||
|
def create_listeners(address, port, logger=nil)
|
||
|
unless port
|
||
|
raise ArgumentError, "must specify port"
|
||
| ... | ... | |
|
end
|
||
|
module_function :create_listeners
|
||
|
##
|
||
|
# Characters used to generate random strings
|
||
|
RAND_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
|
||
|
"0123456789" +
|
||
|
"abcdefghijklmnopqrstuvwxyz"
|
||
|
##
|
||
|
# Generates a random string of length +len+
|
||
|
def random_string(len)
|
||
|
rand_max = RAND_CHARS.bytesize
|
||
|
ret = ""
|
||
| ... | ... | |
|
require "timeout"
|
||
|
require "singleton"
|
||
|
##
|
||
|
# 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'
|
||
|
#
|
||
|
class TimeoutHandler
|
||
|
include Singleton
|
||
|
##
|
||
|
# Musex used to synchronize access across threads
|
||
|
TimeoutMutex = Mutex.new
|
||
|
##
|
||
|
# Registers a new timeout handler
|
||
|
#
|
||
|
# +time+:: Timeout in seconds
|
||
|
# +exception+:: Exception to raise when timeout elapsed
|
||
|
def TimeoutHandler.register(seconds, exception)
|
||
|
TimeoutMutex.synchronize{
|
||
|
instance.register(Thread.current, Time.now + seconds, exception)
|
||
|
}
|
||
|
end
|
||
|
##
|
||
|
# Cancels the timeout handler +id+
|
||
|
def TimeoutHandler.cancel(id)
|
||
|
TimeoutMutex.synchronize{
|
||
|
instance.cancel(Thread.current, id)
|
||
| ... | ... | |
|
}
|
||
|
end
|
||
|
##
|
||
|
# Interrupts the timeout handler +id+ and raise +exception+
|
||
|
def interrupt(thread, id, exception)
|
||
|
TimeoutMutex.synchronize{
|
||
|
if cancel(thread, id) && thread.alive?
|
||
| ... | ... | |
|
}
|
||
|
end
|
||
|
##
|
||
|
# Registers a new timeout handler
|
||
|
#
|
||
|
# +time+:: Timeout in seconds
|
||
|
# +exception+:: Exception to raise when timeout elapsed
|
||
|
def register(thread, time, exception)
|
||
|
@timeout_info[thread] ||= Array.new
|
||
|
@timeout_info[thread] << [time, exception]
|
||
|
return @timeout_info[thread].last.object_id
|
||
|
end
|
||
|
##
|
||
|
# Cancels the timeout handler +id+
|
||
|
def cancel(thread, id)
|
||
|
if ary = @timeout_info[thread]
|
||
|
ary.delete_if{|info| info.object_id == id }
|
||
| ... | ... | |
|
end
|
||
|
end
|
||
|
##
|
||
|
# Executes the passed block and raises +exception+ if execution takes more
|
||
|
# than +seconds+ seconds.
|
||
|
# If +seconds+ is zero or nil, simply executes the block
|
||
|
def timeout(seconds, exception=Timeout::Error)
|
||
|
return yield if seconds.nil? or seconds.zero?
|
||
|
# raise ThreadError, "timeout within critical session" if Thread.critical
|
||