diff options
author | Florian Klink <flokli@flokli.de> | 2023-09-18T13·51+0300 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2023-09-18T14·34+0000 |
commit | 6c586bc2a7a49755b4b2566c034bc1affc92011d (patch) | |
tree | 826be0c654fa0430b8abed47cb154a2d0bfc4749 /tvix/nar-bridge/cmd/nar_bridge/main.go | |
parent | 02aed32bf2627a969c15f358737abf5acb697dd4 (diff) |
feat(tvix/nar-bridge): graceful shutdown r/6616
This gives existing clients 30s to finish their requests after receiving an interrupt. Change-Id: Ia9b0e662fd1ffbbb6c2d03f3dd6548b13cf3d241 Reviewed-on: https://cl.tvl.fyi/c/depot/+/9365 Autosubmit: flokli <flokli@flokli.de> Tested-by: BuildkiteCI Reviewed-by: Connor Brewster <cbrewster@hey.com>
Diffstat (limited to 'tvix/nar-bridge/cmd/nar_bridge/main.go')
-rw-r--r-- | tvix/nar-bridge/cmd/nar_bridge/main.go | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/tvix/nar-bridge/cmd/nar_bridge/main.go b/tvix/nar-bridge/cmd/nar_bridge/main.go index 482012f39b5f..a1732433c050 100644 --- a/tvix/nar-bridge/cmd/nar_bridge/main.go +++ b/tvix/nar-bridge/cmd/nar_bridge/main.go @@ -1,8 +1,10 @@ package main import ( + "context" "os" "os/signal" + "time" "github.com/alecthomas/kong" @@ -33,25 +35,17 @@ func main() { } logrus.SetLevel(logLevel) - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt) - - go func() { - for range c { - log.Info("Received Signal, shutting down…") - os.Exit(1) - } - }() + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) + defer stop() // connect to tvix-store log.Debugf("Dialing to %v", cli.StoreAddr) - conn, err := grpc.Dial(cli.StoreAddr, grpc.WithTransportCredentials(insecure.NewCredentials())) + conn, err := grpc.DialContext(ctx, cli.StoreAddr, grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { log.Fatalf("did not connect: %v", err) } defer conn.Close() - log.Printf("Starting nar-bridge at %v", cli.ListenAddr) s := server.New( storev1pb.NewDirectoryServiceClient(conn), storev1pb.NewBlobServiceClient(conn), @@ -60,9 +54,21 @@ func main() { 30, ) - err = s.ListenAndServe(cli.ListenAddr) - if err != nil { - log.Error("Server failed: %w", err) + log.Printf("Starting nar-bridge at %v", cli.ListenAddr) + go s.ListenAndServe(cli.ListenAddr) + + // listen for the interrupt signal. + <-ctx.Done() + + // Restore default behaviour on the interrupt signal + stop() + log.Info("Received Signal, shutting down, press Ctl+C again to force.") + + timeoutCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + + if s.Shutdown(timeoutCtx); err != nil { + log.WithError(err).Warn("failed to shutdown") os.Exit(1) } } |