Class: FrugalTimeout::SleeperNotifier

Inherits:
Object
  • Object
show all
Includes:
Hookable, MonitorMixin
Defined in:
lib/frugal_timeout.rb

Overview

{{{1 SleeperNotifier Executes callback when a request expires.

  1. Set callback to execute with #onExpiry=.

  2. Set expiry time with #expireAt.

  3. After the expiry time comes, execute the callback.

It’s possible to set a new expiry time before the time set previously expires. However, if the old request has already expired, @onExpiry will still be called.

Constant Summary

Constants included from Hookable

Hookable::DO_NOTHING

Instance Method Summary collapse

Methods included from Hookable

#def_hook, #def_hook_synced

Constructor Details

#initializeSleeperNotifier

Returns a new instance of SleeperNotifier.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/frugal_timeout.rb', line 163

def initialize
  super()
  def_hook_synced :onExpiry
  @condVar, @expireAt = new_cond, nil

  @thread = Thread.new {
	loop {
	  synchronize { @onExpiry }.call if synchronize {
 waitForValidRequest

 timeLeft = timeLeftUntilExpiry
 # Prevent processing of the same request again.
 disposeOfRequest
 elapsedTime = MonotonicTime.measure { wait timeLeft }

 elapsedTime >= timeLeft
	  }
	}
  }
  ObjectSpace.define_finalizer self, proc { @thread.kill }
end

Instance Method Details

#expireAt(time) ⇒ Object



185
186
187
188
189
190
# File 'lib/frugal_timeout.rb', line 185

def expireAt time
  synchronize {
	@expireAt = time
	signalThread
  }
end