Skip to content

Commit 26fdfa2

Browse files
committed
Retry balance checks on failures
We want to use a retry mechanism for single executions of the balance check. The retries will be performed during the period of `retryTimeout`, logging a warning on each error. When the timeout is hit an error will be logged and retries stopped. A next balance check will be triggered at the tick.
1 parent 931fd63 commit 26fdfa2

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

pkg/chain/celo/celoutil/balance_monitor.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package celoutil
22

33
import (
44
"context"
5+
"time"
6+
57
"github.com/celo-org/celo-blockchain/common"
68
"github.com/keep-network/keep-common/pkg/chain/celo"
79
"github.com/keep-network/keep-common/pkg/chain/ethlike"
8-
"time"
910
)
1011

1112
// BalanceSource provides a balance info for the given address.
@@ -38,16 +39,19 @@ func NewBalanceMonitor(balanceSource BalanceSource) *BalanceMonitor {
3839
// Observe starts a process which checks the address balance with the given
3940
// tick and triggers an alert in case the balance falls below the
4041
// alert threshold value.
42+
// The balance check will be retried in case of an error up to the retry timeout.
4143
func (bm *BalanceMonitor) Observe(
4244
ctx context.Context,
4345
address common.Address,
4446
alertThreshold *celo.Wei,
4547
tick time.Duration,
48+
retryTimeout time.Duration,
4649
) {
4750
bm.delegate.Observe(
4851
ctx,
4952
ethlike.Address(address),
5053
&alertThreshold.Token,
5154
tick,
55+
retryTimeout,
5256
)
5357
}

pkg/chain/ethereum/ethutil/balance_monitor.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package ethutil
22

33
import (
44
"context"
5+
"time"
6+
57
"github.com/ethereum/go-ethereum/common"
68
"github.com/keep-network/keep-common/pkg/chain/ethereum"
79
"github.com/keep-network/keep-common/pkg/chain/ethlike"
8-
"time"
910
)
1011

1112
// BalanceSource provides a balance info for the given address.
@@ -38,16 +39,19 @@ func NewBalanceMonitor(balanceSource BalanceSource) *BalanceMonitor {
3839
// Observe starts a process which checks the address balance with the given
3940
// tick and triggers an alert in case the balance falls below the
4041
// alert threshold value.
42+
// The balance check will be retried in case of an error up to the retry timeout.
4143
func (bm *BalanceMonitor) Observe(
4244
ctx context.Context,
4345
address common.Address,
4446
alertThreshold *ethereum.Wei,
4547
tick time.Duration,
48+
retryTimeout time.Duration,
4649
) {
4750
bm.delegate.Observe(
4851
ctx,
4952
ethlike.Address(address),
5053
&alertThreshold.Token,
5154
tick,
55+
retryTimeout,
5256
)
5357
}

pkg/chain/ethlike/balance_monitor.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package ethlike
22

33
import (
44
"context"
5+
"fmt"
56
"time"
7+
8+
"github.com/keep-network/keep-common/pkg/wrappers"
69
)
710

811
// BalanceSource provides a balance info for the given address.
@@ -22,17 +25,26 @@ func NewBalanceMonitor(balanceSource BalanceSource) *BalanceMonitor {
2225
// Observe starts a process which checks the address balance with the given
2326
// tick and triggers an alert in case the balance falls below the
2427
// alert threshold value.
28+
// The balance check will be retried in case of an error up to the retry timeout.
2529
func (bm *BalanceMonitor) Observe(
2630
ctx context.Context,
2731
address Address,
2832
alertThreshold *Token,
2933
tick time.Duration,
34+
retryTimeout time.Duration,
3035
) {
31-
check := func() {
36+
check := func(ctx context.Context) error {
3237
balance, err := bm.balanceSource(address)
3338
if err != nil {
34-
logger.Errorf("balance monitor error: [%v]", err)
35-
return
39+
wrappedErr := fmt.Errorf(
40+
"failed to get balance for account [%s]: [%w]",
41+
address.TerminalString(),
42+
err,
43+
)
44+
45+
logger.Warning(wrappedErr)
46+
47+
return wrappedErr
3648
}
3749

3850
if balance.Cmp(alertThreshold.Int) == -1 {
@@ -43,6 +55,8 @@ func (bm *BalanceMonitor) Observe(
4355
alertThreshold.Text(10),
4456
)
4557
}
58+
59+
return nil
4660
}
4761

4862
go func() {
@@ -52,7 +66,9 @@ func (bm *BalanceMonitor) Observe(
5266
for {
5367
select {
5468
case <-ticker.C:
55-
check()
69+
err := wrappers.DoWithDefaultRetry(retryTimeout, check)
70+
71+
logger.Errorf("balance monitor error: [%v]", err)
5672
case <-ctx.Done():
5773
return
5874
}

0 commit comments

Comments
 (0)