-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.go
261 lines (231 loc) · 7.13 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
package netaccept
import (
"context"
"crypto/tls"
"net"
"sync"
"time"
)
// An Server defines parameters to accept connections.
// It is similar with GoLang's http.Server.
type Server struct {
// Handler to invoke.
Handler Handler
// TLSConfig optionally provides a TLS configuration.
TLSConfig *tls.Config
// MaxConn provides maximum connection count. If it is zero, max connection is unlimited.
MaxConn int
mu sync.Mutex
cancelled bool
initialized bool
ctx context.Context
ctxCancel context.CancelFunc
listeners []net.Listener
conns map[net.Conn]struct{}
connsMu sync.RWMutex
}
// Shutdown gracefully shuts down the Server without interrupting any
// connections. Shutdown works by first closing all open listeners, and then waiting indefinitely for
// connections to exit Serve method of Handler and then shut down. If the provided
// context expires before the shutdown is complete, Shutdown returns the
// context's error, otherwise it returns any error returned from closing the
// Server's underlying Listener(s).
//
// When Shutdown is called, Serve, ServeTLS, ListenAndServe, and ListenAndServeTLS
// immediately return ErrServerClosed. Make sure the program doesn't exit and waits
// instead for Shutdown to return.
//
// Once Shutdown has been called on a server, it may not be reused;
// future calls to methods such as Serve will return ErrServerClosed.
func (srv *Server) Shutdown(ctx context.Context) (err error) {
err = srv.cancel()
for {
select {
case <-time.After(5 * time.Millisecond):
srv.connsMu.RLock()
if len(srv.conns) <= 0 {
srv.connsMu.RUnlock()
return
}
srv.connsMu.RUnlock()
case <-ctx.Done():
srv.connsMu.RLock()
for conn := range srv.conns {
conn.Close()
}
srv.connsMu.RUnlock()
err = ctx.Err()
return
}
}
}
// Close immediately closes all active net.Listeners and any connections.
// For a graceful shutdown, use Shutdown.
//
// Close returns any error returned from closing the Server's underlying
// Listener(s).
//
// When Close is called, Serve, ServeTLS, ListenAndServe, and ListenAndServeTLS
// immediately return ErrServerClosed.
//
// Once Close has been called on a server, it may not be reused;
// future calls to methods such as Serve will return ErrServerClosed.
func (srv *Server) Close() (err error) {
err = srv.cancel()
srv.connsMu.RLock()
for conn := range srv.conns {
conn.Close()
}
srv.connsMu.RUnlock()
return
}
// ListenAndServe listens on the given network and address; and then calls Serve to handle incoming connections.
//
// ListenAndServe always returns a non-nil error. After Shutdown or Close, the returned error is ErrServerClosed.
func (srv *Server) ListenAndServe(network, address string) error {
lis, err := net.Listen(network, address)
if err != nil {
return err
}
defer lis.Close()
return srv.Serve(lis)
}
// ListenAndServeTLS listens on the given network and address; and
// then calls ServeTLS to handle incoming TLS connections.
//
// Filenames containing a certificate and matching private key for the
// Server must be provided if neither the Server's TLSConfig.Certificates
// nor TLSConfig.GetCertificate are populated. If the certificate is
// signed by a certificate authority, the certFile should be the
// concatenation of the Server's certificate, any intermediates, and
// the CA's certificate.
//
// ListenAndServeTLS always returns a non-nil error. After Shutdown or Close, the returned error is ErrServerClosed.
func (srv *Server) ListenAndServeTLS(network, address string, certFile, keyFile string) error {
lis, err := net.Listen(network, address)
if err != nil {
return err
}
defer lis.Close()
return srv.ServeTLS(lis, certFile, keyFile)
}
// Serve accepts incoming connections on the Listener lis, creating a new service
// goroutine for each. The service goroutines read requests and then call
// srv.Handler to reply to them.
//
// Serve always returns a non-nil error and closes lis. After Shutdown or Close,
// the returned error is ErrServerClosed.
func (srv *Server) Serve(lis net.Listener) error {
var err error
defer lis.Close()
srv.mu.Lock()
if srv.cancelled {
srv.mu.Unlock()
return ErrServerClosed
}
if !srv.initialized {
srv.initialized = true
srv.ctx, srv.ctxCancel = context.WithCancel(context.Background())
srv.listeners = make([]net.Listener, 0, 4096)
}
srv.listeners = append(srv.listeners, &onceCloseListener{Listener: lis})
srv.mu.Unlock()
srv.connsMu.Lock()
srv.conns = make(map[net.Conn]struct{})
srv.connsMu.Unlock()
for srv.ctx.Err() == nil {
if srv.MaxConn > 0 {
srv.connsMu.RLock()
connCount := len(srv.conns)
srv.connsMu.RUnlock()
if connCount >= srv.MaxConn {
select {
case <-srv.ctx.Done():
return ErrServerClosed
case <-time.After(5 * time.Millisecond):
}
continue
}
}
var conn net.Conn
conn, err = lis.Accept()
if err != nil {
if oe, ok := err.(*net.OpError); ok && oe.Temporary() {
select {
case <-srv.ctx.Done():
return ErrServerClosed
case <-time.After(5 * time.Millisecond):
}
continue
}
if srv.ctx.Err() != nil {
return ErrServerClosed
}
return err
}
go srv.serve(conn)
}
return ErrServerClosed
}
// ServeTLS accepts incoming connections on the Listener lis, creating a
// new service goroutine for each. The service goroutines read requests and
// then call srv.Handler to reply to them.
//
// Files containing a certificate and matching private key for
// the Server must be provided if neither the Server's TLSConfig.Certificates
// nor TLSConfig.GetCertificate are populated. If the certificate is signed by
// a certificate authority, the certFile should be the concatenation of the
// server's certificate, any intermediates, and the CA's certificate.
//
// ServeTLS always closes lis unless returned error is TLSError.
// ServeTLS always returns a non-nil error. After Shutdown or Close, the returned error is ErrServerClosed.
func (srv *Server) ServeTLS(lis net.Listener, certFile, keyFile string) error {
var err error
var config *tls.Config
if srv.TLSConfig != nil {
config = srv.TLSConfig.Clone()
} else {
config = &tls.Config{}
}
configHasCert := len(config.Certificates) > 0 || config.GetCertificate != nil
if !configHasCert || certFile != "" || keyFile != "" {
config.Certificates = make([]tls.Certificate, 1)
config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return wrapTLSError(err)
}
}
return srv.Serve(tls.NewListener(lis, config))
}
// serve serves connection to handler.
func (srv *Server) serve(conn net.Conn) {
srv.connsMu.Lock()
srv.conns[conn] = struct{}{}
srv.connsMu.Unlock()
srv.Handler.Serve(conn)
conn.Close()
srv.connsMu.Lock()
delete(srv.conns, conn)
srv.connsMu.Unlock()
}
// cancel cancels serving operation and closes listener once, then returns closing error.
func (srv *Server) cancel() (err error) {
srv.mu.Lock()
defer srv.mu.Unlock()
if srv.cancelled {
return nil
}
srv.cancelled = true
if !srv.initialized {
return nil
}
srv.ctxCancel()
for _, lis := range srv.listeners {
if l, ok := lis.(*onceCloseListener); ok {
err = l.Close()
} else {
err = lis.Close()
}
}
return
}