Skip to content

Commit

Permalink
fix: improve shutdown (#1)
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 authored Sep 17, 2020
1 parent e8741d8 commit e870642
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import (
"io"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"

"github.com/caarlos0/httperr"
"gocloud.dev/blob"
Expand Down Expand Up @@ -59,8 +63,30 @@ func main() {
return nil
}))

done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

srv := &http.Server{
Addr: listen,
Handler: handler,
}

go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
log.Println("listening on", listen)
http.ListenAndServe(listen, handler)

<-done
log.Println("stopping")

ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()

if err := srv.Shutdown(ctx); err != nil {
log.Fatalf("couldn't stop server: %+v", err)
}
}

type stringSlice []string
Expand Down

0 comments on commit e870642

Please sign in to comment.