Implement graceful shutdown of client

Install handlers for SIGTERM and command line interrupt.
This commit is contained in:
Jan Dittberner 2022-12-03 13:35:56 +01:00
parent 9c608ed81f
commit 4345e5d899
4 changed files with 22 additions and 2 deletions

View file

@ -24,7 +24,9 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"os/signal"
"strings" "strings"
"syscall"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
@ -150,7 +152,21 @@ func startClient(configFile string, logger *logrus.Logger) error {
logger.Info("setup complete, starting client operation") 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) return fmt.Errorf("error in client: %w", err)
} }

2
go.mod
View file

@ -3,7 +3,7 @@ module git.cacert.org/cacert-gosignerclient
go 1.19 go 1.19
require ( 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/balacode/go-delta v0.1.0
github.com/shamaton/msgpackgen v0.3.0 github.com/shamaton/msgpackgen v0.3.0
github.com/sirupsen/logrus v1.9.0 github.com/sirupsen/logrus v1.9.0

2
go.sum
View file

@ -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-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 h1:yX3lhEoBQkUKu23xggAzAeYWuziCkRYktSjsAOfNGHY=
git.cacert.org/cacert-gosigner v0.0.0-20221203104439-bc81ab84cb4a/go.mod h1:OGIB5wLUhJiBhTzSXReOhGxuy7sT5VvyOyT8Ux8EGyw= 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 h1:pwz4CMn06P2bIaIfAx3GSabMPwJp/Ww4if+7SgPYa3I=
github.com/balacode/go-delta v0.1.0/go.mod h1:wLNrwTI3lHbPBvnLzqbHmA7HVVlm1u22XLvhbeA6t3o= github.com/balacode/go-delta v0.1.0/go.mod h1:wLNrwTI3lHbPBvnLzqbHmA7HVVlm1u22XLvhbeA6t3o=
github.com/balacode/zr v1.0.0/go.mod h1:pLeSAL3DhZ9L0JuiRkUtIX3mLOCtzBLnDhfmykbSmkE= github.com/balacode/zr v1.0.0/go.mod h1:pLeSAL3DhZ9L0JuiRkUtIX3mLOCtzBLnDhfmykbSmkE=

View file

@ -133,6 +133,8 @@ func (c *Client) Run(ctx context.Context) error {
}(c.framer) }(c.framer)
go func() { go func() {
defer wg.Done()
clientProtocol := protocol.NewClient(c.handler, c.commands, c.in, c.out, c.logger) clientProtocol := protocol.NewClient(c.handler, c.commands, c.in, c.out, c.logger)
err := clientProtocol.Handle(subCtx) err := clientProtocol.Handle(subCtx)