From 8110dbb967ce65880a0a80a5b8bc78030ea7250c Mon Sep 17 00:00:00 2001 From: Matty Evans Date: Thu, 5 Dec 2024 16:36:48 +1000 Subject: [PATCH] feat: Improve server shutdown sequence and add documentation --- pkg/server/server.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/server/server.go b/pkg/server/server.go index 1d944577..c3b63b78 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -213,7 +213,11 @@ func (x *Xatu) Start(ctx context.Context) error { } func (x *Xatu) stop(ctx context.Context) error { - x.log.Info("Beginning server shutdown sequence") + x.log.WithFields(logrus.Fields{ + "pre_stop_sleep_seconds": x.config.PreStopSleepSeconds, + }).Info("Beginning server shutdown sequence") + + time.Sleep(time.Duration(x.config.PreStopSleepSeconds) * time.Second) ctx, cancel := context.WithTimeout(ctx, 20*time.Second) defer cancel() @@ -386,6 +390,9 @@ func (x *Xatu) startGrpcServer(ctx context.Context) error { x.log.WithField("addr", x.config.Addr).Info("Starting gRPC server") + // Upon shutdown, the listener will be closed by (grpcServer).GracefulStop(). This + // will trigger a net.ErrClosed error (normal behavior), and we can return nil. + // Think of net.ErrClosed as 'listener was closed intentionally'. err = x.grpcServer.Serve(lis) if err != nil && !errors.Is(err, net.ErrClosed) { return err