@@ -27,6 +27,7 @@ const defaultTimeout = 10 * time.Second
2727func 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 }()
0 commit comments