Class: FrugalTimeout::SleeperNotifier
- Inherits:
-
Object
- Object
- FrugalTimeout::SleeperNotifier
- Includes:
- Hookable, MonitorMixin
- Defined in:
- lib/frugal_timeout.rb
Overview
{{{1 SleeperNotifier Executes callback when a request expires.
-
Set callback to execute with #onExpiry=.
-
Set expiry time with #expireAt.
-
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
Instance Method Summary collapse
- #expireAt(time) ⇒ Object
-
#initialize ⇒ SleeperNotifier
constructor
A new instance of SleeperNotifier.
Methods included from Hookable
Constructor Details
#initialize ⇒ SleeperNotifier
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 |