-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathchannel.go
238 lines (211 loc) · 5.44 KB
/
channel.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package gorums
import (
"context"
"math"
"math/rand"
"sync"
"sync/atomic"
"time"
"github.com/relab/gorums/ordering"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/reflect/protoreflect"
)
type request struct {
ctx context.Context
msg *Message
opts callOptions
}
type response struct {
nid uint32
msg protoreflect.ProtoMessage
err error
}
type channel struct {
sendQ chan request
nodeID uint32
mu sync.Mutex
lastError error
latency time.Duration
backoffCfg backoff.Config
rand *rand.Rand
gorumsClient ordering.GorumsClient
gorumsStream ordering.Gorums_NodeStreamClient
streamMut sync.RWMutex
streamBroken atomicFlag
parentCtx context.Context
streamCtx context.Context
cancelStream context.CancelFunc
responseRouter map[uint64]chan<- response
responseMut sync.Mutex
}
func newChannel(n *Node) *channel {
return &channel{
sendQ: make(chan request, n.mgr.opts.sendBuffer),
backoffCfg: n.mgr.opts.backoff,
nodeID: n.ID(),
latency: -1 * time.Second,
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
responseRouter: make(map[uint64]chan<- response),
}
}
func (c *channel) connect(ctx context.Context, conn *grpc.ClientConn) error {
var err error
c.parentCtx = ctx
c.streamCtx, c.cancelStream = context.WithCancel(c.parentCtx)
c.gorumsClient = ordering.NewGorumsClient(conn)
c.gorumsStream, err = c.gorumsClient.NodeStream(c.streamCtx)
if err != nil {
return err
}
go c.sendMsgs()
go c.recvMsgs()
return nil
}
func (c *channel) routeResponse(msgID uint64, resp response) {
c.responseMut.Lock()
defer c.responseMut.Unlock()
if ch, ok := c.responseRouter[msgID]; ok {
ch <- resp
delete(c.responseRouter, msgID)
}
}
func (c *channel) enqueue(req request, responseChan chan<- response) {
if responseChan != nil {
c.responseMut.Lock()
c.responseRouter[req.msg.Metadata.MessageID] = responseChan
c.responseMut.Unlock()
}
c.sendQ <- req
}
func (c *channel) sendMsg(req request) (err error) {
// unblock the waiting caller unless noSendWaiting is enabled
defer func() {
if req.opts.callType == E_Multicast || req.opts.callType == E_Unicast && !req.opts.noSendWaiting {
c.routeResponse(req.msg.Metadata.MessageID, response{})
}
}()
// don't send if context is already cancelled.
if req.ctx.Err() != nil {
return req.ctx.Err()
}
c.streamMut.RLock()
defer c.streamMut.RUnlock()
done := make(chan struct{}, 1)
// wait for either the message to be sent, or the request context being cancelled.
// if the request context was cancelled, then we most likely have a blocked stream.
go func() {
select {
case <-done:
case <-req.ctx.Done():
c.cancelStream()
}
}()
err = c.gorumsStream.SendMsg(req.msg)
if err != nil {
c.setLastErr(err)
c.streamBroken.set()
}
done <- struct{}{}
return err
}
func (c *channel) sendMsgs() {
var req request
for {
select {
case <-c.parentCtx.Done():
return
case req = <-c.sendQ:
}
// return error if stream is broken
if c.streamBroken.get() {
err := status.Errorf(codes.Unavailable, "stream is down")
c.routeResponse(req.msg.Metadata.MessageID, response{nid: c.nodeID, msg: nil, err: err})
continue
}
// else try to send message
err := c.sendMsg(req)
if err != nil {
// return the error
c.routeResponse(req.msg.Metadata.MessageID, response{nid: c.nodeID, msg: nil, err: err})
}
}
}
func (c *channel) recvMsgs() {
for {
resp := newMessage(responseType)
c.streamMut.RLock()
err := c.gorumsStream.RecvMsg(resp)
if err != nil {
c.streamBroken.set()
c.streamMut.RUnlock()
c.setLastErr(err)
// attempt to reconnect
c.reconnect()
} else {
c.streamMut.RUnlock()
err := status.FromProto(resp.Metadata.GetStatus()).Err()
c.routeResponse(resp.Metadata.MessageID, response{nid: c.nodeID, msg: resp.Message, err: err})
}
select {
case <-c.parentCtx.Done():
return
default:
}
}
}
func (c *channel) reconnect() {
c.streamMut.Lock()
defer c.streamMut.Unlock()
backoffCfg := c.backoffCfg
var retries float64
for {
var err error
c.streamCtx, c.cancelStream = context.WithCancel(c.parentCtx)
c.gorumsStream, err = c.gorumsClient.NodeStream(c.streamCtx)
if err == nil {
c.streamBroken.clear()
return
}
c.cancelStream()
c.setLastErr(err)
delay := float64(backoffCfg.BaseDelay)
max := float64(backoffCfg.MaxDelay)
for r := retries; delay < max && r > 0; r-- {
delay *= backoffCfg.Multiplier
}
delay = math.Min(delay, max)
delay *= 1 + backoffCfg.Jitter*(rand.Float64()*2-1)
select {
case <-time.After(time.Duration(delay)):
retries++
case <-c.parentCtx.Done():
return
}
}
}
func (c *channel) setLastErr(err error) {
c.mu.Lock()
defer c.mu.Unlock()
c.lastError = err
}
// lastErr returns the last error encountered (if any) when using this channel.
func (c *channel) lastErr() error {
c.mu.Lock()
defer c.mu.Unlock()
return c.lastError
}
// channelLatency returns the latency between the client and this channel.
func (c *channel) channelLatency() time.Duration {
c.mu.Lock()
defer c.mu.Unlock()
return c.latency
}
type atomicFlag struct {
flag int32
}
func (f *atomicFlag) set() { atomic.StoreInt32(&f.flag, 1) }
func (f *atomicFlag) get() bool { return atomic.LoadInt32(&f.flag) == 1 }
func (f *atomicFlag) clear() { atomic.StoreInt32(&f.flag, 0) }