Skip to content

Commit

Permalink
feat: support custom RPC request timeout through env (#116)
Browse files Browse the repository at this point in the history
* feat: support custom RPC request timeout through env

* chore: fix lint
  • Loading branch information
lehainam-dev authored May 30, 2024
1 parent 76bf581 commit d010aec
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, // nolint: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 d010aec

Please sign in to comment.