Skip to content

Commit b77a0ec

Browse files
committed
Allow for shorter graceful shutdown
The healthcheck_interval env-var allows a user to specify a shorter time before shutting down the HTTP server to new connections. This change has been tested upstream in the of-watchdog which wraps this template. The default behaviour is to continue to use the write_timeout for a safe shutdown duration. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent b29fec5 commit b77a0ec

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

template/golang-middleware/main.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const defaultTimeout = 10 * time.Second
2424
func main() {
2525
readTimeout := parseIntOrDurationValue(os.Getenv("read_timeout"), defaultTimeout)
2626
writeTimeout := parseIntOrDurationValue(os.Getenv("write_timeout"), defaultTimeout)
27+
healthInterval := parseIntOrDurationValue(os.Getenv("healthcheck_interval"), writeTimeout)
2728

2829
s := &http.Server{
2930
Addr: fmt.Sprintf(":%d", 8082),
@@ -34,7 +35,7 @@ func main() {
3435

3536
http.HandleFunc("/", function.Handle)
3637

37-
listenUntilShutdown(s, writeTimeout)
38+
listenUntilShutdown(s, healthInterval)
3839
}
3940

4041
func listenUntilShutdown(s *http.Server, shutdownTimeout time.Duration) {
@@ -45,17 +46,14 @@ func listenUntilShutdown(s *http.Server, shutdownTimeout time.Duration) {
4546

4647
<-sig
4748

48-
log.Printf("[entrypoint] SIGTERM received.. shutting down server in %s\n", shutdownTimeout.String())
49-
49+
log.Printf("[entrypoint] SIGTERM: no connections in: %s", shutdownTimeout.String())
5050
<-time.Tick(shutdownTimeout)
5151

5252
if err := s.Shutdown(context.Background()); err != nil {
5353
log.Printf("[entrypoint] Error in Shutdown: %v", err)
5454
}
5555

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

6058
close(idleConnsClosed)
6159
}()

0 commit comments

Comments
 (0)