Class: Restate::DurableFuture
- Inherits:
-
Object
- Object
- Restate::DurableFuture
- Defined in:
- lib/restate/durable_future.rb,
sig/restate.rbs
Overview
A durable future wrapping a VM handle. Lazily resolves on first await and caches the result.
Returned by ctx.run and ctx.sleep.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#handle ⇒ Integer
readonly
Returns the value of attribute handle.
Instance Method Summary collapse
-
#await ⇒ Object
Block until the result is available and return it.
-
#completed? ⇒ Boolean
Check whether the future has completed (non-blocking).
-
#initialize(ctx, handle, serde: nil) ⇒ DurableFuture
constructor
A new instance of DurableFuture.
-
#or_timeout(duration) ⇒ Object
Race
selfagainstRestate.sleep(duration). - #resolve! ⇒ void
Constructor Details
#initialize(ctx, handle, serde: nil) ⇒ DurableFuture
Returns a new instance of DurableFuture.
10 11 12 13 14 15 16 |
# File 'lib/restate/durable_future.rb', line 10 def initialize(ctx, handle, serde: nil) @ctx = ctx @handle = handle @serde = serde @resolved = false @value = nil end |
Instance Attribute Details
#handle ⇒ Integer (readonly)
Returns the value of attribute handle.
8 9 10 |
# File 'lib/restate/durable_future.rb', line 8 def handle @handle end |
Instance Method Details
#await ⇒ Object
Block until the result is available and return it. Caches across calls,
including failures — a second await on a failed future re-raises the
same TerminalError rather than re-fetching from the VM (the notification
is single-shot).
24 25 26 27 28 29 |
# File 'lib/restate/durable_future.rb', line 24 def await resolve! unless @resolved raise @error if @error @value end |
#completed? ⇒ Boolean
Check whether the future has completed (non-blocking).
34 35 36 |
# File 'lib/restate/durable_future.rb', line 34 def completed? @resolved || @ctx.completed?(@handle) end |
#or_timeout(duration) ⇒ Object
Race self against Restate.sleep(duration). Returns the value
if the future wins; raises TimeoutError otherwise.
Does not cancel the underlying work (matches TS/Java SDKs); on
a Restate::DurableCallFuture, call #cancel in the rescue if you want
the remote invocation stopped.
43 44 45 46 47 48 49 |
# File 'lib/restate/durable_future.rb', line 43 def or_timeout(duration) sleep_future = Restate.sleep(duration) Restate.wait_any(self, sleep_future) return await if completed? raise TimeoutError end |
#resolve! ⇒ void
This method returns an undefined value.
53 54 55 56 57 58 59 60 |
# File 'lib/restate/durable_future.rb', line 53 def resolve! raw = @ctx.resolve_handle(@handle) @value = @serde ? @serde.deserialize(raw) : raw rescue TerminalError => e @error = e ensure @resolved = true end |