Skip to content

Commit

Permalink
feat: support custom RPC request timeout through env
Browse files Browse the repository at this point in the history
  • Loading branch information
lehainam-dev committed May 30, 2024
1 parent 76bf581 commit 072b03b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
11 changes: 8 additions & 3 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,18 @@ func redisConfigFromCli(c *cli.Context) redis.Config {
func NewListener(c *cli.Context) (*listener.Listener, error) {
l := zap.S()

rpcRequestTimeout := c.Duration(rpcRequestTimeoutFlag.Name)
if rpcRequestTimeout == 0 {
rpcRequestTimeout = defaultRequestTimeout
}

httpClient := &http.Client{
Timeout: defaultRequestTimeout,
Timeout: rpcRequestTimeout,
}
wsRPC := c.String(wsRPCFlag.Name)
l.Infow("Connect to node websocket rpc", "rpc", wsRPC)
wsEVMClient, err := evmclient.DialContextWithTimeout(
context.Background(), wsRPC, httpClient, defaultRequestTimeout)
context.Background(), wsRPC, httpClient, rpcRequestTimeout)
if err != nil {
l.Errorw("Fail to connect to node", "rpc", wsRPC, "error", err)

Expand All @@ -67,7 +72,7 @@ func NewListener(c *cli.Context) (*listener.Listener, error) {
httpRPC := c.String(httpRPCFlag.Name)
l.Infow("Connect to node http rpc", "rpc", httpRPC)
httpEVMClient, err := evmclient.DialContextWithTimeout(
context.Background(), httpRPC, httpClient, defaultRequestTimeout)
context.Background(), httpRPC, httpClient, rpcRequestTimeout)
if err != nil {
l.Errorw("Fail to connect to node", "rpc", httpRPC, "error", err)

Expand Down
7 changes: 7 additions & 0 deletions internal/app/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ var (
Value: "http://localhost:8545",
Usage: "HTTP RPC to connect to blockchain node, default: http://localhost:8545",
}
rpcRequestTimeoutFlag = &cli.DurationFlag{
Name: "rpc-request-timeout",
EnvVars: []string{"RPC_REQUEST_TIMEOUT"},
Value: 10 * time.Second,

Check failure on line 32 in internal/app/flags.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

mnd: Magic number: 10, in <assign> detected (gomnd)
Usage: "Timeout for RPC request",
}
sanityNodeRPCFlag = &cli.StringFlag{
Name: "sanity-node-rpc",
EnvVars: []string{"SANITY_NODE_RPC"},
Expand Down Expand Up @@ -212,6 +218,7 @@ func NewFlags() []cli.Flag {
logLevelFlag,
wsRPCFlag,
httpRPCFlag,
rpcRequestTimeoutFlag,
sanityNodeRPCFlag,
sanityCheckIntervalFlag,
}
Expand Down

0 comments on commit 072b03b

Please sign in to comment.