-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.go
90 lines (78 loc) · 1.86 KB
/
listener.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
package main
import (
"context"
"net"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"github.com/wavesplatform/gowaves/pkg/proto"
)
type Listener struct {
ctx context.Context
wait func() error
bind proto.TCPAddr
declared proto.TCPAddr
cm *ConnectionManager
nl net.Listener
}
func NewListener(bind, declared proto.TCPAddr, cm *ConnectionManager) Service {
if declared.Empty() {
zap.S().Info("Declared address of Fork Detector is empty")
zap.S().Info("No network server will be started")
return &EmptyService{}
}
if bind.Empty() && bind.Port == 0 {
zap.S().Warn("Bind address is empty")
zap.S().Warn("No network server will be started")
return &EmptyService{}
}
zap.S().Infof("Starting network server on '%s'", bind.String())
return &Listener{
bind: bind,
declared: declared,
cm: cm,
}
}
func (l *Listener) Run(ctx context.Context) {
g, gc := errgroup.WithContext(ctx)
l.ctx = gc
l.wait = g.Wait
g.Go(l.run)
}
func (l *Listener) Shutdown() {
if err := l.nl.Close(); err != nil {
zap.S().Errorf("Failed to close listener on %s: %v", l.bind, err)
return
}
if err := l.wait(); err != nil {
zap.S().Warnf("Failed to shutdown Listener: %v", err)
}
zap.S().Info("Listener shutdown successfully")
}
func (l *Listener) run() error {
zap.S().Infof("Start listening on %s", l.bind.String())
var cfg net.ListenConfig
nl, err := cfg.Listen(l.ctx, "tcp", l.bind.String())
if err != nil {
return err
}
l.nl = nl
for {
select {
case <-l.ctx.Done():
return nil
default:
conn, acErr := l.nl.Accept()
if acErr != nil {
zap.S().Errorf("Failed to accept connection: %v", acErr)
continue
}
go func() {
if aErr := l.cm.Accept(l.ctx, conn); aErr != nil {
zap.S().Debugf("[LSN] Failed to accept incoming connection from '%s': %v",
conn.RemoteAddr().String(), aErr)
return
}
}()
}
}
}