class HTTPX::Timers

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

Methods

Public Class

  1. new

Public Instance

  1. after
  2. fire
  3. wait_interval

Classes and Modules

  1. HTTPX::Timers::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
 9 def after(interval_in_secs, cb = nil, &blk)
10   return unless interval_in_secs
11 
12   callback = cb || blk
13 
14   # I'm assuming here that most requests will have the same
15   # request timeout, as in most cases they share common set of
16   # options. A user setting different request timeouts for 100s of
17   # requests will already have a hard time dealing with that.
18   unless (interval = @intervals.find { |t| t.interval == interval_in_secs })
19     interval = Interval.new(interval_in_secs)
20     interval.on_empty { @intervals.delete(interval) }
21     @intervals << interval
22     @intervals.sort!
23   end
24 
25   interval << callback
26 
27   @next_interval_at = nil
28 
29   interval
30 end
fire(error = nil)
[show source]
   # File lib/httpx/timers.rb
40 def fire(error = nil)
41   raise error if error && error.timeout != @intervals.first
42   return if @intervals.empty? || !@next_interval_at
43 
44   elapsed_time = Utils.elapsed_time(@next_interval_at)
45 
46   @intervals = @intervals.drop_while { |interval| interval.elapse(elapsed_time) <= 0 }
47 
48   @next_interval_at = nil if @intervals.empty?
49 end
wait_interval()
[show source]
   # File lib/httpx/timers.rb
32 def wait_interval
33   return if @intervals.empty?
34 
35   @next_interval_at = Utils.now
36 
37   @intervals.first.interval
38 end