Skip to content

Commit

Permalink
fix: go routine error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gatsbyz committed Nov 3, 2023
1 parent b6e7007 commit 2ec2ef9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
29 changes: 17 additions & 12 deletions cmd/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}()

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 2ec2ef9

Please sign in to comment.