forked from VolantMQ/volantmq
/
tcp.go
165 lines (132 loc) · 3.11 KB
/
tcp.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
package transport
import (
"crypto/tls"
"net"
"time"
"github.com/VolantMQ/volantmq/configuration"
"github.com/VolantMQ/volantmq/systree"
"go.uber.org/zap"
)
// ConfigTCP configuration of tcp transport
type ConfigTCP struct {
Scheme string
TLS *tls.Config
transport *Config
}
type tcp struct {
baseConfig
tls *tls.Config
listener net.Listener
}
// NewConfigTCP allocate new transport config for tcp transport
// Use of this function is preferable instead of direct allocation of ConfigTCP
func NewConfigTCP(transport *Config) *ConfigTCP {
return &ConfigTCP{
Scheme: "tcp",
transport: transport,
}
}
// NewTCP create new tcp transport
func NewTCP(config *ConfigTCP, internal *InternalConfig) (Provider, error) {
l := &tcp{}
l.quit = make(chan struct{})
l.InternalConfig = *internal
l.config = *config.transport
l.tls = config.TLS
var err error
if l.listener, err = net.Listen(config.Scheme, config.transport.Host+":"+config.transport.Port); err != nil {
return nil, err
}
if l.tls != nil {
l.protocol = "ssl"
} else {
l.protocol = "tcp"
}
l.log = configuration.GetLogger().Named("listener: " + l.protocol + "://:" + config.transport.Port)
return l, nil
}
func (l *tcp) Ready() error {
if err := l.baseReady(); err != nil {
return err
}
return nil
}
func (l *tcp) Alive() error {
if err := l.baseReady(); err != nil {
return err
}
return nil
}
// Close tcp listener
func (l *tcp) Close() error {
var err error
l.onceStop.Do(func() {
close(l.quit)
err = l.listener.Close()
l.onConnection.Wait()
l.listener = nil
l.log = nil
})
return err
}
// newConnTCP initiate connection with net.Conn tcp object and stat
func (l *tcp) newConnTCP(conn net.Conn, stat systree.BytesMetric) (Conn, error) {
//desc, err := netpoll.HandleReadOnce(conn)
//if err != nil {
// return nil, err
//}
newConn := &connTCP{
conn: conn,
stat: stat,
//pollDesc: pollDesc{
// desc: desc,
// ePoll: l.EventPoll,
//},
}
if l.tls != nil {
newConn.conn = tls.Server(conn, l.tls)
}
return newConn, nil
}
// Serve start serving connections
func (l *tcp) Serve() error {
var tempDelay time.Duration // how long to sleep on accept failure
for {
var cn net.Conn
var err error
if cn, err = l.listener.Accept(); err != nil {
// http://zhen.org/blog/graceful-shutdown-of-go-net-dot-listeners/
select {
case <-l.quit:
return nil
default:
}
// Borrowed from go1.3.3/src/pkg/net/http/server.go:1699
if ne, ok := err.(net.Error); ok && ne.Temporary() {
if tempDelay == 0 {
tempDelay = 5 * time.Millisecond
} else {
tempDelay *= 2
}
if max := 1 * time.Second; tempDelay > max {
tempDelay = max
}
l.log.Error("accept connection. Retrying",
zap.Error(err),
zap.Duration("retryIn", tempDelay))
time.Sleep(tempDelay)
continue
}
return err
}
l.onConnection.Add(1)
go func(cn net.Conn) {
defer l.onConnection.Done()
if inConn, e := l.newConnTCP(cn, l.Metric.Bytes()); e != nil {
l.log.Error("create connection interface", zap.Error(e))
} else {
l.handleConnection(inConn)
}
}(cn)
}
}