Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: retry upon RPC connection failure #71

Merged
merged 9 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased
### Changes
- ([\#71](https://github.com/forbole/juno/pull/71)) Retry RPC client connection upon failure instead of panic

## v3.3.0
### Changes
- ([\#67](https://github.com/forbole/juno/pull/67)) Added support for concurrent transaction handling
Expand Down
39 changes: 25 additions & 14 deletions cmd/start/cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package start

import (
"fmt"
"os"
"os/signal"
"sync"
Expand Down Expand Up @@ -125,16 +124,13 @@ func enqueueMissingBlocks(exportQueue types.HeightQueue, ctx *parser.Context) {
cfg := config.Cfg.Parser

// Get the latest height
latestBlockHeight, err := ctx.Node.LatestHeight()
if err != nil {
panic(fmt.Errorf("failed to get last block from RPCConfig client: %s", err))
}
latestBlockHeight := mustGetLatestHeight(ctx, 0)

if cfg.FastSync {
ctx.Logger.Info("fast sync is enabled, ignoring all previous blocks", "latest_block_height", latestBlockHeight)
for _, module := range ctx.Modules {
if mod, ok := module.(modules.FastSyncModule); ok {
err = mod.DownloadState(latestBlockHeight)
err := mod.DownloadState(latestBlockHeight)
if err != nil {
ctx.Logger.Error("error while performing fast sync",
"err", err,
Expand All @@ -155,17 +151,11 @@ func enqueueMissingBlocks(exportQueue types.HeightQueue, ctx *parser.Context) {

// enqueueNewBlocks enqueues new block heights onto the provided queue.
func enqueueNewBlocks(exportQueue types.HeightQueue, ctx *parser.Context) {
currHeight, err := ctx.Node.LatestHeight()
if err != nil {
panic(fmt.Errorf("failed to get last block from RPCConfig client: %s", err))
}
currHeight := mustGetLatestHeight(ctx, 0)

// Enqueue upcoming heights
for {
latestBlockHeight, err := ctx.Node.LatestHeight()
if err != nil {
panic(fmt.Errorf("failed to get last block from RPCConfig client: %s", err))
}
latestBlockHeight := mustGetLatestHeight(ctx, 0)

// Enqueue all heights from the current height up to the latest height
for ; currHeight <= latestBlockHeight; currHeight++ {
Expand All @@ -176,6 +166,27 @@ func enqueueNewBlocks(exportQueue types.HeightQueue, ctx *parser.Context) {
}
}

// mustGetLatestHeight will keep trying until it gets the latest height from RPC client, return 0 if retry count > 50
func mustGetLatestHeight(ctx *parser.Context, retryCount int16) int64 {
if retryCount > 50 {
return 0
}

latestBlockHeight, err := ctx.Node.LatestHeight()
if err != nil {
avgBlockTime := config.Cfg.Parser.AvgBlockTime

ctx.Logger.Error("failed to get last block from RPCConfig client",
"err", err, "retry interval", avgBlockTime, "retry count", retryCount)
time.Sleep(avgBlockTime)

retryCount++
return mustGetLatestHeight(ctx, retryCount)
}

return latestBlockHeight
}
huichiaotsou marked this conversation as resolved.
Show resolved Hide resolved

// trapSignal will listen for any OS signal and invoke Done on the main
// WaitGroup allowing the main process to gracefully exit.
func trapSignal(ctx *parser.Context) {
Expand Down