From 35d598f564c6f258aaa26a9ec0c9123aa6dc4dee Mon Sep 17 00:00:00 2001 From: Eduard S Date: Wed, 23 Dec 2020 17:33:41 +0100 Subject: [PATCH] Don't log errors when context done --- node/node.go | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/node/node.go b/node/node.go index 9e38083..d75757b 100644 --- a/node/node.go +++ b/node/node.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/http" - "strings" "sync" "time" @@ -409,22 +408,17 @@ func (n *Node) handleReorg(stats *synchronizer.Stats) { // TODO(Edu): Consider keeping the `lastBlock` inside synchronizer so that we // don't have to pass it around. -func (n *Node) syncLoopFn(lastBlock *common.Block) (*common.Block, time.Duration) { - blockData, discarded, err := n.sync.Sync2(n.ctx, lastBlock) +func (n *Node) syncLoopFn(ctx context.Context, lastBlock *common.Block) (*common.Block, time.Duration, error) { + blockData, discarded, err := n.sync.Sync2(ctx, lastBlock) stats := n.sync.Stats() if err != nil { // case: error - if strings.Contains(err.Error(), "context canceled") { - log.Warnw("Synchronizer.Sync", "err", err) - } else { - log.Errorw("Synchronizer.Sync", "err", err) - } - return nil, n.cfg.Synchronizer.SyncLoopInterval.Duration + return nil, n.cfg.Synchronizer.SyncLoopInterval.Duration, err } else if discarded != nil { // case: reorg log.Infow("Synchronizer.Sync reorg", "discarded", *discarded) n.handleReorg(stats) - return nil, time.Duration(0) + return nil, time.Duration(0), nil } else if blockData != nil { // case: new block n.handleNewBlock(stats, synchronizer.SCVariablesPtr{ @@ -432,10 +426,10 @@ func (n *Node) syncLoopFn(lastBlock *common.Block) (*common.Block, time.Duration Auction: blockData.Auction.Vars, WDelayer: blockData.WDelayer.Vars, }, blockData.Rollup.Batches) - return &blockData.Block, time.Duration(0) + return &blockData.Block, time.Duration(0), nil } else { // case: no block - return lastBlock, n.cfg.Synchronizer.SyncLoopInterval.Duration + return lastBlock, n.cfg.Synchronizer.SyncLoopInterval.Duration, nil } } @@ -454,6 +448,7 @@ func (n *Node) StartSynchronizer() { n.wg.Add(1) go func() { + var err error var lastBlock *common.Block waitDuration := time.Duration(0) for { @@ -463,7 +458,13 @@ func (n *Node) StartSynchronizer() { n.wg.Done() return case <-time.After(waitDuration): - lastBlock, waitDuration = n.syncLoopFn(lastBlock) + if lastBlock, waitDuration, err = n.syncLoopFn(n.ctx, + lastBlock); err != nil { + if n.ctx.Err() != nil { + continue + } + log.Errorw("Synchronizer.Sync", "err", err) + } } } }() @@ -496,6 +497,9 @@ func (n *Node) StartDebugAPI() { n.wg.Done() }() if err := n.debugAPI.Run(n.ctx); err != nil { + if n.ctx.Err() != nil { + return + } log.Fatalw("DebugAPI.Run", "err", err) } }() @@ -511,6 +515,9 @@ func (n *Node) StartNodeAPI() { n.wg.Done() }() if err := n.nodeAPI.Run(n.ctx); err != nil { + if n.ctx.Err() != nil { + return + } log.Fatalw("NodeAPI.Run", "err", err) } }()