Class: HTTPClient::TimeoutScheduler
- Inherits:
-
Object
- Object
- HTTPClient::TimeoutScheduler
- Defined in:
- lib/httpclient/timeout.rb
Overview
Replaces timeout.rb to avoid Thread creation and scheduling overhead.
You should check another timeout replace in WEBrick. See lib/webrick/utils.rb in ruby/1.9.
About this implementation:
-
Do not create Thread for each timeout() call. Just create 1 Thread for timeout scheduler.
-
Do not wakeup the scheduler thread so often. Let scheduler thread sleep until the nearest period.
Defined Under Namespace
Classes: Period
Instance Method Summary collapse
-
#cancel(period) ⇒ Object
Cancels the given period.
-
#initialize ⇒ TimeoutScheduler
constructor
Creates new TimeoutScheduler.
-
#register(thread, sec, ex) ⇒ Object
Registers new timeout period.
Constructor Details
#initialize ⇒ TimeoutScheduler
Creates new TimeoutScheduler.
56 57 58 59 60 |
# File 'lib/httpclient/timeout.rb', line 56 def initialize @pool = {} @next = nil @thread = start_timer_thread end |
Instance Method Details
#cancel(period) ⇒ Object
Cancels the given period.
78 79 80 81 |
# File 'lib/httpclient/timeout.rb', line 78 def cancel(period) @pool.delete(period) period.cancel end |
#register(thread, sec, ex) ⇒ Object
Registers new timeout period.
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/httpclient/timeout.rb', line 63 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 |