Class: ZMQMachine::Timers

Inherits:
Object
  • Object
show all
Defined in:
lib/zm/timers.rb

Overview

Manages the addition and cancellation of all timers. Each #Reactor maintains its own set of timers; the timer belongs to the reactor context.

This should never be instantiated directly by user code. A timer must be known to the #Reactor in which it is running, so use the #Reactor#oneshot_timer and #Reactor#periodical_timer convenience methods. It ensures that new timers are installed in the correct #Reactor.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTimers

Returns a new instance of Timers.



50
51
52
# File 'lib/zm/timers.rb', line 50

def initialize
  @timers = []
end

Class Method Details

.nowObject

Returns the current time using the following algo:

(Time.now.to_f * 1000).to_i

Added as a class method so that it can be overridden by a user who wants to provide their own time source. For example, a user could use a third-party gem that provides a better performing time source.



168
169
170
# File 'lib/zm/timers.rb', line 168

def self.now
  (Time.now.to_f * 1000).to_i
end

.now_convertedObject

Convert Timers.now to a number usable by the Time class.



174
175
176
# File 'lib/zm/timers.rb', line 174

def self.now_converted
  now / 1000.0
end

Instance Method Details

#add_oneshot(delay, timer_proc = nil, &blk) ⇒ Object

Adds a non-periodical, one-shot timer in order of first-to-fire to last-to-fire.

Returns nil unless a timer_proc or blk are provided. There is no point to an empty timer that does nothing when fired.



65
66
67
68
69
70
71
72
# File 'lib/zm/timers.rb', line 65

def add_oneshot delay, timer_proc = nil, &blk
  blk ||= timer_proc
  return nil unless blk

  timer = Timer.new self, delay, false, blk
  add timer
  timer
end

#add_periodical(delay, timer_proc = nil, &blk) ⇒ Object

Adds a periodical timer in order of first-to-fire to last-to-fire.

Returns nil unless a timer_proc or blk are provided. There is no point to an empty timer that does nothing when fired.



81
82
83
84
85
86
87
88
# File 'lib/zm/timers.rb', line 81

def add_periodical delay, timer_proc = nil, &blk
  blk ||= timer_proc
  return nil unless blk

  timer = Timer.new self, delay, true, blk
  add timer
  timer
end

#cancel(timer) ⇒ Object

Cancel the timer.

Returns true when cancellation succeeds. Returns false when it fails to find the given timer.



96
97
98
99
100
101
102
103
104
# File 'lib/zm/timers.rb', line 96

def cancel timer
  i = index timer

  if timer == @timers.at(i)
    @timers.delete_at(i) ? true : false
  else
    false
  end
end

#fire_expiredObject

A convenience method that loops through all known timers and fires all of the expired timers.

– Internally the list is sorted whenever a timer is added or deleted. It stops processing this list when the next timer is not yet expired; it knows all subsequent timers are not expired too.

timers should be sorted by expiration time NOTE: was using #delete_if here, but it does not delete any items when the break executes before iterating through the entire set; that’s unacceptable so I save each timer for deletion and do that in a separate loop

Additionally, some timers may execute code paths that cancel other timers. If those timers are deleted while we are still iterating over them, the behavior is undefined (each runtime can handle it differently). To avoid that issue, we determine if they are expired and save them off for final processing outside of the loop. Any firing timer that deletes another timer will be safe.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/zm/timers.rb', line 128

def fire_expired
  # all time is expected as milliseconds
  now = Timers.now
  runnables, periodicals, expired = [], [], []

  # defer firing the timer until after this loop so we can clean it up first
  @timers.each_with_index do |timer, index|
    break unless timer.expired?(now)
    runnables << timer
    periodicals << timer if timer.periodical?
    expired << index
  end

  remove expired
  runnables.each { |timer| timer.fire }
  renew periodicals
end

#listObject



54
55
56
# File 'lib/zm/timers.rb', line 54

def list
  @timers
end

#rescheduleObject

Runs through all timers and asks each one to reschedule itself from Timers.now + whatever delay was originally recorded.



149
150
151
152
153
154
155
156
157
# File 'lib/zm/timers.rb', line 149

def reschedule
  timers = @timers.dup
  @timers.clear

  timers.each do |timer|
    timer.reschedule
    add timer
  end
end