forked from zenazn/goji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graceful.go
57 lines (49 loc) · 1.62 KB
/
graceful.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
/*
Package graceful implements graceful shutdown for HTTP servers by closing idle
connections after receiving a signal. By default, this package listens for
interrupts (i.e., SIGINT), but when it detects that it is running under Einhorn
it will additionally listen for SIGUSR2 as well, giving your application
automatic support for graceful restarts/code upgrades.
*/
package graceful
import (
"net"
"sync/atomic"
"github.com/zenazn/goji/graceful/listener"
)
// WrapListener wraps an arbitrary net.Listener for use with graceful shutdowns.
// In the background, it uses the listener sub-package to Wrap the listener in
// Deadline mode. If another mode of operation is desired, you should call
// listener.Wrap yourself: this function is smart enough to not double-wrap
// listeners.
func WrapListener(l net.Listener) net.Listener {
if lt, ok := l.(*listener.T); ok {
appendListener(lt)
return lt
}
lt := listener.Wrap(l, listener.Deadline)
appendListener(lt)
return lt
}
func appendListener(l *listener.T) {
mu.Lock()
defer mu.Unlock()
listeners = append(listeners, l)
}
const errClosing = "use of closed network connection"
// During graceful shutdown, calls to Accept will start returning errors. This
// is inconvenient, since we know these sorts of errors are peaceful, so we
// silently swallow them.
func peacefulError(err error) error {
if atomic.LoadInt32(&closing) == 0 {
return err
}
// Unfortunately Go doesn't really give us a better way to select errors
// than this, so *shrug*.
if oe, ok := err.(*net.OpError); ok {
if oe.Op == "accept" && oe.Err.Error() == errClosing {
return nil
}
}
return err
}