Skip to content

Commit f7c2916

Browse files
authored
Merge pull request #12 from practicalgo/fix_issue_11
Fix for issue #11
2 parents ed9b0cc + bb6ad59 commit f7c2916

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

chap7/graceful-shutdown/server.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ func shutDown(ctx context.Context, s *http.Server, waitForShutdownCompletion cha
3535
signal.Notify(sigch, syscall.SIGINT, syscall.SIGTERM)
3636
sig := <-sigch
3737
log.Printf("Got signal: %v . Server shutting down.", sig)
38-
if err := s.Shutdown(ctx); err != nil {
38+
39+
childCtx, cancel := context.WithTimeout(
40+
ctx, 30*time.Second,
41+
)
42+
defer cancel()
43+
44+
if err := s.Shutdown(childCtx); err != nil {
3945
log.Printf("Error during shutdown: %v", err)
4046
}
4147
waitForShutdownCompletion <- struct{}{}
@@ -48,11 +54,6 @@ func main() {
4854
}
4955

5056
waitForShutdownCompletion := make(chan struct{})
51-
ctx, cancel := context.WithTimeout(
52-
context.Background(), 30*time.Second,
53-
)
54-
defer cancel()
55-
5657
mux := http.NewServeMux()
5758
mux.HandleFunc("/api/users/", handleUserAPI)
5859

@@ -61,7 +62,7 @@ func main() {
6162
Handler: mux,
6263
}
6364

64-
go shutDown(ctx, &s, waitForShutdownCompletion)
65+
go shutDown(context.Background(), &s, waitForShutdownCompletion)
6566

6667
err := s.ListenAndServe()
6768
log.Print(

0 commit comments

Comments
 (0)