-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.go
180 lines (154 loc) · 4.18 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
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
package tokay
import (
"fmt"
"net"
"sync/atomic"
"time"
)
func listenAndServe(engine *Engine, addr string) error {
s := engine.Server
ln, err := net.Listen("tcp4", addr)
if err != nil {
return err
}
if tcpln, ok := ln.(*net.TCPListener); ok {
listener := NewGracefulListener(tcpKeepaliveListener{
TCPListener: tcpln,
keepalive: s.TCPKeepalive,
keepalivePeriod: s.TCPKeepalivePeriod,
}, engine.maxGracefulWaitTime)
engine.Close = listener.Close
return s.Serve(listener)
}
return s.Serve(ln)
}
// ListenAndServeTLS serves HTTPS requests from the given TCP4 addr.
//
// certFile and keyFile are paths to TLS certificate and key files.
//
// Pass custom listener to Serve if you need listening on non-TCP4 media
// such as IPv6.
//
// If the certFile or keyFile has not been provided to the server structure,
// the function will use the previously added TLS configuration.
//
// Accepted connections are configured to enable TCP keep-alives.
func listenAndServeTLS(engine *Engine, addr, certFile, keyFile string) error {
s := engine.Server
ln, err := net.Listen("tcp4", addr)
if err != nil {
return err
}
if tcpln, ok := ln.(*net.TCPListener); ok {
listener := NewGracefulListener(tcpKeepaliveListener{
TCPListener: tcpln,
keepalive: s.TCPKeepalive,
keepalivePeriod: s.TCPKeepalivePeriod,
}, engine.maxGracefulWaitTime)
engine.Close = listener.Close
return s.ServeTLS(listener, certFile, keyFile)
}
return s.ServeTLS(ln, certFile, keyFile)
}
// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
// connections. It's used by ListenAndServe, ListenAndServeTLS and
// ListenAndServeTLSEmbed so dead TCP connections (e.g. closing laptop mid-download)
// eventually go away.
type tcpKeepaliveListener struct {
*net.TCPListener
keepalive bool
keepalivePeriod time.Duration
}
func (ln tcpKeepaliveListener) Accept() (net.Conn, error) {
tc, err := ln.AcceptTCP()
if err != nil {
return nil, err
}
if err := tc.SetKeepAlive(ln.keepalive); err != nil {
tc.Close() //nolint:errcheck
return nil, err
}
if ln.keepalivePeriod > 0 {
if err := tc.SetKeepAlivePeriod(ln.keepalivePeriod); err != nil {
tc.Close() //nolint:errcheck
return nil, err
}
}
return tc, nil
}
// GracefulListener defines a listener that we can gracefully stop
type GracefulListener struct {
// inner listener
ln net.Listener
// maximum wait time for graceful shutdown
maxWaitTime time.Duration
// this channel is closed during graceful shutdown on zero open connections.
done chan struct{}
// the number of open connections
connsCount uint64
// becomes non-zero when graceful shutdown starts
shutdown uint64
}
// NewGracefulListener wraps the given listener into 'graceful shutdown' listener.
func NewGracefulListener(ln net.Listener, maxWaitTime time.Duration) net.Listener {
return &GracefulListener{
ln: ln,
maxWaitTime: maxWaitTime,
done: make(chan struct{}),
}
}
// Accept creates a conn
func (ln *GracefulListener) Accept() (net.Conn, error) {
c, err := ln.ln.Accept()
if err != nil {
return nil, err
}
atomic.AddUint64(&ln.connsCount, 1)
return &gracefulConn{
Conn: c,
ln: ln,
}, nil
}
// Addr returns the listen address
func (ln *GracefulListener) Addr() net.Addr {
return ln.ln.Addr()
}
// Close closes the inner listener and waits until all the pending
// open connections are closed before returning.
func (ln *GracefulListener) Close() (err error) {
defer func() {
ln.ln.Close()
}()
return ln.waitForZeroConns()
}
func (ln *GracefulListener) waitForZeroConns() error {
atomic.AddUint64(&ln.shutdown, 1)
if atomic.LoadUint64(&ln.connsCount) == 0 {
close(ln.done)
return nil
}
select {
case <-ln.done:
return nil
case <-time.After(ln.maxWaitTime):
return fmt.Errorf("cannot complete graceful shutdown in %s", ln.maxWaitTime)
}
}
func (ln *GracefulListener) closeConn() {
connsCount := atomic.AddUint64(&ln.connsCount, ^uint64(0))
if atomic.LoadUint64(&ln.shutdown) != 0 && connsCount == 0 {
close(ln.done)
}
}
type gracefulConn struct {
net.Conn
ln *GracefulListener
}
func (c *gracefulConn) Close() error {
err := c.Conn.Close()
if err != nil {
return err
}
c.ln.closeConn()
return nil
}