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

Add graceful shutdown to ocsp server for docker. #1272

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
41 changes: 40 additions & 1 deletion cli/ocspserve/ocspserve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
package ocspserve

import (
"context"
"errors"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"time"

"github.com/cloudflare/cfssl/cli"
"github.com/cloudflare/cfssl/log"
Expand Down Expand Up @@ -56,7 +60,42 @@ func ocspServerMain(args []string, c cli.Config) error {

addr := net.JoinHostPort(c.Address, strconv.Itoa(c.Port))
log.Info("Now listening on ", addr)
return http.ListenAndServe(addr, nil)

server := &http.Server{Addr: addr, Handler: nil}

//gracefull shutdown on SIGTERM or SIGINT
//see issue https://github.com/golang/go/issues/19541
//see https://play.golang.org/p/LdXUYyzDxY
exit := make(chan struct{})
quit := make(chan os.Signal, 1) //use buffered version due to vet false positive . Could be unbuffered in [email protected]
signal.Notify(quit, os.Interrupt)

go func() {
defer func() {
if err := recover(); err != nil {
log.Infof("recovered: %+v\n", err)
}
}()
<-quit
d := time.Now().Add(60 * time.Second) // deadline 5s max
ctx, cancel := context.WithDeadline(context.Background(), d)

defer cancel()

log.Info("Shutting down server...")
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("could not shutdown: %v", err)
}
close(exit)
}()
err := server.ListenAndServe()
<-exit

if err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}

return err
}

// Command assembles the definition of Command 'ocspserve'
Expand Down