Skip to content

Commit 07c41d3

Browse files
committed
chore: use retry-go for retries in query
1 parent 258d1a0 commit 07c41d3

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

client/client.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ type Client struct {
2424
logger log.Logger // Embedded logger
2525
protoCodec codec.ProtoCodecMarshaler // Used for marshaling and unmarshaling protobuf data
2626
queryHeight int64 // Query height for blockchain data
27-
queryMaxRetries int // Maximum number of retries for queries
2827
queryProve bool // Flag indicating whether to prove queries
28+
queryRetries uint // Number of retries for queries
2929
queryRetryDelay time.Duration // Delay between query retries
3030
rpcAddr string // RPC server address
3131
rpcTimeout time.Duration // RPC timeout duration
@@ -70,18 +70,18 @@ func (c *Client) WithProtoCodec(protoCodec codec.ProtoCodecMarshaler) *Client {
7070
return c
7171
}
7272

73-
// WithQueryMaxRetries sets the maximum number of retries for queries and returns the updated Client.
74-
func (c *Client) WithQueryMaxRetries(maxRetries int) *Client {
75-
c.queryMaxRetries = maxRetries
76-
return c
77-
}
78-
7973
// WithQueryProve sets the prove flag for queries and returns the updated Client.
8074
func (c *Client) WithQueryProve(prove bool) *Client {
8175
c.queryProve = prove
8276
return c
8377
}
8478

79+
// WithQueryRetries sets the number of retries for queries and returns the updated Client.
80+
func (c *Client) WithQueryRetries(retries uint) *Client {
81+
c.queryRetries = retries
82+
return c
83+
}
84+
8585
// WithQueryRetryDelay sets the retry delay duration for queries and returns the updated Client.
8686
func (c *Client) WithQueryRetryDelay(delay time.Duration) *Client {
8787
c.queryRetryDelay = delay

client/query.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ import (
55
"errors"
66
"fmt"
77
"strings"
8-
"time"
98

9+
"github.com/avast/retry-go/v4"
1010
abci "github.com/cometbft/cometbft/abci/types"
1111
"github.com/cometbft/cometbft/libs/bytes"
1212
"github.com/cometbft/cometbft/rpc/client"
1313
core "github.com/cometbft/cometbft/rpc/core/types"
1414
"github.com/cosmos/cosmos-sdk/codec"
15-
"github.com/v2fly/v2ray-core/v5/common/retry"
1615
)
1716

1817
// IsNotFoundError checks if the given error string indicates a gRPC NotFound error.
@@ -32,7 +31,7 @@ func (c *Client) ABCIQueryWithOptions(ctx context.Context, path string, data byt
3231
var result *core.ResultABCIQuery
3332

3433
// Define the function to perform the ABCI query.
35-
fn := func() error {
34+
queryFunc := func() error {
3635
// Get the RPC client for querying.
3736
http, err := c.HTTP()
3837
if err != nil {
@@ -55,8 +54,12 @@ func (c *Client) ABCIQueryWithOptions(ctx context.Context, path string, data byt
5554
}
5655

5756
// Retry the query using the configured maximum retries and delay.
58-
delay := uint32(c.queryRetryDelay / time.Millisecond)
59-
if err := retry.Timed(c.queryMaxRetries, delay).On(fn); err != nil {
57+
if err := retry.Do(
58+
queryFunc,
59+
retry.Attempts(c.queryRetries),
60+
retry.Delay(c.queryRetryDelay),
61+
retry.LastErrorOnly(true),
62+
); err != nil {
6063
return nil, err
6164
}
6265

0 commit comments

Comments
 (0)