Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Future ¶
type Future interface {
// Await blocks until the Future is completed or context is canceled and
// returns either a result or an error.
Await(context.Context) (proto.Message, error)
// contains filtered or unexported methods
}
Future represents a value which may or may not currently be available, but will be available at some point in the future, or an error if that value could not be made available. It provides a way to handle asynchronous computations and their results.
The Future interface provides two main methods:
1. Await(ctx context.Context) (proto.Message, error):
- This method blocks until the Future is completed or the provided context is canceled. It returns either the result of the computation or an error if the computation failed or the context was canceled.
2. complete(value proto.Message, err error):
- This method completes the Future with either a value or an error. It is used internally by the completable to set the result of the computation.
Example usage:
task := func() (proto.Message, error) {
// Perform some long-running computation
result := &MyProtoMessage{...}
return result, nil
}
future := future.New(task)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
result, err := future.Await(ctx)
if err != nil {
log.Fatalf("Failed to get result: %v", err)
}
log.Printf("Received result: %v", result)
func New ¶
New creates a new Future that executes the given long-running task. The task is a function that returns a proto.Message and an error. The Future is completed with the value returned by the task or failed with the error.
The task is executed asynchronously in a separate goroutine. The Future can be awaited using the Await method, which will block until the task is completed or the provided context is canceled.
Example usage:
task := func() (proto.Message, error) {
// Perform some long-running computation
result := &MyProtoMessage{...}
return result, nil
}
future := future.New(task)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
result, err := future.Await(ctx)
if err != nil {
log.Fatalf("Failed to get result: %v", err)
}
log.Printf("Received result: %v", result)