File tree Expand file tree Collapse file tree 1 file changed +12
-3
lines changed
Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import (
1313 "net"
1414 "net/http"
1515 "os"
16+ "sync"
1617 "time"
1718)
1819
@@ -139,10 +140,18 @@ func (sc StartConfig) start(ctx stdContext.Context, h http.Handler) error {
139140 logger .Info ("http(s) server started" , "address" , listener .Addr ().String ())
140141 }
141142
143+ wg := sync.WaitGroup {}
144+ defer wg .Wait () // wait for graceful shutdown goroutine to finish
145+
146+ gCtx , cancel := stdContext .WithCancel (ctx ) // end graceful goroutine when Serve returns early
147+ defer cancel ()
148+
142149 if sc .GracefulTimeout >= 0 {
143- gCtx , cancel := stdContext .WithCancel (ctx ) // end goroutine when Serve returns early
144- defer cancel ()
145- go gracefulShutdown (gCtx , & sc , & server , logger )
150+ wg .Add (1 )
151+ go func () {
152+ defer wg .Done ()
153+ gracefulShutdown (gCtx , & sc , & server , logger )
154+ }()
146155 }
147156
148157 if err := server .Serve (listener ); err != nil && ! errors .Is (err , http .ErrServerClosed ) {
You can’t perform that action at this time.
0 commit comments