Class: AceMQ::AMQP::Patterns::Requester::Waiter Private

Inherits:
Object
  • Object
show all
Defined in:
lib/acemq/amqp/patterns/request_reply.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

One caller's wait for one reply.

A condition variable rather than a Queue with a timeout, because Queue#pop(timeout:) arrived in Ruby 3.2 and this library runs on 3.1.

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Waiter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Waiter.



270
271
272
273
274
275
# File 'lib/acemq/amqp/patterns/request_reply.rb', line 270

def initialize
  @lock = Mutex.new
  @ready = ConditionVariable.new
  @message = nil
  @delivered = false
end

Instance Method Details

#await(timeout) ⇒ Message?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns nil when the deadline passed first.

Returns:

  • (Message, nil) —

    nil when the deadline passed first



286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/acemq/amqp/patterns/request_reply.rb', line 286

def await(timeout)
  deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
  @lock.synchronize do
    until @delivered
      left = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
      return nil if left <= 0

      # Waited in a loop because a condition variable may wake for no
      # reason, and a spurious wake would otherwise look like a reply.
      @ready.wait(@lock, left)
    end
    @message
  end
end

#deliver(message) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



277
278
279
280
281
282
283
# File 'lib/acemq/amqp/patterns/request_reply.rb', line 277

def deliver(message)
  @lock.synchronize do
    @message = message
    @delivered = true
    @ready.signal
  end
end