From dd48666357a94979ff50a0570b02ca8b1f3c2566 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 13 Oct 2020 18:40:35 +0300 Subject: [PATCH] [#72] Shutdown inner ring app if RPC node has been terminated Adopt error channel from Listener interface. Signed-off-by: Alex Vanin --- cmd/neofs-ir/main.go | 12 +++++++++--- pkg/innerring/innerring.go | 21 ++++++++++++++++++--- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/cmd/neofs-ir/main.go b/cmd/neofs-ir/main.go index 2f634f5b..32c1d52b 100644 --- a/cmd/neofs-ir/main.go +++ b/cmd/neofs-ir/main.go @@ -10,6 +10,7 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/util/grace" "github.com/nspcc-dev/neofs-node/pkg/util/logger" "github.com/nspcc-dev/neofs-node/pkg/util/profiler" + "go.uber.org/zap" ) const ( @@ -44,6 +45,7 @@ func main() { exitErr(err) ctx := grace.NewGracefulContext(log) + intErr := make(chan error) // internal inner ring errors pprof := profiler.NewProfiler(log, cfg) prometheus := profiler.NewMetrics(log, cfg) @@ -66,15 +68,19 @@ func main() { } // start inner ring - err = innerRing.Start(ctx) + err = innerRing.Start(ctx, intErr) if err != nil { exitErr(err) } log.Info("application started") - // todo: select ctx.Done or exported error channel - <-ctx.Done() + select { + case <-ctx.Done(): + case err := <-intErr: + // todo: restart application instead of shutdown + log.Info("internal error", zap.String("msg", err.Error())) + } innerRing.Stop() diff --git a/pkg/innerring/innerring.go b/pkg/innerring/innerring.go index c9558a58..8f725f21 100644 --- a/pkg/innerring/innerring.go +++ b/pkg/innerring/innerring.go @@ -75,7 +75,7 @@ const ( ) // Start runs all event providers. -func (s *Server) Start(ctx context.Context) error { +func (s *Server) Start(ctx context.Context, intError chan<- error) error { err := s.initConfigFromBlockchain() if err != nil { return err @@ -83,8 +83,23 @@ func (s *Server) Start(ctx context.Context) error { s.localTimers.Start(ctx) // local timers start ticking - go s.morphListener.Listen(ctx) // listen for neo:morph events - go s.mainnetListener.Listen(ctx) // listen for neo:mainnet events + morphErr := make(chan error) + mainnnetErr := make(chan error) + + // anonymous function to multiplex error channels + go func() { + select { + case <-ctx.Done(): + return + case err := <-morphErr: + intError <- errors.Wrap(err, "sidechain") + case err := <-mainnnetErr: + intError <- errors.Wrap(err, "mainnet") + } + }() + + go s.morphListener.ListenWithError(ctx, morphErr) // listen for neo:morph events + go s.mainnetListener.ListenWithError(ctx, mainnnetErr) // listen for neo:mainnet events return nil }