@@ -27,6 +27,7 @@ const defaultTimeout = 10 * time.Second
27
27
func main () {
28
28
readTimeout := parseIntOrDurationValue (os .Getenv ("read_timeout" ), defaultTimeout )
29
29
writeTimeout := parseIntOrDurationValue (os .Getenv ("write_timeout" ), defaultTimeout )
30
+ healthInterval := parseIntOrDurationValue (os .Getenv ("healthcheck_interval" ), writeTimeout )
30
31
31
32
s := & http.Server {
32
33
Addr : fmt .Sprintf (":%d" , 8082 ),
@@ -36,28 +37,28 @@ func main() {
36
37
}
37
38
38
39
http .HandleFunc ("/" , makeRequestHandler ())
39
- listenUntilShutdown (s , writeTimeout )
40
+ listenUntilShutdown (s , healthInterval , writeTimeout )
40
41
}
41
42
42
- func listenUntilShutdown (s * http.Server , shutdownTimeout time.Duration ) {
43
+ func listenUntilShutdown (s * http.Server , shutdownTimeout time.Duration , writeTimeout time. Duration ) {
43
44
idleConnsClosed := make (chan struct {})
44
45
go func () {
45
46
sig := make (chan os.Signal , 1 )
46
47
signal .Notify (sig , syscall .SIGTERM )
47
48
48
49
<- sig
49
50
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 ())
52
52
<- time .Tick (shutdownTimeout )
53
53
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 {
55
58
log .Printf ("[entrypoint] Error in Shutdown: %v" , err )
56
59
}
57
60
58
- log .Printf ("[entrypoint] No new connections allowed. Exiting in: %s\n " , shutdownTimeout .String ())
59
-
60
- <- time .Tick (shutdownTimeout )
61
+ log .Printf ("[entrypoint] Exiting." )
61
62
62
63
close (idleConnsClosed )
63
64
}()
0 commit comments