[#323] cmd/node: Compose functions for closing components in app

Closing callback can be registered in app through onShutdown method.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-01-18 11:56:14 +03:00 committed by Alex Vanin
parent 707434efa9
commit 9f41192bff
4 changed files with 29 additions and 4 deletions

View file

@ -187,6 +187,8 @@ type cfg struct {
netStatus *atomic.Int32 netStatus *atomic.Int32
healthStatus *atomic.Int32 healthStatus *atomic.Int32
closers []func()
} }
type cfgGRPC struct { type cfgGRPC struct {

View file

@ -62,6 +62,10 @@ func initControlService(c *cfg) {
c.cfgControlService.server = grpc.NewServer() c.cfgControlService.server = grpc.NewServer()
} }
c.onShutdown(func() {
stopGRPC("NeoFS Control API", c.cfgControlService.server, c.log)
})
control.RegisterControlServiceServer(c.cfgControlService.server, ctlSvc) control.RegisterControlServiceServer(c.cfgControlService.server, ctlSvc)
c.workers = append(c.workers, newWorkerFromFunc(func(ctx context.Context) { c.workers = append(c.workers, newWorkerFromFunc(func(ctx context.Context) {

View file

@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"net" "net"
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
"go.uber.org/zap"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/reflection" "google.golang.org/grpc/reflection"
) )
@ -17,6 +19,10 @@ func initGRPC(c *cfg) {
c.cfgGRPC.server = grpc.NewServer( c.cfgGRPC.server = grpc.NewServer(
grpc.MaxSendMsgSize(c.viper.GetInt(cfgMaxMsgSize)), grpc.MaxSendMsgSize(c.viper.GetInt(cfgMaxMsgSize)),
) )
c.onShutdown(func() {
stopGRPC("NeoFS Public API", c.cfgGRPC.server, c.log)
})
} }
func serveGRPC(c *cfg) { func serveGRPC(c *cfg) {
@ -38,3 +44,13 @@ func serveGRPC(c *cfg) {
} }
}() }()
} }
func stopGRPC(name string, s *grpc.Server, l *logger.Logger) {
l = l.With(zap.String("name", name))
l.Info("stopping gRPC server...")
s.GracefulStop()
l.Info("gRPC server stopped successfully")
}

View file

@ -78,12 +78,15 @@ func wait(c *cfg) {
} }
func shutdown(c *cfg) { func shutdown(c *cfg) {
c.cfgGRPC.server.GracefulStop() for _, closer := range c.closers {
c.cfgControlService.server.GracefulStop() closer()
}
c.log.Info("gRPC server stopped")
goOffline(c) goOffline(c)
c.wg.Wait() c.wg.Wait()
} }
func (c *cfg) onShutdown(f func()) {
c.closers = append(c.closers, f)
}