-
Notifications
You must be signed in to change notification settings - Fork 3
/
future.go
93 lines (77 loc) · 2.28 KB
/
future.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package raft
import (
"errors"
"time"
)
// ErrTimeout is returned when a future timed out waiting for a result.
var ErrTimeout = errors.New(
"a timeout occurred while waiting for the result - try submitting the operation again",
)
// Response is the concrete result produced by a node after processing a client submitted operation.
type Response interface {
OperationResponse | Configuration
}
// Future represents an operation that will occur at a later point in time.
type Future[T Response] interface {
// Await retrieves the result of the future.
Await() Result[T]
}
// future implements the Future interface.
type future[T Response] struct {
// The channel that will receive the result.
responseCh chan Result[T]
// The amount of time to wait on a result before timing out.
timeout time.Duration
// The result of the future.
response Result[T]
}
func newFuture[T Response](timeout time.Duration) *future[T] {
return &future[T]{
timeout: timeout,
responseCh: make(chan Result[T], 1),
}
}
func (f *future[T]) Await() Result[T] {
if f.response != nil {
return f.response
}
select {
case response := <-f.responseCh:
f.response = response
case <-time.After(f.timeout):
f.response = &result[T]{err: ErrTimeout}
}
return f.response
}
// Result represents an abstract result produced by a node after processing a
// client submitted operation.
type Result[T Response] interface {
// Success returns the response associated with an operation.
// Error should always be called before Success - the result
// returned by Success is only valid if Error returns nil.
Success() T
// Error returns any error that occurred during the
// operation that was to produce the response.
Error() error
}
// result implements the Result interface.
type result[T Response] struct {
// The actual result of an operation.
success T
// Any error that occurred during the processing of the result.
err error
}
func (r *result[T]) Success() T {
return r.success
}
func (r *result[T]) Error() error {
return r.err
}
// respond creates a result with the provided response and error and sends
// it to the response channel without blocking.
func respond[T Response](responseCh chan Result[T], response T, err error) {
select {
case responseCh <- &result[T]{success: response, err: err}:
default:
}
}