Skip to content

Commit

Permalink
Merge pull request #97 from mhrabovcin/cleanup-resources
Browse files Browse the repository at this point in the history
fix: add signal handling and resource cleanup
  • Loading branch information
mhrabovcin committed May 13, 2024
2 parents e232729 + fd3fa6f commit b033b9d
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package cmd

import (
"context"
"errors"
"fmt"
"net/http"
"os/signal"
"runtime"
"syscall"
"time"

"github.com/gorilla/handlers"

Expand Down Expand Up @@ -79,7 +83,8 @@ func runServe(bundlePath string, o *serveOptions, out output.Output) error {
return fmt.Errorf("failed to get bundle from path %q: %w", bundlePath, err)
}

ctx := context.Background()
ctx, done := signal.NotifyContext(context.Background(), syscall.SIGINT)
defer done()

out.StartOperation("Starting k8s server")
testEnv, err := startK8sServer(ctx, supportBundle, out, o)
Expand Down Expand Up @@ -113,8 +118,26 @@ func runServe(bundlePath string, o *serveOptions, out output.Output) error {
proxyHandler := proxy.New(testEnv.Config, supportBundle, rewriter.Default())
loggedProxyHandler := handlers.LoggingHandler(out.InfoWriter(), proxyHandler)

http.Handle("/", loggedProxyHandler)
return http.ListenAndServe(o.proxyAddress, nil) //nolint:gosec // not a production server
s := http.Server{
Addr: o.proxyAddress,
Handler: loggedProxyHandler,
ReadHeaderTimeout: time.Second * 5,
}
go func() {
<-ctx.Done()
out.Info("Shutting down troubleshoot-live...")
if err := s.Shutdown(ctx); err != nil {
out.Error(err, "failed to shutdown http server")
}
}()
return ignoreServerClosedError(s.ListenAndServe())
}

func ignoreServerClosedError(err error) error {
if errors.Is(err, http.ErrServerClosed) {
return nil
}
return err
}

func startK8sServer(
Expand Down

0 comments on commit b033b9d

Please sign in to comment.