Skip to content

Commit

Permalink
refactor: use exponential-back-off instead of constant-back-off
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed Sep 30, 2024
1 parent 744f04a commit af2e334
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion server/indexer_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ func (eis *EVMIndexerService) OnStart() error {

// when kava in state-sync mode, it returns zero as latest_block_height, which leads to undesired behavior, more
// details here: https://github.com/Kava-Labs/ethermint/issues/79 to prevent this we wait until state-sync will finish
if err := waitUntilClientReady(ctx, eis.client, backoff.NewConstantBackOff(time.Second)); err != nil {
exponentialBackOff := backoff.NewExponentialBackOff(
backoff.WithMaxInterval(time.Second*10), // set max retry interval
backoff.WithMaxElapsedTime(time.Hour*3), // set timeout
)
if err := waitUntilClientReady(ctx, eis.client, exponentialBackOff); err != nil {
return err
}

Expand Down
18 changes: 18 additions & 0 deletions server/indexer_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"context"
"math"
"testing"
"time"

Expand Down Expand Up @@ -73,3 +74,20 @@ func TestWaitUntilClientReady(t *testing.T) {
})
}
}

func TestWaitUntilClientReadyTimeout(t *testing.T) {
ctxb := context.Background()
// create a mock client which always returns an error
mock := newStatusClientMock(math.MaxUint)

exponentialBackOff := backoff.NewExponentialBackOff(
backoff.WithInitialInterval(time.Millisecond),
backoff.WithMaxInterval(time.Millisecond*10),
backoff.WithMaxElapsedTime(time.Millisecond*100),
)

err := waitUntilClientReady(ctxb, mock, exponentialBackOff)
// make sure error is propagated in case of timeout
require.Error(t, err)
require.Contains(t, err.Error(), "node isn't ready, possibly in state sync process")
}

0 comments on commit af2e334

Please sign in to comment.