-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.go
67 lines (61 loc) · 1.69 KB
/
serve.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
// Package gracefully provides the functionality to server `net/http'
// servers while providing graceful shutdown for them when
// encountering OS interrupt and termination signals.
//
package gracefully
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
var defaultOpts = opts{
log: defaultLog,
graceDuration: 30 * time.Second,
}
// ServeHandler wraps the given handler and address as an `http.Server'
// and then executes `Serve' to serve the newly created server
// with graceful shutdown.
//
func ServeHandler(h http.Handler, addr string, opts ...Option) {
Serve(&http.Server{
Addr: addr,
Handler: h,
}, opts...)
}
// Serve serves the given `http.Server' while protecting it from abrupt
// shutdowns by capturing OS signals of `SIGINT' and `SIGTERM' and
// gracefully stopping the server given the duration configured.
//
func Serve(srv *http.Server, opts ...Option) {
cfg := defaultOpts
cfg.afterShutdown = []StopFunc{srv.Shutdown}
for _, o := range opts {
cfg = o(cfg)
}
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
go mustListenAndServe(srv, cfg)
<-ctx.Done()
stop()
cfg.log("Shutting down gracefully, press Ctrl+C again to force")
mustStopWithGrace(cfg)
}
func mustListenAndServe(srv *http.Server, o opts) {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
o.log("listen: %v", err)
os.Exit(1)
}
}
func mustStopWithGrace(o opts) {
ctx, cancel := context.WithTimeout(context.Background(), o.graceDuration)
defer cancel()
for _, s := range o.afterShutdown {
if err := s(ctx); err != nil {
o.log("failed to gracefully stop. Got: %v", err)
os.Exit(1)
}
}
}