plugin/health : rework overloaded goroutine to support graceful shutdown (#5244)

Signed-off-by: Ondřej Benkovský <ondrej.benkovsky@jamf.com>
This commit is contained in:
Ondřej Benkovský
2022-04-13 19:09:03 +02:00
committed by GitHub
parent 068af64b19
commit a929b0b1ec
5 changed files with 63 additions and 12 deletions

View File

@@ -2,6 +2,7 @@
package health
import (
"context"
"io"
"net"
"net/http"
@@ -22,14 +23,13 @@ type health struct {
nlSetup bool
mux *http.ServeMux
stop chan bool
stop context.CancelFunc
}
func (h *health) OnStartup() error {
if h.Addr == "" {
h.Addr = ":8080"
}
h.stop = make(chan bool)
ln, err := reuseport.Listen("tcp", h.Addr)
if err != nil {
return err
@@ -45,8 +45,11 @@ func (h *health) OnStartup() error {
io.WriteString(w, http.StatusText(http.StatusOK))
})
ctx := context.Background()
ctx, h.stop = context.WithCancel(ctx)
go func() { http.Serve(h.ln, h.mux) }()
go func() { h.overloaded() }()
go func() { h.overloaded(ctx) }()
return nil
}
@@ -61,9 +64,9 @@ func (h *health) OnFinalShutdown() error {
time.Sleep(h.lameduck)
}
h.ln.Close()
h.stop()
h.ln.Close()
h.nlSetup = false
close(h.stop)
return nil
}