Class: WEBrick::Utils::TimeoutHandler
| Relationships & Source Files | |
| Namespace Children | |
| Classes: | |
| Super Chains via Extension / Inclusion / Inheritance | |
| Instance Chain: 
          self,
          Singleton
         | |
| Inherits: | Object | 
| Defined in: | lib/webrick/utils.rb | 
Overview
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)
endwill raise Timeout::Error
id = TimeoutHandler.register(10, Timeout::Error)
begin
  sleep 5
  puts 'foo'
ensure
  TimeoutHandler.cancel(id)
endwill print 'foo'
Class Method Summary
- 
    
      .cancel(id)  
    
    Cancels the timeout handler id
- 
    
      .new  ⇒ TimeoutHandler 
    
    constructor
    Creates a new TimeoutHandler.
- 
    
      .register(seconds, exception)  
    
    Registers a new timeout handler. 
Instance Method Summary
- 
    
      #cancel(thread, id)  
    
    Cancels the timeout handler id
- 
    
      #interrupt(thread, id, exception)  
    
    Interrupts the timeout handler idand raisesexception
- 
    
      #register(thread, time, exception)  
    
    Registers a new timeout handler. 
Constructor Details
    .new  ⇒ TimeoutHandler 
  
# File 'lib/webrick/utils.rb', line 160
def initialize @timeout_info = Hash.new Thread.start{ while true now = Time.now @timeout_info.keys.each{|thread| ary = @timeout_info[thread] next unless ary ary.dup.each{|info| time, exception = *info interrupt(thread, info.object_id, exception) if time < now } } sleep 0.5 end } end
Class Method Details
.cancel(id)
Cancels the timeout handler id
# File 'lib/webrick/utils.rb', line 151
def TimeoutHandler.cancel(id) TimeoutMutex.synchronize{ instance.cancel(Thread.current, id) } end
.register(seconds, exception)
Registers a new timeout handler
- time
- 
Timeout in seconds 
- exception
- 
Exception to raise when timeout elapsed 
# File 'lib/webrick/utils.rb', line 143
def TimeoutHandler.register(seconds, exception) TimeoutMutex.synchronize{ instance.register(Thread.current, Time.now + seconds, exception) } end
Instance Method Details
#cancel(thread, id)
Cancels the timeout handler id
# File 'lib/webrick/utils.rb', line 201
def cancel(thread, id) 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
#interrupt(thread, id, exception)
Interrupts the timeout handler id and raises exception
# File 'lib/webrick/utils.rb', line 180
def interrupt(thread, id, exception) TimeoutMutex.synchronize{ if cancel(thread, id) && thread.alive? thread.raise(exception, "execution timeout") end } end
#register(thread, time, exception)
Registers a new timeout handler
- time
- 
Timeout in seconds 
- exception
- 
Exception to raise when timeout elapsed 
# File 'lib/webrick/utils.rb', line 193
def register(thread, time, exception) @timeout_info[thread] ||= Array.new @timeout_info[thread] << [time, exception] return @timeout_info[thread].last.object_id end