Skip to content

Commit

Permalink
fix: Proper graceful http shutdown
Browse files Browse the repository at this point in the history
We have to wait for shutdown to return and not for
listenandserve to return.
  • Loading branch information
ananthb committed Jun 17, 2024
1 parent d157279 commit 5b49eb5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
35 changes: 18 additions & 17 deletions cmd/bf/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import (
)

const (
defaultCaHost = "localhost"
defaultCaPort = 8008
defaultCaHost = "localhost"
defaultCaPort = 8008
serverShutdownTimeout = 1 * time.Second
)

// caServeCmd flags
Expand Down Expand Up @@ -124,26 +125,26 @@ var caServeCmd = &cli.Command{

server := http.Server{Addr: addr, Handler: hdlr}

ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
defer cancel()

go func() {
<-ctx.Done()

const serverShutdownTimeout = 1 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), serverShutdownTimeout)
defer cancel()
slog.DebugContext(ctx, "shutting down server")
if err := server.Shutdown(ctx); err != nil {
slog.Error("error shutting down server", "error", err)
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.ErrorContext(ctx, "error starting server", "error", err)
os.Exit(1)
}
slog.InfoContext(ctx, "server shut down")
}()

if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.ErrorContext(ctx, "error starting server", "error", err)
return cli.Exit("Error starting server", 1)
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
defer cancel()

<-ctx.Done()

ctx, cancel = context.WithTimeout(context.Background(), serverShutdownTimeout)
defer cancel()

slog.DebugContext(ctx, "shutting down server")
if err := server.Shutdown(ctx); err != nil {
return err
}
slog.InfoContext(ctx, "server shut down")

return nil
},
Expand Down
34 changes: 21 additions & 13 deletions cmd/bf/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net/http/httputil"
"net/url"
"os"
"os/signal"
"time"

"github.com/RealImage/bifrost"
Expand Down Expand Up @@ -142,26 +143,33 @@ var proxyCmd = &cli.Command{
},
}

go func() {
<-ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
panic(err)
}
slog.InfoContext(ctx, "shutting down server")
}()

slog.InfoContext(ctx, "proxying requests",
"from", "https://"+addr,
"to", backendUrl,
"namespace", caCert.Namespace.String(),
)

if err := server.ListenAndServeTLS("", ""); err != nil &&
!errors.Is(err, http.ErrServerClosed) {
return cli.Exit(fmt.Sprintf("Error starting server: %s", err), 1)
go func() {
if err := server.ListenAndServeTLS("", ""); err != nil &&
!errors.Is(err, http.ErrServerClosed) {
slog.ErrorContext(ctx, "error starting server", "error", err)
os.Exit(1)
}
}()

ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
defer cancel()

<-ctx.Done()

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

if err := server.Shutdown(ctx); err != nil {
return err
}
slog.InfoContext(ctx, "shut down server")

return nil
},
}
Expand Down

0 comments on commit 5b49eb5

Please sign in to comment.