Class: Bones::RPC::Synchronous::Future

Inherits:
Object
  • Object
show all
Defined in:
lib/bones/rpc/synchronous/future.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initializeFuture

Returns a new instance of Future.



9
10
11
12
13
14
15
# File 'lib/bones/rpc/synchronous/future.rb', line 9

def initialize
  @start = Time.now
  @mutex = Mutex.new
  @ready = false
  @result = nil
  @forwards = nil
end

Instance Method Details

#ready?Boolean

Check if this future has a value yet

Returns:

  • (Boolean)


18
19
20
# File 'lib/bones/rpc/synchronous/future.rb', line 18

def ready?
  @ready
end

#runtimeObject



68
69
70
71
72
73
74
# File 'lib/bones/rpc/synchronous/future.rb', line 68

def runtime
  if @stop
    @stop - @start
  else
    Time.now - @start
  end
end

#signal(value) ⇒ Object Also known as: <<

Signal this future with the given result value



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bones/rpc/synchronous/future.rb', line 52

def signal(value)
  @stop = Time.now
  result = Result.new(value, self)

  @mutex.synchronize do
    raise "the future has already happened!" if @ready

    @result = result
    @ready = true
  end
end

#value(timeout = nil) ⇒ Object Also known as: call

Obtain the value for this Future



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bones/rpc/synchronous/future.rb', line 23

def value(timeout = nil)
  ready = result = nil

  begin
    @mutex.lock

    if @ready
      ready = true
      result = @result
    end
  ensure
    @mutex.unlock
  end

  unless ready
    if timeout
      raise TimeoutError, "Timeout not supported by Bones::RPC::Synchronous backend"
    end
  end

  if result
    result.value
  else
    raise TimeoutError, "Timeout not supported by Bones::RPC::Synchronous backend"
  end
end