Skip to content

Commit b45a15e

Browse files
committed
Rework shutdown/signal handling of http output
1 parent 4957ff0 commit b45a15e

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

main.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,8 @@ func main() {
7272
}
7373

7474
if *addr != "" {
75-
stp := make(chan os.Signal, 1)
76-
signal.Notify(stp, os.Interrupt, syscall.SIGTERM)
77-
ctx, ctxCancel := context.WithCancel(context.Background())
78-
defer ctxCancel()
75+
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
76+
defer cancel()
7977

8078
p := prometheus.NewPedanticRegistry()
8179
p.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
@@ -97,9 +95,14 @@ func main() {
9795
log.Fatalln(err.Error())
9896
}
9997
}()
100-
10198
log.Println("exporter listening on:", listener.Addr().String())
102-
<-stp
103-
h.Shutdown(ctx)
99+
100+
<-ctx.Done()
101+
102+
t, tc := context.WithTimeout(context.Background(), 5*time.Second)
103+
defer tc()
104+
h.Shutdown(t)
105+
106+
log.Println("exporter shutdown completed")
104107
}
105108
}

0 commit comments

Comments
 (0)