Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stop healthz http server cleanly #1535

Merged
merged 1 commit into from
Feb 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,7 @@ func main() {
}()

if opts.healthzPort > 0 {
// It's not super easy to shutdown the HTTP server so don't attempt to stop it cleanly
go mustRunHealthz()
mustRunHealthz(ctx.Done(), &wg)
}

// Fetch the network config (i.e. what backend to use etc..).
Expand Down Expand Up @@ -782,7 +781,7 @@ func WriteSubnetFile(path string, config *subnet.Config, ipMasq bool, bn backend
//TODO - is this safe? What if it's not on the same FS?
}

func mustRunHealthz() {
func mustRunHealthz(stopChan <-chan struct{}, wg *sync.WaitGroup) {
address := net.JoinHostPort(opts.healthzIP, strconv.Itoa(opts.healthzPort))
log.Infof("Start healthz server on %s", address)

Expand All @@ -791,10 +790,29 @@ func mustRunHealthz() {
w.Write([]byte("flanneld is running"))
})

if err := http.ListenAndServe(address, nil); err != nil {
log.Errorf("Start healthz server error. %v", err)
panic(err)
}
server := &http.Server{Addr: address}

wg.Add(2)
go func() {
// when Shutdown is called, ListenAndServe immediately return ErrServerClosed.
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why && !errors.Is(err, http.ErrServerClosed)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will call server.Shutdown() to shutdown healthz http server when global context is done, and when server.Shutdown() is called, the first goroutine function server.ListenAndServe() immediately return http.ErrServerClosed, so it means we will get http.ErrServerClosed when healthz http server close normally, and close normally should not panic.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

log.Errorf("Start healthz server error. %v", err)
panic(err)
}
wg.Done()
}()

go func() {
// wait to stop
<-stopChan

// create new context with timeout for http server to shutdown gracefully
ctx, _ := context.WithTimeout(context.Background(), 3*time.Second)
if err := server.Shutdown(ctx); err != nil {
log.Errorf("Shutdown healthz server error. %v", err)
}
wg.Done()
}()
}

func ReadCIDRFromSubnetFile(path string, CIDRKey string) ip.IP4Net {
Expand Down