-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
243 lines (199 loc) · 6.03 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
// Package server provides a foundation for creating remerge-style servers
package server
import (
"crypto/tls"
"fmt"
"net"
"sync"
"time"
"github.com/rcrowley/go-metrics"
"github.com/remerge/cue"
)
type Server struct {
Id string
Port int
TlsPort int // revive:disable:var-naming
TlsConfig *tls.Config // revive:disable:var-naming
listenConfig *net.ListenConfig
MaxConns int64
MaxConcurrentTLSHandshakes int64
BufferSize int
Timeout time.Duration // Service timeout. Default is 3s
Log cue.Logger
Handler Handler
listener *Listener
tlsListener *Listener
accepts metrics.Counter
tooManyConns metrics.Counter
closes metrics.Counter
numConns metrics.Counter
connections map[*Connection]struct{}
connectionsMutex sync.Mutex
numHandshakes metrics.Counter
tooManyConcurrentHandshakes metrics.Counter
tlsErrors metrics.Counter
}
//const keepAlive = 3 * time.Minute
func NewServer(port int) (server *Server, err error) {
server = &Server{
Id: fmt.Sprintf("server:%d", port),
Port: port,
BufferSize: 32768,
Timeout: 3 * time.Second,
connections: make(map[*Connection]struct{}),
}
server.Log = cue.NewLogger(server.Id)
server.Log.Infof("new server on port %d", port)
server.listenConfig = &net.ListenConfig{
//KeepAlive: keepAlive,
}
server.accepts = metrics.GetOrRegisterCounter(fmt.Sprintf("rex_server,port=%d accept", port), nil)
server.tooManyConns = metrics.GetOrRegisterCounter(fmt.Sprintf("rex_server,port=%d too_many_connection", port), nil)
server.closes = metrics.GetOrRegisterCounter(fmt.Sprintf("rex_server,port=%d close", port), nil)
server.numConns = metrics.GetOrRegisterCounter(fmt.Sprintf("rex_server,port=%d connection", port), nil)
server.numHandshakes = metrics.GetOrRegisterCounter(fmt.Sprintf("rex_server,port=%d handshakes", port), nil)
server.tooManyConcurrentHandshakes = metrics.GetOrRegisterCounter(fmt.Sprintf("rex_server,port=%d too_many_concurrent_handshakes", port), nil)
server.tlsErrors = metrics.GetOrRegisterCounter(fmt.Sprintf("rex_server,port=%d tls_error", port), nil)
return server, nil
}
func NewServerWithTLS(port int, tlsPort int, key string, cert string) (server *Server, err error) {
server, err = NewServer(port)
if err != nil {
return nil, err
}
if tlsPort < 1 {
return server, nil
}
server.Log.Infof("using TLS key=%v cert=%v", key, cert)
pair, err := tls.LoadX509KeyPair(cert, key)
if err != nil {
return nil, err
}
server.TlsConfig = &tls.Config{
Certificates: []tls.Certificate{pair},
MinVersion: tls.VersionTLS12,
}
server.TlsPort = tlsPort
return server, nil
}
func (server *Server) HasTLS() bool {
return server.TlsPort > 0 && server.TlsConfig != nil
}
func (server *Server) Listen() (err error) {
server.listener, err = NewListener(server.Port, server.listenConfig)
if err != nil {
return err
}
if server.HasTLS() {
server.tlsListener, err = NewTlsListener(server.TlsPort, server.TlsConfig, server.listenConfig)
if err != nil {
return err
}
}
return nil
}
func (server *Server) Run() error {
if err := server.Listen(); err != nil {
return err
}
server.Serve()
if server.HasTLS() {
server.ServeTLS()
}
return nil
}
func (server *Server) Stop() {
if server == nil {
return
}
if server.HasTLS() && server.tlsListener != nil {
server.Log.Infof("shutting down TLS listener")
server.tlsListener.Stop()
server.Log.Infof("waiting for requests to finish")
server.tlsListener.Wait()
}
if server.listener != nil {
server.Log.Infof("shutting down listener")
server.listener.Stop()
server.Log.Infof("waiting for requests to finish")
server.listener.Wait()
}
server.connectionsMutex.Lock()
for c := range server.connections {
c.closeMutex.Lock()
c.closeInternal()
c.closeMutex.Unlock()
}
server.connectionsMutex.Unlock()
server.waitForConnectionsToClose()
}
// StopDeadline is the hard deadline, after which Server.Stop() does no longer
// wait for connections to close and returns.
var StopDeadline = time.Minute
// waitForConnectionsToClose waits until all the handlers from listener and
// tlsListener are done and closed their connections.
func (server *Server) waitForConnectionsToClose() {
deadline := time.NewTimer(StopDeadline)
for {
if server.numConns.Count() == 0 {
server.Log.Infof("all requests finished")
break
}
select {
case <-deadline.C:
server.Log.Warnf("not all requests finished after %v", StopDeadline)
break
case <-time.After(time.Millisecond):
}
}
}
func (server *Server) Serve() {
server.listener.wg.Add(1)
go func() {
server.Log.Panic(server.listener.Run(server.acceptLoop), "could not run the listener")
}()
}
func (server *Server) ServeTLS() {
server.tlsListener.wg.Add(1)
go func() {
server.Log.Panic(server.tlsListener.Run(server.acceptLoop), "could not run the TLS listener")
}()
}
func (server *Server) acceptLoop(listener *Listener) error {
defer listener.Close()
for {
if listener.IsStopped() {
return nil
}
conn, err := listener.Accept()
if err != nil {
if listener.IsStopped() {
return nil
}
return err
}
server.accepts.Inc(1)
// for cases of probe or blackhole connection
if err = conn.SetDeadline(time.Now().Add(server.Timeout)); err != nil {
_ = conn.Close()
continue
}
if server.MaxConns > 0 && server.numConns.Count() > server.MaxConns {
server.tooManyConns.Inc(1)
_ = conn.Close()
continue
}
if tlsConn, ok := conn.(*tls.Conn); ok {
if server.MaxConcurrentTLSHandshakes > 0 && server.numHandshakes.Count() >= server.MaxConcurrentTLSHandshakes {
server.tooManyConcurrentHandshakes.Inc(1)
_ = tlsConn.Close()
continue
}
// We need to increase the outstanding handshakes here otherwise we
// accept way more than the limit due to the races with the new go routine.
// This is safe as well as long as .Server() is called which does a numHandshakes.Dec(1) .
server.numHandshakes.Inc(1)
}
go server.NewConnection(conn).Serve()
}
}