From 4345e5d899f0630b3caf68dffaa46c8b02cccc04 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Sat, 3 Dec 2022 13:35:56 +0100 Subject: [PATCH] Implement graceful shutdown of client Install handlers for SIGTERM and command line interrupt. --- cmd/signerclient/main.go | 18 +++++++++++++++++- go.mod | 2 +- go.sum | 2 ++ internal/client/client.go | 2 ++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/cmd/signerclient/main.go b/cmd/signerclient/main.go index 4358c56..169a56f 100644 --- a/cmd/signerclient/main.go +++ b/cmd/signerclient/main.go @@ -24,7 +24,9 @@ import ( "fmt" "io" "os" + "os/signal" "strings" + "syscall" "github.com/sirupsen/logrus" "gopkg.in/yaml.v3" @@ -150,7 +152,21 @@ func startClient(configFile string, logger *logrus.Logger) error { logger.Info("setup complete, starting client operation") - if err = signerClient.Run(context.Background()); err != nil { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + + go func() { + <-c + + logger.Info("received shutdown signal") + + cancel() + }() + + if err = signerClient.Run(ctx); err != nil { return fmt.Errorf("error in client: %w", err) } diff --git a/go.mod b/go.mod index 052ca2c..640f1a1 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.cacert.org/cacert-gosignerclient go 1.19 require ( - git.cacert.org/cacert-gosigner v0.0.0-20221203104439-bc81ab84cb4a + git.cacert.org/cacert-gosigner v0.0.0-20221203123337-46407b368528 github.com/balacode/go-delta v0.1.0 github.com/shamaton/msgpackgen v0.3.0 github.com/sirupsen/logrus v1.9.0 diff --git a/go.sum b/go.sum index 6eabf0b..cf3ee8e 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ git.cacert.org/cacert-gosigner v0.0.0-20221202173159-afe7d23c9b6f h1:VcIwyogvdmY git.cacert.org/cacert-gosigner v0.0.0-20221202173159-afe7d23c9b6f/go.mod h1:OGIB5wLUhJiBhTzSXReOhGxuy7sT5VvyOyT8Ux8EGyw= git.cacert.org/cacert-gosigner v0.0.0-20221203104439-bc81ab84cb4a h1:yX3lhEoBQkUKu23xggAzAeYWuziCkRYktSjsAOfNGHY= git.cacert.org/cacert-gosigner v0.0.0-20221203104439-bc81ab84cb4a/go.mod h1:OGIB5wLUhJiBhTzSXReOhGxuy7sT5VvyOyT8Ux8EGyw= +git.cacert.org/cacert-gosigner v0.0.0-20221203123337-46407b368528 h1:W1K/YiNp8ganr2GuOpoNC+ZcaVsG15tczpNuvAjHn+U= +git.cacert.org/cacert-gosigner v0.0.0-20221203123337-46407b368528/go.mod h1:OGIB5wLUhJiBhTzSXReOhGxuy7sT5VvyOyT8Ux8EGyw= github.com/balacode/go-delta v0.1.0 h1:pwz4CMn06P2bIaIfAx3GSabMPwJp/Ww4if+7SgPYa3I= github.com/balacode/go-delta v0.1.0/go.mod h1:wLNrwTI3lHbPBvnLzqbHmA7HVVlm1u22XLvhbeA6t3o= github.com/balacode/zr v1.0.0/go.mod h1:pLeSAL3DhZ9L0JuiRkUtIX3mLOCtzBLnDhfmykbSmkE= diff --git a/internal/client/client.go b/internal/client/client.go index dabdd11..92e7d48 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -133,6 +133,8 @@ func (c *Client) Run(ctx context.Context) error { }(c.framer) go func() { + defer wg.Done() + clientProtocol := protocol.NewClient(c.handler, c.commands, c.in, c.out, c.logger) err := clientProtocol.Handle(subCtx)