-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.go
480 lines (439 loc) · 12.3 KB
/
server.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
// SPDX-License-Identifier: Apache-2.0
package frisbee
import (
"context"
"crypto/tls"
"errors"
"net"
"sync"
"sync/atomic"
"time"
"github.com/loopholelabs/frisbee-go/pkg/packet"
"github.com/loopholelabs/logging/types"
)
var (
OnClosedNil = errors.New("OnClosed function cannot be nil")
PreWriteNil = errors.New("PreWrite function cannot be nil")
ListenerNil = errors.New("listener cannot be nil")
)
var (
defaultOnClosed = func(_ *Async, _ error) {}
defaultPreWrite = func() {}
defaultStreamHandler = func(stream *Stream) {
_ = stream.Close()
}
)
// Server accepts connections from frisbee Clients and can send and receive frisbee Packets
type Server struct {
listener net.Listener
handlerTable HandlerTable
shutdown atomic.Bool
options *Options
wg sync.WaitGroup
connections map[*Async]struct{}
connectionsMu sync.Mutex
startedCh chan struct{}
concurrency uint64
limiter chan struct{}
baseContext context.Context
baseContextCancel context.CancelFunc
// onClosed is a function run by the server whenever a connection is closed
onClosed func(*Async, error)
// preWrite is run by the server before a write happens
preWrite func()
// streamHandler is used to handle incoming client-initiated streams on the server
streamHandler func(*Stream)
// ConnContext is used to define a connection-specific context based on the incoming connection
// and is run whenever a new connection is opened
ConnContext func(context.Context, *Async) context.Context
// StreamContext is used to define a stream-specific context based on the incoming stream
// and is run whenever a new stream is opened
StreamContext func(context.Context, *Stream) context.Context
// PacketContext is used to define a handler-specific contexts based on the incoming packet
// and is run whenever a new packet arrives
PacketContext func(context.Context, *packet.Packet) context.Context
// UpdateContext is used to update a handler-specific context whenever the returned
// Action from a handler is UPDATE
UpdateContext func(context.Context, *Async) context.Context
}
// NewServer returns an uninitialized frisbee Server with the registered HandlerTable.
// The Start method must then be called to start the server and listen for connections.
func NewServer(handlerTable HandlerTable, ctx context.Context, opts ...Option) (*Server, error) {
options := loadOptions(opts...)
baseContext, baseContextCancel := context.WithCancel(ctx)
s := &Server{
options: options,
connections: make(map[*Async]struct{}),
startedCh: make(chan struct{}),
baseContext: baseContext,
baseContextCancel: baseContextCancel,
onClosed: defaultOnClosed,
preWrite: defaultPreWrite,
streamHandler: defaultStreamHandler,
}
return s, s.SetHandlerTable(handlerTable)
}
// SetOnClosed sets the onClosed function for the server. If f is nil, it returns an error.
func (s *Server) SetOnClosed(f func(*Async, error)) error {
if f == nil {
return OnClosedNil
}
s.onClosed = f
return nil
}
// SetPreWrite sets the preWrite function for the server. If f is nil, it returns an error.
func (s *Server) SetPreWrite(f func()) error {
if f == nil {
return PreWriteNil
}
s.preWrite = f
return nil
}
// SetStreamHandler sets the streamHandler function for the server.
func (s *Server) SetStreamHandler(f func(context.Context, *Stream)) error {
s.streamHandler = func(stream *Stream) {
streamCtx := s.baseContext
if s.StreamContext != nil {
streamCtx = s.StreamContext(streamCtx, stream)
}
f(streamCtx, stream)
}
return nil
}
// SetHandlerTable sets the handler table for the server.
//
// This function should not be called once the server has started.
func (s *Server) SetHandlerTable(handlerTable HandlerTable) error {
for i := uint16(0); i < RESERVED9; i++ {
if _, ok := handlerTable[i]; ok {
return InvalidHandlerTable
}
}
s.handlerTable = handlerTable
return nil
}
// GetHandlerTable gets the handler table for the server.
//
// This function should not be called once the server has started.
func (s *Server) GetHandlerTable() HandlerTable {
return s.handlerTable
}
// SetConcurrency sets the maximum number of concurrent goroutines that will be created
// by the server to handle incoming packets.
//
// An important caveat of this is that handlers must always thread-safe if they share resources
// between connections. If the concurrency is set to a value != 1, then the handlers
// must also be thread-safe if they share resources per connection.
//
// This function should not be called once the server has started.
func (s *Server) SetConcurrency(concurrency uint64) {
s.concurrency = concurrency
if s.concurrency > 1 {
s.limiter = make(chan struct{}, s.concurrency)
}
}
// Start will start the frisbee server and its reactor goroutines
// to receive and handle incoming connections. If the baseContext, ConnContext,
// onClosed, OnShutdown, or preWrite functions have not been defined, it will
// use the default functions for these.
func (s *Server) Start(addr string) error {
var listener net.Listener
var err error
if s.options.TLSConfig != nil {
listener, err = tls.Listen("tcp", addr, s.options.TLSConfig)
} else {
listener, err = net.Listen("tcp", addr)
}
if err != nil {
return err
}
return s.StartWithListener(listener)
}
// StartWithListener will start the frisbee server and its reactor goroutines
// to receive and handle incoming connections with a given net.Listener. If the baseContext, ConnContext,
// onClosed, OnShutdown, or preWrite functions have not been defined, it will
// use the default functions for these.
func (s *Server) StartWithListener(listener net.Listener) error {
if listener == nil {
return ListenerNil
}
s.listener = listener
s.wg.Add(1)
close(s.startedCh)
return s.handleListener()
}
// started returns a channel that will be closed when the server has successfully started
//
// This is meant to only be used for testing purposes.
func (s *Server) started() <-chan struct{} {
return s.startedCh
}
func (s *Server) handleListener() error {
var backoff time.Duration
for {
newConn, err := s.listener.Accept()
if err != nil {
if s.shutdown.Load() {
s.wg.Done()
return nil
}
if ne, ok := err.(temporary); ok && ne.Temporary() {
if backoff == 0 {
backoff = minBackoff
} else {
backoff *= 2
}
if backoff > maxBackoff {
backoff = maxBackoff
}
s.Logger().Warn().Err(err).Msgf("Temporary Accept Error, retrying in %s", backoff)
time.Sleep(backoff)
if s.shutdown.Load() {
s.wg.Done()
return nil
}
continue
}
s.wg.Done()
return err
}
backoff = 0
s.ServeConn(newConn)
}
}
func (s *Server) createHandler(conn *Async, closed *atomic.Bool, wg *sync.WaitGroup, ctx context.Context, cancel context.CancelFunc) func(*packet.Packet) {
return func(p *packet.Packet) {
handlerFunc := s.handlerTable[p.Metadata.Operation]
if handlerFunc != nil {
packetCtx := ctx
if s.PacketContext != nil {
packetCtx = s.PacketContext(packetCtx, p)
}
outgoing, action := handlerFunc(packetCtx, p)
if outgoing != nil && outgoing.Metadata.ContentLength == uint32(outgoing.Content.Len()) {
s.preWrite()
err := conn.WritePacket(outgoing)
if outgoing != p {
packet.Put(outgoing)
}
packet.Put(p)
if err != nil {
_ = conn.Close()
if closed.CompareAndSwap(false, true) {
s.onClosed(conn, err)
}
cancel()
wg.Done()
return
}
} else {
packet.Put(p)
}
switch action {
case NONE:
case CLOSE:
_ = conn.Close()
if closed.CompareAndSwap(false, true) {
s.onClosed(conn, nil)
}
cancel()
}
} else {
packet.Put(p)
}
wg.Done()
}
}
func (s *Server) handleSinglePacket(frisbeeConn *Async, connCtx context.Context) {
var p *packet.Packet
var outgoing *packet.Packet
var action Action
var handlerFunc Handler
var err error
p, err = frisbeeConn.ReadPacket()
if err != nil {
_ = frisbeeConn.Close()
s.onClosed(frisbeeConn, err)
return
}
for {
handlerFunc = s.handlerTable[p.Metadata.Operation]
if handlerFunc != nil {
packetCtx := connCtx
if s.PacketContext != nil {
packetCtx = s.PacketContext(packetCtx, p)
}
outgoing, action = handlerFunc(packetCtx, p)
if outgoing != nil && outgoing.Metadata.ContentLength == uint32(outgoing.Content.Len()) {
s.preWrite()
err = frisbeeConn.WritePacket(outgoing)
if outgoing != p {
packet.Put(outgoing)
}
packet.Put(p)
if err != nil {
_ = frisbeeConn.Close()
s.onClosed(frisbeeConn, err)
return
}
} else {
packet.Put(p)
}
switch action {
case NONE:
case CLOSE:
_ = frisbeeConn.Close()
s.onClosed(frisbeeConn, nil)
return
}
} else {
packet.Put(p)
}
p, err = frisbeeConn.ReadPacket()
if err != nil {
_ = frisbeeConn.Close()
s.onClosed(frisbeeConn, err)
return
}
}
}
func (s *Server) handleUnlimitedPacket(frisbeeConn *Async, connCtx context.Context) {
p, err := frisbeeConn.ReadPacket()
if err != nil {
_ = frisbeeConn.Close()
s.onClosed(frisbeeConn, err)
return
}
wg := new(sync.WaitGroup)
var closed atomic.Bool
connCtx, cancel := context.WithCancel(connCtx)
handle := s.createHandler(frisbeeConn, &closed, wg, connCtx, cancel)
for {
wg.Add(1)
go handle(p)
p, err = frisbeeConn.ReadPacket()
if err != nil {
_ = frisbeeConn.Close()
if closed.CompareAndSwap(false, true) {
s.onClosed(frisbeeConn, err)
}
cancel()
wg.Wait()
return
}
}
}
func (s *Server) handleLimitedPacket(frisbeeConn *Async, connCtx context.Context) {
p, err := frisbeeConn.ReadPacket()
if err != nil {
_ = frisbeeConn.Close()
s.onClosed(frisbeeConn, err)
return
}
wg := new(sync.WaitGroup)
var closed atomic.Bool
connCtx, cancel := context.WithCancel(connCtx)
handler := s.createHandler(frisbeeConn, &closed, wg, connCtx, cancel)
handle := func(p *packet.Packet) {
handler(p)
<-s.limiter
}
for {
select {
case s.limiter <- struct{}{}:
wg.Add(1)
go handle(p)
p, err = frisbeeConn.ReadPacket()
if err != nil {
_ = frisbeeConn.Close()
if closed.CompareAndSwap(false, true) {
s.onClosed(frisbeeConn, err)
}
cancel()
wg.Wait()
return
}
case <-connCtx.Done():
_ = frisbeeConn.Close()
if closed.CompareAndSwap(false, true) {
s.onClosed(frisbeeConn, err)
}
wg.Wait()
return
}
}
}
// ServeConn takes a net.Conn and starts a goroutine to handle it using the Server.
func (s *Server) ServeConn(conn net.Conn) {
s.wg.Add(1)
go s.serveConn(conn)
}
// serveConn takes a net.Conn and serves it using the Server
// and assumes that the server's wait group has been incremented by 1.
func (s *Server) serveConn(newConn net.Conn) {
var err error
switch v := newConn.(type) {
case *net.TCPConn:
err = v.SetKeepAlive(true)
if err != nil {
s.Logger().Error().Err(err).Msg("Error while setting TCP Keepalive")
_ = v.Close()
s.wg.Done()
return
}
err = v.SetKeepAlivePeriod(s.options.KeepAlive)
if err != nil {
s.Logger().Error().Err(err).Msg("Error while setting TCP Keepalive Period")
_ = v.Close()
s.wg.Done()
return
}
}
frisbeeConn := NewAsync(newConn, s.Logger(), s.streamHandler)
connCtx := s.baseContext
s.connectionsMu.Lock()
if s.shutdown.Load() {
s.wg.Done()
return
}
s.connections[frisbeeConn] = struct{}{}
s.connectionsMu.Unlock()
if s.ConnContext != nil {
connCtx = s.ConnContext(connCtx, frisbeeConn)
}
switch s.concurrency {
case 0:
s.handleUnlimitedPacket(frisbeeConn, connCtx)
case 1:
s.handleSinglePacket(frisbeeConn, connCtx)
default:
s.handleLimitedPacket(frisbeeConn, connCtx)
}
s.connectionsMu.Lock()
if !s.shutdown.Load() {
delete(s.connections, frisbeeConn)
}
s.connectionsMu.Unlock()
s.wg.Done()
}
// Logger returns the server's logger (useful for ServerRouter functions)
func (s *Server) Logger() types.Logger {
return s.options.Logger
}
// Shutdown shuts down the frisbee server and kills all the goroutines and active connections
func (s *Server) Shutdown() error {
if s.shutdown.CompareAndSwap(false, true) {
s.baseContextCancel()
s.connectionsMu.Lock()
for c := range s.connections {
_ = c.Close()
delete(s.connections, c)
}
s.connectionsMu.Unlock()
defer s.wg.Wait()
if s.listener != nil {
return s.listener.Close()
}
}
return nil
}