Class: HTTPClient::TimeoutScheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/httpclient/timeout.rb

Defined Under Namespace

Classes: Period

Instance Method Summary collapse

Constructor Details

#initializeTimeoutScheduler

Creates new TimeoutScheduler.



57
58
59
60
61
# File 'lib/httpclient/timeout.rb', line 57

def initialize
  @pool = {}
  @next = nil
  @thread = start_timer_thread
end

Instance Method Details

#cancel(period) ⇒ Object

Cancels the given period.



79
80
81
82
# File 'lib/httpclient/timeout.rb', line 79

def cancel(period)
  @pool.delete(period)
  period.cancel
end

#register(thread, sec, ex) ⇒ Object

Registers new timeout period.



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/httpclient/timeout.rb', line 64

def register(thread, sec, ex)
  period = Period.new(thread, Time.now + sec, ex || ::Timeout::Error)
  @pool[period] = true
  if @next.nil? or period.time < @next
    begin
      @thread.wakeup
    rescue ThreadError
      # Thread may be dead by fork.
      @thread = start_timer_thread
    end
  end
  period
end