-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchannel.go
446 lines (372 loc) · 10.1 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package lime
import (
"context"
"errors"
"fmt"
"log"
"reflect"
"sync"
)
type MessageSender interface {
SendMessage(ctx context.Context, msg *Message) error
}
type MessageReceiver interface {
ReceiveMessage(ctx context.Context) (*Message, error)
MsgChan() <-chan *Message
}
type NotificationSender interface {
SendNotification(ctx context.Context, not *Notification) error
}
type NotificationReceiver interface {
ReceiveNotification(ctx context.Context) (*Notification, error)
NotChan() <-chan *Notification
}
type RequestCommandSender interface {
SendRequestCommand(ctx context.Context, cmd *RequestCommand) error
}
type ResponseCommandSender interface {
SendResponseCommand(ctx context.Context, cmd *ResponseCommand) error
}
type RequestCommandReceiver interface {
ReceiveRequestCommand(ctx context.Context) (*RequestCommand, error)
ReqCmdChan() <-chan *RequestCommand
}
type ResponseCommandReceiver interface {
ReceiveResponseCommand(ctx context.Context) (*ResponseCommand, error)
RespCmdChan() <-chan *ResponseCommand
}
type CommandProcessor interface {
ProcessCommand(ctx context.Context, cmd *RequestCommand) (*ResponseCommand, error)
}
type Receiver interface {
MessageReceiver
NotificationReceiver
RequestCommandReceiver
ResponseCommandReceiver
}
// Sender defines a service for sending envelopes to a remote party.
type Sender interface {
MessageSender
NotificationSender
RequestCommandSender
ResponseCommandSender
}
type channel struct {
transport Transport
sessionID string
remoteNode Node
localNode Node
state SessionState
stateMu sync.RWMutex
inMsgChan chan *Message
inNotChan chan *Notification
inReqCmdChan chan *RequestCommand
inRespCmdChan chan *ResponseCommand
inSesChan chan *Session
sendMu sync.Mutex
rcvMu sync.Mutex
startRcv sync.Once
stopRcv sync.Once
rcvDone chan struct{}
client bool
processingCmds map[string]chan *ResponseCommand
processingCmdsMu sync.RWMutex
cancel context.CancelFunc // The function for cancelling the listener goroutine
}
func newChannel(t Transport, bufferSize int) *channel {
if t == nil || reflect.ValueOf(t).IsNil() {
panic("transport cannot be nil")
}
c := channel{
transport: t,
state: SessionStateNew,
inMsgChan: make(chan *Message, bufferSize),
inNotChan: make(chan *Notification, bufferSize),
inReqCmdChan: make(chan *RequestCommand, bufferSize),
inRespCmdChan: make(chan *ResponseCommand, bufferSize),
inSesChan: make(chan *Session, 1),
rcvDone: make(chan struct{}),
processingCmds: make(map[string]chan *ResponseCommand),
processingCmdsMu: sync.RWMutex{},
}
return &c
}
func (c *channel) Established() bool {
return c.State() == SessionStateEstablished && c.transport.Connected()
}
func (c *channel) startReceiver() {
defer c.rcvMu.Unlock()
c.rcvMu.Lock()
ctx, cancel := context.WithCancel(context.Background())
c.cancel = cancel
go receiveFromTransport(ctx, c, c.rcvDone)
}
func (c *channel) stopReceiver() {
defer c.rcvMu.Unlock()
c.rcvMu.Lock()
if c.cancel != nil {
c.cancel()
<-c.rcvDone
}
}
func (c *channel) setState(state SessionState) {
c.setStateWLock(state)
switch state {
case SessionStateEstablished:
c.startRcv.Do(c.startReceiver)
case SessionStateFinished, SessionStateFailed:
c.stopRcv.Do(c.stopReceiver)
}
}
func (c *channel) setStateWLock(state SessionState) {
c.stateMu.Lock()
defer c.stateMu.Unlock()
if state.Step() < c.state.Step() {
panic(fmt.Errorf("cannot change from state %s to %s", c.state, state))
}
c.state = state
}
func (c *channel) MsgChan() <-chan *Message {
return c.inMsgChan
}
func (c *channel) NotChan() <-chan *Notification {
return c.inNotChan
}
func (c *channel) ReqCmdChan() <-chan *RequestCommand {
return c.inReqCmdChan
}
func (c *channel) RespCmdChan() <-chan *ResponseCommand {
return c.inRespCmdChan
}
func receiveFromTransport(ctx context.Context, c *channel, done chan<- struct{}) {
defer func() {
close(done)
close(c.inMsgChan)
close(c.inNotChan)
close(c.inReqCmdChan)
close(c.inRespCmdChan)
close(c.inSesChan)
}()
for c.Established() {
env, err := c.transport.Receive(ctx)
if err != nil {
if ctx.Err() == nil {
log.Printf("receiveFromTransport: %v", err)
}
return
}
switch e := env.(type) {
case *Message:
select {
case <-ctx.Done():
return
case c.inMsgChan <- e:
}
case *Notification:
select {
case <-ctx.Done():
return
case c.inNotChan <- e:
}
case *RequestCommand:
select {
case <-ctx.Done():
return
case c.inReqCmdChan <- e:
}
case *ResponseCommand:
if !c.trySubmitCommandResult(e) {
select {
case <-ctx.Done():
return
case c.inRespCmdChan <- e:
}
}
case *Session:
select {
case <-ctx.Done():
return
case c.inSesChan <- e:
// If a session is received while established,
// the receiver goroutine can stop.
if c.client {
c.setStateWLock(e.State)
}
return
}
default:
panic(fmt.Errorf("unknown envelope type %v", reflect.ValueOf(e)))
}
}
}
func (c *channel) ID() string {
return c.sessionID
}
func (c *channel) RemoteNode() Node {
return c.remoteNode
}
func (c *channel) LocalNode() Node {
return c.localNode
}
func (c *channel) State() SessionState {
c.stateMu.RLock()
defer c.stateMu.RUnlock()
return c.state
}
func (c *channel) sendSession(ctx context.Context, ses *Session) error {
if err := c.ensureTransportOK("send session"); err != nil {
return err
}
// check the current channel state
state := c.State()
if state == SessionStateFinished || state == SessionStateFailed {
return fmt.Errorf("send session: cannot do in the %v state", state)
}
err := c.transport.Send(ctx, ses)
if err != nil {
return fmt.Errorf("send session: transport error: %w", err)
}
return nil
}
func (c *channel) receiveSession(ctx context.Context) (*Session, error) {
if ctx == nil {
panic("nil context")
}
state := c.State()
switch state {
case SessionStateFinished:
return nil, fmt.Errorf("receive session: cannot do in the %v state", state)
case SessionStateEstablished:
select {
case <-ctx.Done():
return nil, fmt.Errorf("receive session: %w", ctx.Err())
case s, ok := <-c.inSesChan:
if !ok {
return nil, errors.New("receive session: channel closed")
}
return s, nil
}
}
if err := c.ensureTransportOK("receive session"); err != nil {
return nil, err
}
env, err := c.transport.Receive(ctx)
if err != nil {
return nil, fmt.Errorf("receive session: transport error: %w", err)
}
ses, ok := env.(*Session)
if !ok {
return nil, errors.New("receive session: unexpected envelope type")
}
return ses, nil
}
func (c *channel) SendMessage(ctx context.Context, msg *Message) error {
return c.sendToTransport(ctx, msg, "send message")
}
func (c *channel) SendNotification(ctx context.Context, not *Notification) error {
return c.sendToTransport(ctx, not, "send notification")
}
func (c *channel) SendRequestCommand(ctx context.Context, cmd *RequestCommand) error {
return c.sendToTransport(ctx, cmd, "send request command")
}
func (c *channel) SendResponseCommand(ctx context.Context, cmd *ResponseCommand) error {
return c.sendToTransport(ctx, cmd, "send response command")
}
func (c *channel) ProcessCommand(ctx context.Context, reqCmd *RequestCommand) (*ResponseCommand, error) {
return c.processCommand(ctx, c, reqCmd)
}
func (c *channel) Close() error {
c.stopRcv.Do(c.stopReceiver)
if c.transport.Connected() {
return c.transport.Close()
}
return nil
}
func (c *channel) sendToTransport(ctx context.Context, e envelope, action string) error {
if e == nil || reflect.ValueOf(e).IsNil() {
panic(fmt.Errorf("%v: envelope cannot be nil", action))
}
if err := c.ensureEstablished(action); err != nil {
return err
}
c.sendMu.Lock()
defer c.sendMu.Unlock()
if err := c.transport.Send(ctx, e); err != nil {
return fmt.Errorf("%v: %w", action, err)
}
return nil
}
func (c *channel) ensureEstablished(action string) error {
return c.ensureState(SessionStateEstablished, action)
}
func (c *channel) ensureState(state SessionState, action string) error {
if err := c.ensureTransportOK(action); err != nil {
return err
}
s := c.State()
if s != state {
return fmt.Errorf("%v: cannot do in the %v state", action, s)
}
return nil
}
func (c *channel) ensureTransportOK(action string) error {
if c.transport == nil || reflect.ValueOf(c.transport).IsNil() {
return fmt.Errorf("%v: transport is nil", action)
}
if !c.transport.Connected() {
return fmt.Errorf("%v: transport is not connected", action)
}
return nil
}
func (c *channel) processCommand(ctx context.Context, sender RequestCommandSender, reqCmd *RequestCommand) (*ResponseCommand, error) {
if reqCmd == nil {
panic("process command: command cannot be nil")
}
if reqCmd.ID == "" {
panic("process command: invalid command id")
}
c.processingCmdsMu.Lock()
if _, ok := c.processingCmds[reqCmd.ID]; ok {
c.processingCmdsMu.Unlock()
return nil, errors.New("process command: the command id is already in use")
}
respChan := make(chan *ResponseCommand, 1)
c.processingCmds[reqCmd.ID] = respChan
c.processingCmdsMu.Unlock()
defer func() {
c.processingCmdsMu.Lock()
delete(c.processingCmds, reqCmd.ID)
c.processingCmdsMu.Unlock()
}()
err := sender.SendRequestCommand(ctx, reqCmd)
if err != nil {
return nil, err
}
select {
case <-ctx.Done():
return nil, fmt.Errorf("process command: %w", ctx.Err())
case respCmd := <-respChan:
return respCmd, nil
}
}
func (c *channel) trySubmitCommandResult(respCmd *ResponseCommand) bool {
if respCmd == nil {
return false
}
c.processingCmdsMu.RLock()
respChan, ok := c.processingCmds[respCmd.ID]
c.processingCmdsMu.RUnlock()
if !ok {
return false
}
c.processingCmdsMu.Lock()
delete(c.processingCmds, respCmd.ID)
c.processingCmdsMu.Unlock()
respChan <- respCmd
return true
}
// RcvDone signals when the channel receiver goroutine is done.
// This usually indicates that the session with the remote node was finished.
func (c *channel) RcvDone() <-chan struct{} {
return c.rcvDone
}