Improve shutdown sequence
This commit is contained in:
parent
62de443d3b
commit
f1867c1010
15
main.go
15
main.go
|
@ -191,12 +191,8 @@ func main() {
|
||||||
server := &http.Server{Addr: listenAddr}
|
server := &http.Server{Addr: listenAddr}
|
||||||
|
|
||||||
// Run the producer
|
// Run the producer
|
||||||
go func() {
|
errs := make(chan error, 1)
|
||||||
if err := server.ListenAndServe(); err != nil &&
|
go func() { errs <- server.ListenAndServe() }()
|
||||||
err != http.ErrServerClosed {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Run the consumer
|
// Run the consumer
|
||||||
ctx, ctxCancel := context.WithCancel(context.Background())
|
ctx, ctxCancel := context.WithCancel(context.Background())
|
||||||
|
@ -217,7 +213,12 @@ func main() {
|
||||||
// Wait for a termination signal
|
// Wait for a termination signal
|
||||||
sigs := make(chan os.Signal, 1)
|
sigs := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||||
<-sigs
|
|
||||||
|
select {
|
||||||
|
case <-sigs:
|
||||||
|
case err := <-errs:
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
// Stop the producer gracefully
|
// Stop the producer gracefully
|
||||||
ctxSd, ctxSdCancel := context.WithTimeout(context.Background(), 5)
|
ctxSd, ctxSdCancel := context.WithTimeout(context.Background(), 5)
|
||||||
|
|
Loading…
Reference in New Issue