From 072b03bee22a6368ed0009336636a6721f5164bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=E1=BA=A3i=20Nam?= Date: Thu, 30 May 2024 13:41:32 +0700 Subject: [PATCH] feat: support custom RPC request timeout through env --- internal/app/app.go | 11 ++++++++--- internal/app/flags.go | 7 +++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index 38fdaa4..d00ef40 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -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) @@ -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) diff --git a/internal/app/flags.go b/internal/app/flags.go index 4e3763e..2f8ec83 100644 --- a/internal/app/flags.go +++ b/internal/app/flags.go @@ -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, + Usage: "Timeout for RPC request", + } sanityNodeRPCFlag = &cli.StringFlag{ Name: "sanity-node-rpc", EnvVars: []string{"SANITY_NODE_RPC"}, @@ -212,6 +218,7 @@ func NewFlags() []cli.Flag { logLevelFlag, wsRPCFlag, httpRPCFlag, + rpcRequestTimeoutFlag, sanityNodeRPCFlag, sanityCheckIntervalFlag, }