-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathasync.go
689 lines (633 loc) · 18.2 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
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
// SPDX-License-Identifier: Apache-2.0
package frisbee
import (
"bufio"
"context"
"crypto/tls"
"encoding/binary"
"errors"
"net"
"sync"
"sync/atomic"
"time"
"github.com/loopholelabs/common/pkg/queue"
"github.com/loopholelabs/logging/loggers/noop"
"github.com/loopholelabs/logging/types"
"github.com/loopholelabs/frisbee-go/internal/dialer"
"github.com/loopholelabs/frisbee-go/pkg/metadata"
"github.com/loopholelabs/frisbee-go/pkg/packet"
)
// Async is the underlying asynchronous frisbee connection which has extremely efficient read and write logic and
// can handle the specific frisbee requirements. This is not meant to be used on its own, and instead is
// meant to be used by frisbee client and server implementations
type Async struct {
sync.Mutex
conn net.Conn
closed atomic.Bool
writer *bufio.Writer
flushCh chan struct{}
closeCh chan struct{}
incoming *queue.Circular[packet.Packet, *packet.Packet]
staleMu sync.Mutex
stale []*packet.Packet
logger types.Logger
wg sync.WaitGroup
errorMu sync.RWMutex
error error
streamsMu sync.Mutex
streams map[uint16]*Stream
newStreamHandlerMu sync.Mutex
newStreamHandler NewStreamHandler
}
// ConnectAsync creates a new TCP connection (using net.Dial) and wraps it in a frisbee connection
func ConnectAsync(addr string, keepAlive time.Duration, logger types.Logger, TLSConfig *tls.Config, streamHandler ...NewStreamHandler) (*Async, error) {
var conn net.Conn
var err error
d := dialer.NewRetry()
if TLSConfig != nil {
conn, err = d.DialTLS("tcp", addr, TLSConfig)
} else {
conn, err = d.Dial("tcp", addr)
if err == nil {
_ = conn.(*net.TCPConn).SetKeepAlive(true)
_ = conn.(*net.TCPConn).SetKeepAlivePeriod(keepAlive)
}
}
if err != nil {
return nil, err
}
return NewAsync(conn, logger, streamHandler...), nil
}
// NewAsync takes an existing net.Conn object and wraps it in a frisbee connection
func NewAsync(c net.Conn, logger types.Logger, streamHandler ...NewStreamHandler) (conn *Async) {
conn = &Async{
conn: c,
writer: bufio.NewWriterSize(c, DefaultBufferSize),
incoming: queue.NewCircular[packet.Packet, *packet.Packet](DefaultBufferSize),
flushCh: make(chan struct{}, 3),
closeCh: make(chan struct{}),
streams: make(map[uint16]*Stream),
logger: logger,
}
if logger == nil {
conn.logger = noop.New(types.InfoLevel)
}
if len(streamHandler) > 0 && streamHandler[0] != nil {
conn.newStreamHandler = streamHandler[0]
}
conn.wg.Add(1)
go conn.flushLoop()
conn.wg.Add(1)
go conn.readLoop()
conn.wg.Add(1)
go conn.pingLoop()
return
}
// SetDeadline sets the read and write deadline on the underlying net.Conn
func (c *Async) SetDeadline(t time.Time) error {
if c.closed.Load() {
return ConnectionClosed
}
return c.conn.SetDeadline(t)
}
// SetReadDeadline sets the read deadline on the underlying net.Conn
func (c *Async) SetReadDeadline(t time.Time) error {
if c.closed.Load() {
return ConnectionClosed
}
return c.conn.SetReadDeadline(t)
}
// SetWriteDeadline sets the write deadline on the underlying net.Conn
func (c *Async) SetWriteDeadline(t time.Time) error {
if c.closed.Load() {
return ConnectionClosed
}
return c.conn.SetWriteDeadline(t)
}
// ConnectionState returns the tls.ConnectionState of a *tls.Conn
// if the connection is not *tls.Conn then the NotTLSConnectionError is returned
func (c *Async) ConnectionState() (tls.ConnectionState, error) {
if tlsConn, ok := c.conn.(*tls.Conn); ok {
return tlsConn.ConnectionState(), nil
}
return emptyState, NotTLSConnectionError
}
// Handshake performs the tls.Handshake() of a *tls.Conn
// if the connection is not *tls.Conn then the NotTLSConnectionError is returned
func (c *Async) Handshake() error {
if tlsConn, ok := c.conn.(*tls.Conn); ok {
return tlsConn.Handshake()
}
return NotTLSConnectionError
}
// HandshakeContext performs the tls.HandshakeContext() of a *tls.Conn
// if the connection is not *tls.Conn then the NotTLSConnectionError is returned
func (c *Async) HandshakeContext(ctx context.Context) error {
if tlsConn, ok := c.conn.(*tls.Conn); ok {
return tlsConn.HandshakeContext(ctx)
}
return NotTLSConnectionError
}
// LocalAddr returns the local address of the underlying net.Conn
func (c *Async) LocalAddr() net.Addr {
return c.conn.LocalAddr()
}
// RemoteAddr returns the remote address of the underlying net.Conn
func (c *Async) RemoteAddr() net.Addr {
return c.conn.RemoteAddr()
}
// CloseChannel returns a channel that can be listened to for a close event on a frisbee connection
func (c *Async) CloseChannel() <-chan struct{} {
return c.closeCh
}
// WritePacket takes a packet.Packet and queues it up to send asynchronously.
//
// If packet.Metadata.ContentLength == 0, then the content array's length must be 0. Otherwise, it is required that packet.Metadata.ContentLength == len(content).
func (c *Async) WritePacket(p *packet.Packet) error {
if p.Metadata.Operation <= RESERVED9 {
return InvalidOperation
}
return c.writePacket(p, true)
}
// ReadPacket is a blocking function that will wait until a Frisbee packet is available and then return it (and its content).
// In the event that the connection is closed, ReadPacket will return an error.
func (c *Async) ReadPacket() (*packet.Packet, error) {
if c.closed.Load() {
c.staleMu.Lock()
if len(c.stale) > 0 {
var p *packet.Packet
p, c.stale = c.stale[0], c.stale[1:]
c.staleMu.Unlock()
return p, nil
}
c.staleMu.Unlock()
c.Logger().Debug().Err(ConnectionClosed).Msg("error while popping from packet queue")
return nil, ConnectionClosed
}
readPacket, err := c.incoming.Pop()
if err != nil {
if c.closed.Load() {
c.staleMu.Lock()
if len(c.stale) > 0 {
var p *packet.Packet
p, c.stale = c.stale[0], c.stale[1:]
c.staleMu.Unlock()
return p, nil
}
c.staleMu.Unlock()
c.Logger().Debug().Err(ConnectionClosed).Msg("error while popping from packet queue")
return nil, ConnectionClosed
}
c.Logger().Debug().Err(err).Msg("error while popping from packet queue")
return nil, err
}
return readPacket, nil
}
// Flush allows for synchronous messaging by flushing the write buffer and instantly sending packets
func (c *Async) Flush() error {
err := c.flush()
if err != nil {
return c.closeWithError(err)
}
return nil
}
// WriteBufferSize returns the size of the underlying write buffer (used for internal packet handling and for heartbeat logic)
func (c *Async) WriteBufferSize() int {
c.Lock()
if c.closed.Load() {
c.Unlock()
return 0
}
i := c.writer.Buffered()
c.Unlock()
return i
}
// Logger returns the underlying logger of the frisbee connection
func (c *Async) Logger() types.Logger {
return c.logger
}
// Error returns the error that caused the frisbee.Async connection to close
func (c *Async) Error() error {
c.errorMu.RLock()
defer c.errorMu.RUnlock()
return c.error
}
// Closed returns whether the frisbee.Async connection is closed
func (c *Async) Closed() bool {
return c.closed.Load()
}
// Raw shuts off all of frisbee's underlying functionality and converts the frisbee connection into a normal TCP connection (net.Conn)
func (c *Async) Raw() net.Conn {
_ = c.close()
return c.conn
}
// NewStream returns a new stream that can be used to send and receive packets
func (c *Async) NewStream(id uint16) (stream *Stream) {
c.streamsMu.Lock()
if stream = c.streams[id]; stream == nil {
stream = newStream(id, c)
c.streams[id] = stream
}
c.streamsMu.Unlock()
return
}
// SetNewStreamHandler sets the callback handler for new streams.
//
// It's important to note that this handler is called for new streams and if it is
// not set then stream packets will be dropped.
//
// It's also important to note that the handler itself is called in its own goroutine to
// avoid blocking the read lop. This means that the handler must be thread-safe.`
func (c *Async) SetNewStreamHandler(handler NewStreamHandler) {
c.newStreamHandlerMu.Lock()
c.newStreamHandler = handler
c.newStreamHandlerMu.Unlock()
}
// Close closes the frisbee connection gracefully
func (c *Async) Close() error {
err := c.close()
if err != nil && errors.Is(err, ConnectionClosed) {
return nil
}
_ = c.conn.Close()
return err
}
// write packet is the internal write packet function that does not check for reserved operations.
func (c *Async) writePacket(p *packet.Packet, closeOnErr bool) error {
if int(p.Metadata.ContentLength) != p.Content.Len() {
return InvalidContentLength
}
encodedMetadata := metadata.GetBuffer()
binary.BigEndian.PutUint16(encodedMetadata[metadata.IdOffset:metadata.IdOffset+metadata.IdSize], p.Metadata.Id)
binary.BigEndian.PutUint16(encodedMetadata[metadata.OperationOffset:metadata.OperationOffset+metadata.OperationSize], p.Metadata.Operation)
binary.BigEndian.PutUint32(encodedMetadata[metadata.ContentLengthOffset:metadata.ContentLengthOffset+metadata.ContentLengthSize], p.Metadata.ContentLength)
c.Lock()
if c.closed.Load() {
c.Unlock()
return ConnectionClosed
}
err := c.conn.SetWriteDeadline(time.Now().Add(DefaultDeadline))
if err != nil {
c.Unlock()
if c.closed.Load() {
c.Logger().Debug().Err(ConnectionClosed).Uint16("Packet ID", p.Metadata.Id).Msg("error while setting write deadline before writing packet")
return ConnectionClosed
}
c.Logger().Debug().Err(err).Uint16("Packet ID", p.Metadata.Id).Msg("error while setting write deadline before writing packet")
if closeOnErr {
return c.closeWithError(err)
}
return err
}
_, err = c.writer.Write(encodedMetadata[:])
metadata.PutBuffer(encodedMetadata)
if err != nil {
c.Unlock()
if c.closed.Load() {
c.Logger().Debug().Err(ConnectionClosed).Uint16("Packet ID", p.Metadata.Id).Msg("error while writing encoded metadata")
return ConnectionClosed
}
c.Logger().Debug().Err(err).Uint16("Packet ID", p.Metadata.Id).Msg("error while writing encoded metadata")
if closeOnErr {
return c.closeWithError(err)
}
return err
}
if p.Metadata.ContentLength != 0 {
_, err = c.writer.Write(p.Content.Bytes()[:p.Metadata.ContentLength])
if err != nil {
c.Unlock()
if c.closed.Load() {
c.Logger().Debug().Err(ConnectionClosed).Uint16("Packet ID", p.Metadata.Id).Msg("error while writing packet content")
return ConnectionClosed
}
c.Logger().Debug().Err(err).Uint16("Packet ID", p.Metadata.Id).Msg("error while writing packet content")
if closeOnErr {
return c.closeWithError(err)
}
return err
}
}
if len(c.flushCh) == 0 {
select {
case c.flushCh <- struct{}{}:
default:
}
}
c.Unlock()
return nil
}
// flush is an internal function for flushing data from the write buffer, however
// it is unique in that it does not call closeWithError (and so does not try and close the underlying connection)
// when it encounters an error, and instead leaves that responsibility to its parent caller
func (c *Async) flush() error {
c.Lock()
if c.closed.Load() {
c.Unlock()
return ConnectionClosed
}
if c.writer.Buffered() > 0 {
err := c.conn.SetWriteDeadline(time.Now().Add(DefaultDeadline))
if err != nil {
c.Unlock()
return err
}
err = c.writer.Flush()
if err != nil {
c.Unlock()
c.Logger().Error().Err(err).Msg("error while flushing data")
return err
}
}
c.Unlock()
return nil
}
func (c *Async) close() error {
c.staleMu.Lock()
c.streamsMu.Lock()
if c.closed.CompareAndSwap(false, true) {
c.Logger().Debug().Msg("connection close called, killing goroutines")
c.Lock()
c.incoming.Close()
close(c.closeCh)
close(c.flushCh)
c.Unlock()
_ = c.conn.SetDeadline(pastTime)
c.wg.Wait()
_ = c.conn.SetDeadline(emptyTime)
c.stale = c.incoming.Drain()
c.staleMu.Unlock()
for _, stream := range c.streams {
_ = stream.closeSend(false)
}
c.streamsMu.Unlock()
c.Lock()
if c.writer.Buffered() > 0 {
_ = c.conn.SetWriteDeadline(time.Now().Add(DefaultDeadline))
_ = c.writer.Flush()
_ = c.conn.SetWriteDeadline(emptyTime)
}
c.Unlock()
return nil
}
c.staleMu.Unlock()
c.streamsMu.Unlock()
return ConnectionClosed
}
func (c *Async) closeWithError(err error) error {
c.errorMu.Lock()
defer c.errorMu.Unlock()
c.error = err
closeError := c.close()
if closeError != nil {
c.Logger().Debug().Err(closeError).Msgf("attempted to close connection with error `%s`, but got error while closing", err)
c.error = errors.Join(closeError, err)
return c.error
}
_ = c.conn.Close()
return err
}
func (c *Async) flushLoop() {
var err error
for {
if _, ok := <-c.flushCh; !ok {
c.wg.Done()
return
}
err = c.flush()
if err != nil {
c.wg.Done()
_ = c.closeWithError(err)
return
}
}
}
func (c *Async) pingLoop() {
ticker := time.NewTicker(DefaultPingInterval)
defer ticker.Stop()
var err error
for {
select {
case <-c.closeCh:
c.wg.Done()
return
case <-ticker.C:
err = c.writePacket(PINGPacket, false)
if err != nil {
c.wg.Done()
_ = c.closeWithError(err)
return
}
}
}
}
func (c *Async) readLoop() {
buf := make([]byte, DefaultBufferSize)
var index int
var stream *Stream
var isStream bool
var newStreamHandler NewStreamHandler
for {
buf = buf[:cap(buf)]
if len(buf) < metadata.Size {
c.Logger().Debug().Err(InvalidBufferLength).Msg("error during read loop, calling closeWithError")
c.wg.Done()
_ = c.closeWithError(InvalidBufferLength)
return
}
var n int
var err error
for n < metadata.Size {
var nn int
err = c.conn.SetReadDeadline(time.Now().Add(DefaultDeadline))
if err != nil {
c.Logger().Debug().Err(err).Msg("error setting read deadline during read loop, calling closeWithError")
c.wg.Done()
_ = c.closeWithError(err)
return
}
nn, err = c.conn.Read(buf[n:])
n += nn
if err != nil {
if n < metadata.Size {
c.wg.Done()
_ = c.closeWithError(err)
return
}
break
}
}
index = 0
for index < n {
p := packet.Get()
p.Metadata.Id = binary.BigEndian.Uint16(buf[index+metadata.IdOffset : index+metadata.IdOffset+metadata.IdSize])
p.Metadata.Operation = binary.BigEndian.Uint16(buf[index+metadata.OperationOffset : index+metadata.OperationOffset+metadata.OperationSize])
p.Metadata.ContentLength = binary.BigEndian.Uint32(buf[index+metadata.ContentLengthOffset : index+metadata.ContentLengthOffset+metadata.ContentLengthSize])
index += metadata.Size
switch p.Metadata.Operation {
case PING:
c.Logger().Trace().Msg("PING Packet received by read loop, sending back PONG packet")
err = c.writePacket(PONGPacket, false)
if err != nil {
c.wg.Done()
_ = c.closeWithError(err)
return
}
packet.Put(p)
case PONG:
c.Logger().Trace().Msg("PONG Packet received by read loop")
packet.Put(p)
case STREAM:
c.Logger().Trace().Msg("STREAM Packet received by read loop")
isStream = true
c.newStreamHandlerMu.Lock()
newStreamHandler = c.newStreamHandler
c.newStreamHandlerMu.Unlock()
if newStreamHandler != nil || p.Metadata.ContentLength == 0 {
c.streamsMu.Lock()
stream = c.streams[p.Metadata.Id]
c.streamsMu.Unlock()
}
fallthrough
default:
if p.Metadata.ContentLength > 0 {
if n-index < int(p.Metadata.ContentLength) {
minSize := int(p.Metadata.ContentLength) - p.Content.Write(buf[index:n])
n = 0
for cap(buf) < minSize {
buf = append(buf[:cap(buf)], 0)
}
buf = buf[:cap(buf)]
for n < minSize {
var nn int
err = c.conn.SetReadDeadline(time.Now().Add(DefaultDeadline))
if err != nil {
c.wg.Done()
_ = c.closeWithError(err)
return
}
nn, err = c.conn.Read(buf[n:])
n += nn
if err != nil {
if n < minSize {
c.wg.Done()
_ = c.closeWithError(err)
return
}
break
}
}
p.Content.Write(buf[:minSize])
index = minSize
} else {
index += p.Content.Write(buf[index : index+int(p.Metadata.ContentLength)])
}
}
if !isStream {
err = c.incoming.Push(p)
if err != nil {
c.Logger().Debug().Err(err).Msg("error while pushing to incoming packet queue")
c.wg.Done()
_ = c.closeWithError(err)
return
}
} else {
if p.Metadata.ContentLength == 0 {
if stream != nil {
stream.close()
c.streamsMu.Lock()
delete(c.streams, p.Metadata.Id)
c.streamsMu.Unlock()
}
packet.Put(p)
} else {
if newStreamHandler == nil {
c.Logger().Debug().Msg("STREAM Packet discarded by read loop")
packet.Put(p)
} else {
if stream == nil {
stream = newStream(p.Metadata.Id, c)
c.streamsMu.Lock()
c.streams[p.Metadata.Id] = stream
c.streamsMu.Unlock()
go newStreamHandler(stream)
}
err = stream.queue.Push(p)
if err != nil {
c.Logger().Debug().Err(err).Msg("error while pushing to a stream queue packet queue")
c.wg.Done()
_ = c.closeWithError(err)
return
}
}
}
}
newStreamHandler = nil
stream = nil
isStream = false
}
if n == index {
index = 0
buf = buf[:cap(buf)]
if len(buf) < metadata.Size {
c.wg.Done()
_ = c.closeWithError(InvalidBufferLength)
return
}
n = 0
for n < metadata.Size {
var nn int
err = c.conn.SetReadDeadline(time.Now().Add(DefaultDeadline))
if err != nil {
c.wg.Done()
_ = c.closeWithError(err)
return
}
nn, err = c.conn.Read(buf[n:])
n += nn
if err != nil {
if n < metadata.Size {
c.wg.Done()
_ = c.closeWithError(err)
return
}
break
}
}
} else if n-index < metadata.Size {
copy(buf, buf[index:n])
n -= index
index = n
buf = buf[:cap(buf)]
minSize := metadata.Size - index
if len(buf) < minSize {
c.wg.Done()
_ = c.closeWithError(InvalidBufferLength)
return
}
n = 0
for n < minSize {
var nn int
err = c.conn.SetReadDeadline(time.Now().Add(DefaultDeadline))
if err != nil {
c.wg.Done()
_ = c.closeWithError(err)
return
}
nn, err = c.conn.Read(buf[index+n:])
n += nn
if err != nil {
if n < minSize {
c.wg.Done()
_ = c.closeWithError(err)
return
}
break
}
}
n += index
index = 0
}
}
}
}