Class: FrugalTimeout::SleeperNotifier

Inherits:
Object
  • Object
show all
Includes:
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. In this case, processing of the old request stops and the new request processing starts.

Constant Summary collapse

DO_NOTHING =
proc {}

Instance Method Summary collapse

Constructor Details

#initializeSleeperNotifier

Returns a new instance of SleeperNotifier.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/frugal_timeout.rb', line 147

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

  @thread = Thread.new {
	loop {
	  synchronize { @onExpiry }.call if synchronize {
 # Sleep forever until a request comes in.
 unless @expireAt
   wait
   next
 end

 timeLeft = calcTimeLeft
 disposeOfRequest
 elapsedTime = MonotonicTime.measure { wait timeLeft }

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

Instance Method Details

#expireAt(time) ⇒ Object



175
176
177
178
179
180
# File 'lib/frugal_timeout.rb', line 175

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

#onExpiry(&b) ⇒ Object



171
172
173
# File 'lib/frugal_timeout.rb', line 171

def onExpiry &b
  synchronize { @onExpiry = b || DO_NOTHING }
end