class HTTPX::Timers

  1. lib/httpx/timers.rb
Superclass: Object

Methods

Public Class

  1. new

Public Instance

  1. after
  2. empty?
  3. fire
  4. wait_interval

Public Class methods

new()
[show source]
  # File lib/httpx/timers.rb
5 def initialize
6   @intervals = []
7 end

Public Instance methods

after(interval_in_secs, cb = nil, &blk)
[show source]
   # File lib/httpx/timers.rb
13 def after(interval_in_secs, cb = nil, &blk)
14   callback = cb || blk
15 
16   raise Error, "timer must have a callback" unless callback
17 
18   # I'm assuming here that most requests will have the same
19   # request timeout, as in most cases they share common set of
20   # options. A user setting different request timeouts for 100s of
21   # requests will already have a hard time dealing with that.
22   unless (interval = @intervals.bsearch { |t| t.interval == interval_in_secs })
23     interval = Interval.new(interval_in_secs)
24     @intervals << interval
25     @intervals.sort!
26   end
27 
28   interval << callback
29 
30   @next_interval_at = nil
31 
32   Timer.new(interval, callback)
33 end
empty?()
[show source]
   # File lib/httpx/timers.rb
 9 def empty?
10   @intervals.empty?
11 end
fire(error = nil)
[show source]
   # File lib/httpx/timers.rb
47 def fire(error = nil)
48   raise error if error && error.timeout != @intervals.first
49   return if @intervals.empty? || !@next_interval_at
50 
51   elapsed_time = Utils.elapsed_time(@next_interval_at)
52 
53   drop_elapsed!(elapsed_time)
54 
55   @next_interval_at = nil if @intervals.empty?
56 end
wait_interval()
[show source]
   # File lib/httpx/timers.rb
35 def wait_interval
36   return if @intervals.empty?
37 
38   first_interval = @intervals.first
39 
40   drop_elapsed!(0) if first_interval.elapsed?(0)
41 
42   @next_interval_at = Utils.now
43 
44   first_interval.interval
45 end