This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgraceful.go
237 lines (186 loc) · 5.03 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
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
/*
Deprecated: This package is no longer maintained.
Package graceful simplifies graceful shutdown of HTTP servers (Go 1.8+).
Installation
Just go get the package:
go get -u github.com/TV4/graceful
Usage
A small usage example
package main
import (
"context"
"log"
"net/http"
"os"
"time"
"github.com/TV4/graceful"
)
type server struct {
logger *log.Logger
}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
time.Sleep(5 * time.Second)
w.Write([]byte("Hello!"))
}
func (s *server) Shutdown(ctx context.Context) error {
time.Sleep(2 * time.Second)
s.logger.Println("Shutdown finished")
return nil
}
func main() {
graceful.LogListenAndServe(setup(":2017"))
}
func setup(addr string) (*http.Server, *log.Logger) {
s := &server{logger: log.New(os.Stdout, "", 0)}
return &http.Server{Addr: addr, Handler: s}, s.logger
}
*/
package graceful
import (
"context"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
// Server is implemented by *http.Server
type Server interface {
ListenAndServe() error
Shutdowner
}
// TLSServer is implemented by *http.Server
type TLSServer interface {
ListenAndServeTLS(string, string) error
Shutdowner
}
// Shutdowner is implemented by *http.Server, and optionally by *http.Server.Handler
type Shutdowner interface {
Shutdown(ctx context.Context) error
}
// Logger is implemented by *log.Logger
type Logger interface {
Printf(format string, v ...interface{})
Fatal(...interface{})
}
// logger is the logger used by the shutdown function
// (defaults to logging to ioutil.Discard)
var logger Logger = log.New(ioutil.Discard, "", 0)
// signals is the channel used to signal shutdown
var signals chan os.Signal
// Timeout for context used in call to *http.Server.Shutdown
var Timeout = 15 * time.Second
// Format strings used by the logger
var (
ListeningFormat = "Listening on http://%s\n"
ShutdownFormat = "\nServer shutdown with timeout: %s\n"
ErrorFormat = "Error: %v\n"
FinishedFormat = "Shutdown finished %ds before deadline\n"
FinishedHTTP = "Finished all in-flight HTTP requests\n"
HandlerShutdownFormat = "Shutting down handler with timeout: %ds\n"
)
// LogListenAndServe logs using the logger and then calls ListenAndServe
func LogListenAndServe(s Server, loggers ...Logger) {
if hs, ok := s.(*http.Server); ok {
logger = getLogger(loggers...)
if host, port, err := net.SplitHostPort(hs.Addr); err == nil {
if host == "" {
host = net.IPv4zero.String()
}
logger.Printf(ListeningFormat, net.JoinHostPort(host, port))
}
}
ListenAndServe(s)
}
// ListenAndServe starts the server in a goroutine and then calls Shutdown
func ListenAndServe(s Server) {
go func() {
if err := s.ListenAndServe(); err != http.ErrServerClosed {
logger.Fatal(err)
}
}()
Shutdown(s)
}
// ListenAndServeTLS starts the server in a goroutine and then calls Shutdown
func ListenAndServeTLS(s TLSServer, certFile, keyFile string) {
go func() {
if err := s.ListenAndServeTLS(certFile, keyFile); err != http.ErrServerClosed {
logger.Fatal(err)
}
}()
Shutdown(s)
}
// Shutdown blocks until os.Interrupt or syscall.SIGTERM received, then
// running *http.Server.Shutdown with a context having a timeout
func Shutdown(s Shutdowner) {
signals = make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM)
<-signals
shutdown(s, logger)
}
func shutdown(s Shutdowner, logger Logger) {
if s == nil {
return
}
if logger == nil {
logger = log.New(ioutil.Discard, "", 0)
}
ctx, cancel := context.WithTimeout(context.Background(), Timeout)
defer cancel()
logger.Printf(ShutdownFormat, Timeout)
// Stop keeping alive HTTP connections
if hs, ok := s.(interface {
SetKeepAlivesEnabled(bool)
}); ok {
hs.SetKeepAlivesEnabled(false)
}
if err := s.Shutdown(ctx); err != nil {
logger.Printf(ErrorFormat, err)
} else {
if hs, ok := s.(*http.Server); ok {
logger.Printf(FinishedHTTP)
if hss, ok := hs.Handler.(Shutdowner); ok {
select {
case <-ctx.Done():
if err := ctx.Err(); err != nil {
logger.Printf(ErrorFormat, err)
return
}
default:
if deadline, ok := ctx.Deadline(); ok {
secs := (time.Until(deadline) + time.Second/2) / time.Second
logger.Printf(HandlerShutdownFormat, secs)
}
done := make(chan error)
go func() {
<-ctx.Done()
done <- ctx.Err()
}()
go func() {
done <- hss.Shutdown(ctx)
}()
if err := <-done; err != nil {
logger.Printf(ErrorFormat, err)
return
}
}
}
}
if deadline, ok := ctx.Deadline(); ok {
secs := (time.Until(deadline) + time.Second/2) / time.Second
logger.Printf(FinishedFormat, secs)
}
}
}
func getLogger(loggers ...Logger) Logger {
if len(loggers) > 0 {
if loggers[0] != nil {
return loggers[0]
}
return log.New(ioutil.Discard, "", 0)
}
return log.New(os.Stdout, "", 0)
}