Skip to content

Commit

Permalink
make sure each server gets closeWait time to gracefully close
Browse files Browse the repository at this point in the history
  • Loading branch information
dropwhile committed Nov 8, 2023
1 parent 59a4227 commit cf3e33b
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions cmd/go-camo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,20 @@ func main() {
mlog.Info("Starting graceful shutdown")

closeWait := 200 * time.Millisecond
d := time.Now().Add(closeWait)
ctx, cancel := context.WithDeadline(context.Background(), d)

ctx, cancel := context.WithTimeout(context.Background(), closeWait)
// Even though ctx may be expired by then, it is good practice to call its
// cancellation function in any case. Failure to do so may keep the
// context and its parent alive longer than necessary.
defer cancel()
if httpSrv != nil {
if err := httpSrv.Shutdown(ctx); err != nil {
mlog.Info("Error gracefully shutting down HTTP server:", err)
}
}

ctx, cancel = context.WithTimeout(context.Background(), closeWait)
defer cancel()
if tlsSrv != nil {
if err := tlsSrv.Shutdown(ctx); err != nil {
mlog.Info("Error gracefully shutting down HTTP/TLS server:", err)
Expand All @@ -419,10 +424,6 @@ func main() {
mlog.Info("Error gracefully shutting down HTTP3/QUIC server:", err)
}
}
// Even though ctx may be expired, it is good practice to call its
// cancellation function in any case. Failure to do so may keep the
// context and its parent alive longer than necessary.
cancel()

close(idleConnsClosed)
}()
Expand Down

0 comments on commit cf3e33b

Please sign in to comment.