Skip to content

Commit a693532

Browse files
committed
Add a maximum timeout for shutting down
The shutdown operation now has a maximum duration to wait for active connections to complete. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent b77a0ec commit a693532

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

template/golang-http/main.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const defaultTimeout = 10 * time.Second
2727
func main() {
2828
readTimeout := parseIntOrDurationValue(os.Getenv("read_timeout"), defaultTimeout)
2929
writeTimeout := parseIntOrDurationValue(os.Getenv("write_timeout"), defaultTimeout)
30+
healthInterval := parseIntOrDurationValue(os.Getenv("healthcheck_interval"), writeTimeout)
3031

3132
s := &http.Server{
3233
Addr: fmt.Sprintf(":%d", 8082),
@@ -36,28 +37,28 @@ func main() {
3637
}
3738

3839
http.HandleFunc("/", makeRequestHandler())
39-
listenUntilShutdown(s, writeTimeout)
40+
listenUntilShutdown(s, healthInterval, writeTimeout)
4041
}
4142

42-
func listenUntilShutdown(s *http.Server, shutdownTimeout time.Duration) {
43+
func listenUntilShutdown(s *http.Server, shutdownTimeout time.Duration, writeTimeout time.Duration) {
4344
idleConnsClosed := make(chan struct{})
4445
go func() {
4546
sig := make(chan os.Signal, 1)
4647
signal.Notify(sig, syscall.SIGTERM)
4748

4849
<-sig
4950

50-
log.Printf("[entrypoint] SIGTERM received.. shutting down server in %s\n", shutdownTimeout.String())
51-
51+
log.Printf("[entrypoint] SIGTERM: no connections in: %s", shutdownTimeout.String())
5252
<-time.Tick(shutdownTimeout)
5353

54-
if err := s.Shutdown(context.Background()); err != nil {
54+
ctx, cancel := context.WithTimeout(context.Background(), writeTimeout)
55+
defer cancel()
56+
57+
if err := s.Shutdown(ctx); err != nil {
5558
log.Printf("[entrypoint] Error in Shutdown: %v", err)
5659
}
5760

58-
log.Printf("[entrypoint] No new connections allowed. Exiting in: %s\n", shutdownTimeout.String())
59-
60-
<-time.Tick(shutdownTimeout)
61+
log.Printf("[entrypoint] Exiting.")
6162

6263
close(idleConnsClosed)
6364
}()

template/golang-middleware/main.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ func main() {
3535

3636
http.HandleFunc("/", function.Handle)
3737

38-
listenUntilShutdown(s, healthInterval)
38+
listenUntilShutdown(s, healthInterval, writeTimeout)
3939
}
4040

41-
func listenUntilShutdown(s *http.Server, shutdownTimeout time.Duration) {
41+
func listenUntilShutdown(s *http.Server, shutdownTimeout time.Duration, writeTimeout time.Duration) {
4242
idleConnsClosed := make(chan struct{})
4343
go func() {
4444
sig := make(chan os.Signal, 1)
@@ -49,11 +49,14 @@ func listenUntilShutdown(s *http.Server, shutdownTimeout time.Duration) {
4949
log.Printf("[entrypoint] SIGTERM: no connections in: %s", shutdownTimeout.String())
5050
<-time.Tick(shutdownTimeout)
5151

52-
if err := s.Shutdown(context.Background()); err != nil {
52+
ctx, cancel := context.WithTimeout(context.Background(), writeTimeout)
53+
defer cancel()
54+
55+
if err := s.Shutdown(ctx); err != nil {
5356
log.Printf("[entrypoint] Error in Shutdown: %v", err)
5457
}
5558

56-
log.Printf("[entrypoint] Exiting")
59+
log.Printf("[entrypoint] Exiting.")
5760

5861
close(idleConnsClosed)
5962
}()

0 commit comments

Comments
 (0)