-
Notifications
You must be signed in to change notification settings - Fork 14
/
async.go
107 lines (93 loc) · 2.75 KB
/
async.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package gorums
import (
"context"
"github.com/relab/gorums/ordering"
"google.golang.org/protobuf/reflect/protoreflect"
)
// Async encapsulates the state of an asynchronous quorum call,
// and has methods for checking the status of the call or waiting for it to complete.
//
// This struct should only be used by generated code.
type Async struct {
reply protoreflect.ProtoMessage
err error
c chan struct{}
}
// Get returns the reply and any error associated with the called method.
// The method blocks until a reply or error is available.
func (f *Async) Get() (protoreflect.ProtoMessage, error) {
<-f.c
return f.reply, f.err
}
// Done reports if a reply and/or error is available for the called method.
func (f *Async) Done() bool {
select {
case <-f.c:
return true
default:
return false
}
}
type asyncCallState struct {
md *ordering.Metadata
data QuorumCallData
replyChan <-chan response
expectedReplies int
}
// AsyncCall starts an asynchronous quorum call, returning an Async object that can be used to retrieve the results.
//
// This function should only be used by generated code.
func (c RawConfiguration) AsyncCall(ctx context.Context, d QuorumCallData) *Async {
expectedReplies := len(c)
md := &ordering.Metadata{MessageID: c.getMsgID(), Method: d.Method}
replyChan := make(chan response, expectedReplies)
for _, n := range c {
msg := d.Message
if d.PerNodeArgFn != nil {
msg = d.PerNodeArgFn(d.Message, n.id)
if !msg.ProtoReflect().IsValid() {
expectedReplies--
continue // don't send if no msg
}
}
n.channel.enqueue(request{ctx: ctx, msg: &Message{Metadata: md, Message: msg}}, replyChan, false)
}
fut := &Async{c: make(chan struct{}, 1)}
go c.handleAsyncCall(ctx, fut, asyncCallState{
md: md,
data: d,
replyChan: replyChan,
expectedReplies: expectedReplies,
})
return fut
}
func (c RawConfiguration) handleAsyncCall(ctx context.Context, fut *Async, state asyncCallState) {
defer close(fut.c)
var (
resp protoreflect.ProtoMessage
errs []nodeError
quorum bool
replies = make(map[uint32]protoreflect.ProtoMessage)
)
for {
select {
case r := <-state.replyChan:
if r.err != nil {
errs = append(errs, nodeError{nodeID: r.nid, cause: r.err})
break
}
replies[r.nid] = r.msg
if resp, quorum = state.data.QuorumFunction(state.data.Message, replies); quorum {
fut.reply, fut.err = resp, nil
return
}
case <-ctx.Done():
fut.reply, fut.err = resp, QuorumCallError{cause: ctx.Err(), errors: errs, replies: len(replies)}
return
}
if len(errs)+len(replies) == state.expectedReplies {
fut.reply, fut.err = resp, QuorumCallError{cause: Incomplete, errors: errs, replies: len(replies)}
return
}
}
}