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

Graceful shutdown of server #122

Merged
merged 1 commit into from
Jan 15, 2024
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
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
Loading