From 2ec2ef9ad6bb1d8ca877b19d0ba9b51efac1e93f Mon Sep 17 00:00:00 2001 From: Jesse Lee Date: Fri, 3 Nov 2023 05:05:26 -0400 Subject: [PATCH] fix: go routine error handling --- cmd/monitor/monitor.go | 29 +++++++++++++++++------------ go.mod | 2 +- go.sum | 2 -- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/cmd/monitor/monitor.go b/cmd/monitor/monitor.go index 621ca437..755b900a 100644 --- a/cmd/monitor/monitor.go +++ b/cmd/monitor/monitor.go @@ -102,20 +102,25 @@ func monitor(ctx context.Context) error { isUiRendered := false errChan := make(chan error) go func() { - for { - err = fetchBlocks(ctx, ec, ms, rpc, isUiRendered) - if err != nil { - continue - } + select { + case <-ctx.Done(): // listens for a cancellation signal + return // exit the goroutine when the context is done + default: + for { + err = fetchBlocks(ctx, ec, ms, rpc, isUiRendered) + if err != nil { + continue + } - if !isUiRendered { - go func() { - errChan <- renderMonitorUI(ctx, ec, ms, rpc) - }() - isUiRendered = true - } + if !isUiRendered { + go func() { + errChan <- renderMonitorUI(ctx, ec, ms, rpc) + }() + isUiRendered = true + } - time.Sleep(interval) + time.Sleep(interval) + } } }() diff --git a/go.mod b/go.mod index 4e4445b4..50995f16 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21 require ( cloud.google.com/go/datastore v1.14.0 github.com/btcsuite/btcutil v1.0.2 + github.com/cenkalti/backoff/v4 v4.2.1 github.com/ethereum/go-ethereum v1.13.2 github.com/gizak/termui/v3 v3.1.0 github.com/google/gofuzz v1.2.0 @@ -45,7 +46,6 @@ require ( github.com/bits-and-blooms/bitset v1.7.0 // indirect github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 // indirect - github.com/cenkalti/backoff/v4 v4.2.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/cockroachdb/errors v1.8.1 // indirect github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f // indirect diff --git a/go.sum b/go.sum index fdc21927..29ce29d0 100644 --- a/go.sum +++ b/go.sum @@ -88,8 +88,6 @@ github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVa github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc= github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY= github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs= -github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= -github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM= github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=