Skip to content

Commit

Permalink
Merge pull request #122 from giggsoff/graceful-stop
Browse files Browse the repository at this point in the history
Graceful shutdown of server
  • Loading branch information
deitch authored Jan 15, 2024
2 parents 9f6fca2 + 4bf03a2 commit aad2368
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ package server

import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/fs"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/gorilla/mux"
Expand All @@ -38,6 +42,8 @@ type Server struct {

// Start start the server
func (s *Server) Start() {
irqSig := make(chan os.Signal, 1)
signal.Notify(irqSig, syscall.SIGINT, syscall.SIGTERM)
// ensure the server cert and key exist
_, err := os.Stat(s.CertPath)
if err != nil {
Expand Down Expand Up @@ -240,7 +246,28 @@ func (s *Server) Start() {
log.Printf("\tdatabase: %s\n", s.DeviceManager.Database())
log.Printf("\tserver cert: %s\n", s.CertPath)
log.Printf("\tserver key: %s\n", s.KeyPath)
log.Fatal(server.ListenAndServeTLS(s.CertPath, s.KeyPath))
done := make(chan bool)
go func() {
err := server.ListenAndServeTLS(s.CertPath, s.KeyPath)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("Listen and serve: %v", err)
}
done <- true
}()
<-irqSig
log.Printf("server shutdown starting")

//Create shutdown context with 10 second timeout
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

//shutdown the server
err = server.Shutdown(ctx)
if err != nil {
log.Printf("Shutdown request error: %v", err)
}
<-done
log.Printf("server shutdown done")
}

// middleware handlers to check device cert and onboarding cert
Expand Down

0 comments on commit aad2368

Please sign in to comment.