This repository has been archived by the owner on Jan 27, 2019. It is now read-only.
/
channel.go
636 lines (539 loc) · 14.5 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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*
Copyright © Max Mazurov (fox.cpp) 2018
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package zrpc
import (
"bytes"
"encoding/hex"
"fmt"
"math/rand"
"sync"
"sync/atomic"
"syscall"
"time"
"github.com/pebbe/zmq4"
"github.com/pkg/errors"
)
const (
HeartbeatEvent = "_zpc_hb"
WindowSize = "_zpc_more"
)
// ErrIncompatibleVersion is returned when version number in received message
// doesn't matches protocol version implemented by library.
type ErrIncompatibleVersion struct {
remoteVer int
}
func (ive ErrIncompatibleVersion) Error() string {
return fmt.Sprintf("zrpc: incompatible version: %d (theirs) != %d (ours)", ive.remoteVer, ProtocolVersion)
}
var ErrRemoteLost = errors.New("zrpc: remote stopped sending heartbeats")
type socket struct {
// Serializes access to pipeIn.
pipeInLck sync.Mutex
// Serializes access to chans map.
chansLock sync.RWMutex
sock *zmq4.Socket
chans map[string]*channel
// Written to by sendEventFromPipe, read by writeEvent.
pipeErrs chan error
// Where all errors occurred during polling go, nil by default.
readErrs chan<- error
// Used as a queue for outgoing events.
pipeIn, pipeOut *zmq4.Socket
dispatchReactor *zmq4.Reactor
// Used to "ack" that reactor is not running anymore so we can close sockets without race.
stopChan chan bool
// Atomically set by Close to signal tell all background goroutines to stop.
stopping uint32
// Called (if not nil) when new channel is open by remote.
// It is not safe to change it after socket.bind or socket.connect.
newChannel func(ch *channel)
// Configuration
noVersionCheck bool
chanBufferSize uint64
defaultHeartbeatInterval time.Duration
}
func newSocket(t zmq4.Type) (s *socket, err error) {
s = &socket{
chans: map[string]*channel{},
pipeErrs: make(chan error),
readErrs: nil,
stopChan: make(chan bool),
}
s.sock, err = zmq4.NewSocket(t)
if err != nil {
return nil, errors.Wrap(err, "new sock")
}
s.pipeIn, s.pipeOut, err = socketPair()
if err != nil {
return nil, errors.Wrap(err, "new sock")
}
s.dispatchReactor = zmq4.NewReactor()
s.dispatchReactor.AddSocket(s.pipeOut, zmq4.POLLIN, func(state zmq4.State) error {
if atomic.LoadUint32(&s.stopping) == 1 {
return errors.New("")
}
s.sendEventFromPipe()
return nil
})
s.dispatchReactor.AddSocket(s.sock, zmq4.POLLIN, func(state zmq4.State) error {
if atomic.LoadUint32(&s.stopping) == 1 {
return errors.New("")
}
s.dispatchEvent()
return nil
})
s.chanBufferSize = 10
s.defaultHeartbeatInterval = 5 * time.Second
go func() {
for {
if err := s.dispatchReactor.Run(-1); err != nil {
if err.Error() == "" {
s.stopChan <- true
return
}
if zmq4.AsErrno(err) == zmq4.Errno(syscall.EINTR) {
continue
}
}
break
}
s.stopChan <- true
}()
return s, nil
}
func (s *socket) Connect(endpoint string) error {
for {
err := s.sock.Connect(endpoint)
if err != nil {
if IsIntr(err) {
continue
}
}
break
}
return nil
}
func (s *socket) Bind(endpoint string) error {
for {
err := s.sock.Bind(endpoint)
if err != nil {
if IsIntr(err) {
continue
}
return err
}
break
}
return nil
}
func (s *socket) Close() (err error) {
defer func() {
if err != nil {
panic(err)
}
}()
atomic.StoreUint32(&s.stopping, 1)
s.chansLock.Lock()
for id := range s.chans {
s.closeChannelNoLock(id)
}
s.chansLock.Unlock()
for {
if _, err := s.pipeIn.SendMessage("WAKE_UP"); err != nil {
if IsIntr(err) {
continue
}
return err
}
break
}
<-s.stopChan
s.pipeInLck.Lock()
for {
if err := s.pipeIn.Close(); err != nil {
if IsIntr(err) {
continue
}
s.pipeInLck.Unlock()
return err
}
break
}
s.pipeInLck.Unlock()
for {
if err := s.pipeOut.Close(); err != nil {
if IsIntr(err) {
continue
}
return err
}
break
}
for {
if err := s.sock.Close(); err != nil {
if IsIntr(err) {
continue
}
return err
}
break
}
return nil
}
func (s *socket) sendEventFromPipe() {
var msgParts [][]byte
var err error
for {
msgParts, err = s.pipeOut.RecvMessageBytes(0)
if err != nil {
if IsIntr(err) {
continue
}
s.pipeErrs <- err
}
break
}
for i, part := range msgParts {
var flag zmq4.Flag
if i != len(msgParts)-1 {
flag = zmq4.SNDMORE
}
for {
if _, err := s.sock.SendBytes(part, flag); err != nil {
if IsIntr(err) {
continue
}
s.pipeErrs <- err
return
}
break
}
}
s.pipeErrs <- nil
}
// readEvent reads and parses event sent on socket.
// Returned identity is source peer's identity if socket have ROUTER type.
func (s *socket) readEvent() (ev *event, identity string, err error) {
var arr [][]byte
for {
arr, err = s.sock.RecvMessageBytes(0)
if err != nil {
if IsIntr(err) {
continue
}
return nil, "", errors.Wrap(err, "event read (IO err)")
}
break
}
ev = new(event)
if err := ev.UnmarshalBinary(bytes.NewReader(arr[len(arr)-1])); err != nil {
return nil, "", errors.Wrap(err, "event read")
}
if len(arr) > 1 {
return ev, string(arr[0]), nil
}
return ev, "", nil
}
// writeEvent schedules event to be sent to peer identified by passed address (should always be empty for
// client-side socket).
//
// Note: This is low-level operation and should not be used directly.
// Instead, use OpenSendEvent to send first event on channel and channel.SendEvent
// for further communication.
func (s *socket) writeEvent(ev *event, identity string) error {
evBin, err := ev.MarshalBinary()
if err != nil {
return errors.Wrap(err, "event write")
}
s.pipeInLck.Lock()
defer s.pipeInLck.Unlock()
if atomic.LoadUint32(&s.stopping) == 1 {
return zmq4.ErrorSocketClosed
}
for {
if identity == "" {
_, err = s.pipeIn.SendMessage(evBin)
} else {
_, err = s.pipeIn.SendMessage(identity, "", evBin)
}
if err != nil {
if IsIntr(err) {
continue
}
return errors.Wrap(err, "event write")
}
break
}
return <-s.pipeErrs
}
// openChannel allocates queue for certain channel and also links identity (address) with
// it, if identity != "". If queue for channel already exists - function does nothing.
// Returned value is channel queue.
//
// Note: This is low-level operation and should not be used directly.
// On server-side you should use newChannel callback to get notifications
// about new channels.
// On client-side you should use OpenSendEvent to start communication.
func (s *socket) openChannel(id, identity string) *channel {
if id == "" {
id = randomString(32)
}
s.chansLock.Lock()
defer s.chansLock.Unlock()
if ch, prs := s.chans[id]; prs {
return ch
}
ch := newChannel(s, id, identity)
s.chans[id] = ch
return ch
}
// OpenSendEvent allocates new channel and sends event on it.
//
// Note: This function is not usable if socket type is zmq4.ROUTER.
// (regular (not "zeroworkers") server can't start
// communication with client on it's own)
//
// This function automatically populates event header so you don't have to.
//
// This function should be used instead of manual openChannel+SendEvent to
// make generate events compatible with reference implementation
// (go-zrpc handles events generated by openChannel+SendEvent correctly,
// but they are protocol-incompatible in general).
func (s *socket) OpenSendEvent(ev *event) (*channel, error) {
ev.Hdr.Version = ProtocolVersion
ev.Hdr.MsgID = randomString(32)
ev.Hdr.ResponseTo = ""
if err := s.writeEvent(ev, ""); err != nil {
return nil, errors.Wrap(err, "OpenSendEvent")
}
return s.openChannel(ev.Hdr.MsgID, ""), nil
}
// closeChannelNoLock - Don't use directly. See CloseChannel.
func (s *socket) closeChannelNoLock(id string) {
ch, prs := s.chans[id]
if !prs {
return
}
delete(s.chans, id)
ch.close()
}
// CloseChannel frees all resources associated with certain channel.
func (s *socket) CloseChannel(id string) {
s.chansLock.Lock()
defer s.chansLock.Unlock()
s.closeChannelNoLock(id)
}
// dispatchEvents reads event from socket and sends it to corresponding channel's queue
// (creating new one if it doesn't exists yet).
func (s *socket) dispatchEvent() {
ev, identity, err := s.readEvent()
if err != nil {
s.reportError(errors.Wrap(err, "dispatch event"))
}
if !s.noVersionCheck && ev.Hdr.Version != ProtocolVersion {
s.reportError(errors.Wrap(ErrIncompatibleVersion{remoteVer: ev.Hdr.Version}, "dispatch event"))
}
if atomic.LoadUint32(&s.stopping) == 1 {
return
}
var ch *channel
if ev.Hdr.ResponseTo != "" {
var prs bool
s.chansLock.RLock()
ch, prs = s.chans[ev.Hdr.ResponseTo]
s.chansLock.RUnlock()
if !prs {
ch = s.openChannel(ev.Hdr.ResponseTo, identity)
if s.newChannel != nil {
s.newChannel(ch)
}
}
} else {
ch = s.openChannel(ev.Hdr.MsgID, identity)
if s.newChannel != nil {
s.newChannel(ch)
}
}
if atomic.LoadUint32(&s.stopping) == 1 {
return
}
if err := ch.handle(ev); err != nil {
ch.reportError(errors.Wrap(err, "dispatch event"))
}
}
// reportError makes all currently running channel.RecvEvent() calls
// return a passed error.
func (s *socket) reportError(err error) {
s.readErrs <- err
s.chansLock.RLock()
defer s.chansLock.RUnlock()
for _, ch := range s.chans {
ch.reportError(err)
}
}
type channel struct {
parent *socket
id, identity string
stateLck sync.Mutex
stopping bool
stopCh chan struct{}
queue chan *event
errorCh chan error
heartbeatLck sync.Mutex
heartbeatStop chan struct{}
heartbeatTicker *time.Ticker
lastHeartbeat int64 // stored in unix timestamp so we can use atomic ops on it
heartbeatInterval time.Duration
}
func newChannel(parent *socket, id, identity string) *channel {
c := new(channel)
c.parent = parent
c.id = id
c.identity = identity
c.queue = make(chan *event, parent.chanBufferSize)
c.errorCh = make(chan error)
c.stopCh = make(chan struct{})
if parent.defaultHeartbeatInterval != 0 {
// We assume that last heartbeat was during channel initialization
// so we will have point of reference during first check.
c.lastHeartbeat = time.Now().UnixNano()
c.heartbeatStop = make(chan struct{})
c.heartbeatInterval = parent.defaultHeartbeatInterval
c.heartbeatTicker = time.NewTicker(c.heartbeatInterval)
go c.heartbeat()
}
return c
}
// close channel frees channel's queue and all other associated resources.
// You should probably call closeChannel on parent's socket because it also
// have resources associated with channel and they should be freed too.
func (c *channel) close() {
c.heartbeatLck.Lock()
// nil heartbeatStop indicates that heartbeat is disabled
// due to lost remote and channel is basically in
// "dangling" state.
if c.heartbeatInterval != 0 && c.heartbeatStop != nil {
c.heartbeatStop <- struct{}{}
c.heartbeatTicker.Stop()
<-c.heartbeatStop
}
c.heartbeatLck.Unlock()
c.stateLck.Lock()
defer c.stateLck.Unlock()
c.stopping = true
close(c.queue)
c.errorCh = nil
}
// reportError makes running channel.RecvEvent(), if any, return an error.
func (c *channel) reportError(err error) {
c.stateLck.Lock()
defer c.stateLck.Unlock()
if c.stopping {
return
}
select {
case c.errorCh <- err: // looks like this still blocks if channel is nil, further investigation needed
default:
// no one is listening, skip
}
}
func (c *channel) heartbeat() {
for {
select {
case t := <-c.heartbeatTicker.C:
// We haven't seen heartbeat for two intervals now...
if atomic.LoadInt64(&c.lastHeartbeat)+2*c.heartbeatInterval.Nanoseconds() <= t.UnixNano() {
// Stop sending heartbeats since it makes no sense now (and channel is useless anyway).
c.heartbeatLck.Lock()
c.heartbeatStop = nil
c.heartbeatTicker.Stop()
c.heartbeatLck.Unlock()
c.reportError(ErrRemoteLost)
return
}
if err := c.SendEvent(&event{Name: HeartbeatEvent}); err != nil {
if err == zmq4.ErrorSocketClosed {
return
}
c.reportError(errors.Wrap(err, "heartbeat"))
}
case <-c.heartbeatStop:
c.heartbeatStop <- struct{}{}
return
}
}
}
// handle puts event into channel's queue or handles it specially if it
// is event layer's notification (like congestion control or heartbeat).
func (c *channel) handle(ev *event) error {
if ev.Name == HeartbeatEvent {
atomic.StoreInt64(&c.lastHeartbeat, time.Now().UnixNano())
return nil
}
if ev.Name == WindowSize {
// TODO: Respect congestion notification.
return nil
}
c.stateLck.Lock()
defer c.stateLck.Unlock()
if c.stopping {
return nil
}
select {
case c.queue <- ev:
case <-c.stopCh:
return nil
}
return nil
}
// RecvEvent pops first event from channel's queue or returns error if
// something bad happened on parent socket.
//
// Running RecvEvent concurrently from multiple goroutines will
// report error to only one of them, so special care should be taken
// in this case.
func (c *channel) RecvEvent() (*event, error) {
select {
case ev := <-c.queue:
return ev, nil
case err := <-c.errorCh:
return nil, err
case <-c.stopCh:
return nil, errors.New("RecvEvent: cancelled")
}
}
// SendEvent is convenience wrapper for "low-level" socket.writeEvent function.
// It should be always used when possible, because it accounts for window size reported
// by congestion control and automatically creates valid header.
func (c *channel) SendEvent(ev *event) error {
ev.Hdr.Version = ProtocolVersion
if ev.Hdr.MsgID == "" {
ev.Hdr.MsgID = randomString(32)
}
ev.Hdr.ResponseTo = c.id
return c.parent.writeEvent(ev, c.identity)
}
func randomString(length uint) string {
randBytes := make([]byte, length/2)
_, err := rand.Read(randBytes)
if err != nil {
panic(err)
}
return hex.EncodeToString(randBytes)
}