Class: WEBrick::Utils::TimeoutHandler

Inherits:
Object
  • Object
show all
Includes:
Singleton
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)
end

will raise Timeout::Error

id = TimeoutHandler.register(10, Timeout::Error)
begin
  sleep 5
  puts 'foo'
ensure
  TimeoutHandler.cancel(id)
end

will print ‘foo’

Constant Summary collapse

TimeoutMutex =

Mutex used to synchronize access across threads

Thread::Mutex.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTimeoutHandler

Creates a new TimeoutHandler. You should use ::register and ::cancel instead of creating the timeout handler directly.



153
154
155
156
157
158
159
# File 'lib/webrick/utils.rb', line 153

def initialize
  TimeoutMutex.synchronize{
    @timeout_info = Hash.new
  }
  @queue = Thread::Queue.new
  @watcher = nil
end

Class Method Details

.cancel(id) ⇒ Object

Cancels the timeout handler id



142
143
144
# File 'lib/webrick/utils.rb', line 142

def TimeoutHandler.cancel(id)
  instance.cancel(Thread.current, id)
end

.register(seconds, exception) ⇒ Object

Registers a new timeout handler

time

Timeout in seconds

exception

Exception to raise when timeout elapsed



135
136
137
138
# File 'lib/webrick/utils.rb', line 135

def TimeoutHandler.register(seconds, exception)
  at = Process.clock_gettime(Process::CLOCK_MONOTONIC) + seconds
  instance.register(Thread.current, at, exception)
end

.terminateObject



146
147
148
# File 'lib/webrick/utils.rb', line 146

def self.terminate
  instance.terminate
end

Instance Method Details

#cancel(thread, id) ⇒ Object

Cancels the timeout handler id



231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/webrick/utils.rb', line 231

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

#interrupt(thread, id, exception) ⇒ Object

Interrupts the timeout handler id and raises exception



208
209
210
211
212
# File 'lib/webrick/utils.rb', line 208

def interrupt(thread, id, exception)
  if cancel(thread, id) && thread.alive?
    thread.raise(exception, "execution timeout")
  end
end

#register(thread, time, exception) ⇒ Object

Registers a new timeout handler

time

Timeout in seconds

exception

Exception to raise when timeout elapsed



219
220
221
222
223
224
225
226
227
# File 'lib/webrick/utils.rb', line 219

def register(thread, time, exception)
  info = nil
  TimeoutMutex.synchronize{
    (@timeout_info[thread] ||= []) << (info = [time, exception])
  }
  @queue.push nil
  watcher
  return info.object_id
end

#terminateObject



245
246
247
248
249
250
# File 'lib/webrick/utils.rb', line 245

def terminate
  TimeoutMutex.synchronize{
    @timeout_info.clear
    @watcher&.kill&.join
  }
end