Class: Google::Gax::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/google/gax/bundling.rb

Overview

Container for a thread adding the ability to cancel, check if set, and get the result of the thread.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEvent

Returns a new instance of Event.



381
382
383
384
385
386
387
# File 'lib/google/gax/bundling.rb', line 381

def initialize
  @canceller = nil
  @result = nil
  @is_set = false
  @mutex = Mutex.new
  @resource = ConditionVariable.new
end

Instance Attribute Details

#cancellerObject

Returns the value of attribute canceller.



378
379
380
# File 'lib/google/gax/bundling.rb', line 378

def canceller
  @canceller
end

#resultObject

Returns the value of attribute result.



379
380
381
# File 'lib/google/gax/bundling.rb', line 379

def result
  @result
end

Instance Method Details

#cancelObject

Invokes the cancellation function provided. The returned cancellation function returns true if all elements was removed successfully from the inputs, and false if it was not.



412
413
414
415
416
417
418
419
420
421
# File 'lib/google/gax/bundling.rb', line 412

def cancel
  @mutex.synchronize do
    cancelled = canceller.nil? ? false : canceller.call
    # Broadcast if the event was successfully cancelled. If not,
    # the result should end up getting set by the sent api request.
    # When the result is set, the resource is going to broadcast.
    @resource.broadcast if cancelled
    cancelled
  end
end

#set?Boolean

Checks to see if the event has been set. A set Event signals that there is data in @result.

Returns:

  • (Boolean)

    Whether the event has been set.



405
406
407
# File 'lib/google/gax/bundling.rb', line 405

def set?
  @is_set
end

#wait(timeout_millis: nil) ⇒ Object

This is used to wait for a bundle request is complete and the event result is set.

Parameters:

  • timeout_millis (Numeric) (defaults to: nil)

    The number of milliseconds to wait before ceasing to wait. If nil, this function will wait indefinitely.



429
430
431
432
433
434
435
436
# File 'lib/google/gax/bundling.rb', line 429

def wait(timeout_millis: nil)
  @mutex.synchronize do
    return @is_set if @is_set
    t = timeout_millis.nil? ? nil : timeout_millis / MILLIS_PER_SECOND
    @resource.wait(@mutex, t)
    @is_set
  end
end