Skip to content

Commit

Permalink
feat(http): implement graceful shutdown (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
nuxencs authored Mar 2, 2024
1 parent ab3b770 commit 87a73c8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
22 changes: 19 additions & 3 deletions cmd/seasonpackarr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"context"
"encoding/json"
"fmt"
netHTTP "net/http"
Expand Down Expand Up @@ -125,16 +126,31 @@ func main() {

errorChannel := make(chan error)
go func() {
errorChannel <- srv.Open()
err := srv.Open()
if err != nil {
if !errors.Is(err, http.ErrServerClosed) {
errorChannel <- err
}
}
}()

sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)

for sig := range sigCh {
log.Info().Msgf("received signal: %v, shutting down server.", sig)
select {
case sig := <-sigCh:
log.Info().Msgf("received signal: %q, shutting down server.", sig.String())
os.Exit(0)

case err := <-errorChannel:
log.Error().Err(err).Msg("unexpected error from server")
}
if err := srv.Shutdown(context.Background()); err != nil {
log.Error().Err(err).Msg("error during http shutdown")
os.Exit(1)
}

os.Exit(0)

default:
pflag.Usage()
Expand Down
2 changes: 1 addition & 1 deletion internal/http/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package http

import "net/http"

func (s Server) isAuthenticated(next http.Handler) http.Handler {
func (s *Server) isAuthenticated(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// allow access if apiToken value is set to an empty string
if s.cfg.Config.APIToken == "" {
Expand Down
20 changes: 15 additions & 5 deletions internal/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package http

import (
"context"
"fmt"
"net"
"net/http"
Expand All @@ -17,9 +18,13 @@ import (
"github.com/go-chi/chi/v5/middleware"
)

var ErrServerClosed = http.ErrServerClosed

type Server struct {
log logger.Logger
cfg *config.AppConfig

httpServer http.Server
}

func NewServer(log logger.Logger, config *config.AppConfig) *Server {
Expand All @@ -29,7 +34,7 @@ func NewServer(log logger.Logger, config *config.AppConfig) *Server {
}
}

func (s Server) Open() error {
func (s *Server) Open() error {
addr := fmt.Sprintf("%v:%v", s.cfg.Config.Host, s.cfg.Config.Port)

var err error
Expand All @@ -44,23 +49,28 @@ func (s Server) Open() error {
return err
}

func (s Server) tryToServe(addr, proto string) error {
func (s *Server) tryToServe(addr, proto string) error {
listener, err := net.Listen(proto, addr)
if err != nil {
return err
}

s.log.Info().Msgf("Starting %s server. Listening on %s", proto, listener.Addr().String())

server := http.Server{
s.httpServer = http.Server{
Handler: s.Handler(),
ReadHeaderTimeout: time.Second * 15,
}

return server.Serve(listener)
return s.httpServer.Serve(listener)
}

func (s *Server) Shutdown(ctx context.Context) error {
s.log.Info().Msgf("shutting down http server gracefully...")
return s.httpServer.Shutdown(ctx)
}

func (s Server) Handler() http.Handler {
func (s *Server) Handler() http.Handler {
r := chi.NewRouter()

r.Use(middleware.RequestID)
Expand Down

0 comments on commit 87a73c8

Please sign in to comment.