Skip to content

Commit

Permalink
Stop server gracefully with timeout
Browse files Browse the repository at this point in the history
Signed-off-by: Quentin Guidée <[email protected]>
  • Loading branch information
quentinguidee committed Sep 13, 2023
1 parent f0e7e8d commit 80cbea4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 9 additions & 1 deletion router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/signal"
"path"
"time"

"github.com/gin-contrib/cors"
"github.com/gin-contrib/sse"
Expand Down Expand Up @@ -101,7 +102,10 @@ func (r *Router) Stop() {

instanceService.StopAll()

err := r.server.Shutdown(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

err := r.server.Shutdown(ctx)
if err != nil {
log.Error(err)
return
Expand All @@ -123,6 +127,10 @@ func (r *Router) handleSignals() {
<-c
log.Info("shutdown signal sent")
r.Stop()
err := proxyService.Stop()
if err != nil {
log.Error(err)
}
os.Exit(0)
}()
}
Expand Down
6 changes: 5 additions & 1 deletion services/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"time"

"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -60,7 +61,10 @@ func (s *ProxyService) Start() error {
}

func (s *ProxyService) Stop() error {
err := s.server.Shutdown(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

err := s.server.Shutdown(ctx)
if err != nil {
return err
}
Expand Down

0 comments on commit 80cbea4

Please sign in to comment.