From 1bf89eb0309d41d23cc355448f77144d876ec34d Mon Sep 17 00:00:00 2001 From: Tomasz Nowak <1542258+kayano@users.noreply.github.com> Date: Mon, 27 Nov 2023 12:32:17 +0100 Subject: [PATCH] feat: Add configurable timeout to rpc endpoints (#28) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: Add configurable timeout to rpc endpoints * Update README.md Co-authored-by: Joonas Lehtimäki * Update README.md Co-authored-by: Joonas Lehtimäki --------- Co-authored-by: Joonas Lehtimäki --- README.md | 4 ++++ pkg/chain/chain.go | 9 ++++++++- pkg/config/config.go | 13 ++++++++++--- pkg/config/config_test.go | 8 +++++++- pkg/ibc/ibc.go | 4 ++++ 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8bd8825..8a604b1 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ rpc: - chainName: archwaytestnet chainId: constantine-3 url: https://rpc.constantine.archway.tech:443 + timeout: 2s github: org: archway-network @@ -33,6 +34,9 @@ accounts: During startup it fetches IBC paths from github based on provided config. If env var GITHUB_TOKEN is provided it will be used to make authenticated requests to GitHub API. Using provided RPC endpoints it gets clients expiration dates for fetched paths. +Each RCP endpoint can have a different timeout specified. +If env var GLOBAL_RPC_TIMEOUT (default 5s) is provided, it specifies the timeout for endpoints +without having it defined. For provided accounts it fetches wallet balances using endpoints defined in rpc list. diff --git a/pkg/chain/chain.go b/pkg/chain/chain.go index 980da4c..224179f 100644 --- a/pkg/chain/chain.go +++ b/pkg/chain/chain.go @@ -17,13 +17,20 @@ type Info struct { ChainID string RPCAddr string ClientID string + Timeout string } func PrepChain(info Info) (*relayer.Chain, error) { logger := zap.NewNop() + + timeout := rpcTimeout + if info.Timeout != "" { + timeout = info.Timeout + } + providerConfig := cosmos.CosmosProviderConfig{ ChainID: info.ChainID, - Timeout: rpcTimeout, + Timeout: timeout, KeyringBackend: keyringBackend, RPCAddr: info.RPCAddr, } diff --git a/pkg/config/config.go b/pkg/config/config.go index c1a00de..34b2dbe 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -31,12 +31,14 @@ type RPC struct { ChainName string `yaml:"chainName"` ChainID string `yaml:"chainId"` URL string `yaml:"url"` + Timeout string `yaml:"timeout"` } type Config struct { - Accounts []Account `yaml:"accounts"` - RPCs []RPC `yaml:"rpc"` - GitHub struct { + Accounts []Account `yaml:"accounts"` + GlobalRPCTimeout string `env:"GLOBAL_RPC_TIMEOUT" envDefault:"5s"` + RPCs []RPC `yaml:"rpc"` + GitHub struct { Org string `yaml:"org"` Repo string `yaml:"repo"` IBCDir string `yaml:"dir"` @@ -99,6 +101,7 @@ func (a *Account) GetBalance(rpcs *map[string]RPC) error { chain, err := chain.PrepChain(chain.Info{ ChainID: (*rpcs)[a.ChainName].ChainID, RPCAddr: (*rpcs)[a.ChainName].URL, + Timeout: (*rpcs)[a.ChainName].Timeout, }) if err != nil { return err @@ -120,6 +123,10 @@ func (c *Config) GetRPCsMap() *map[string]RPC { rpcs := map[string]RPC{} for _, rpc := range c.RPCs { + if rpc.Timeout == "" { + rpc.Timeout = c.GlobalRPCTimeout + } + rpcs[rpc.ChainName] = rpc } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 884c1cc..61e1588 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -18,21 +18,27 @@ func TestGetRPCsMap(t *testing.T) { ChainName: "archwaytestnet", ChainID: "constantine-3", URL: "https://rpc.constantine.archway.tech:443", + Timeout: "2s", }, } - cfg := Config{RPCs: rpcs} + cfg := Config{ + GlobalRPCTimeout: "5s", + RPCs: rpcs, + } exp := map[string]RPC{ "archway": { ChainName: "archway", ChainID: "archway-1", URL: "https://rpc.mainnet.archway.io:443", + Timeout: "5s", }, "archwaytestnet": { ChainName: "archwaytestnet", ChainID: "constantine-3", URL: "https://rpc.constantine.archway.tech:443", + Timeout: "2s", }, } diff --git a/pkg/ibc/ibc.go b/pkg/ibc/ibc.go index e8c4fda..2c26c1b 100644 --- a/pkg/ibc/ibc.go +++ b/pkg/ibc/ibc.go @@ -45,6 +45,7 @@ func GetClientsInfo(ibc *config.IBCData, rpcs *map[string]config.RPC) (ClientsIn cdA := chain.Info{ ChainID: (*rpcs)[ibc.Chain1.ChainName].ChainID, RPCAddr: (*rpcs)[ibc.Chain1.ChainName].URL, + Timeout: (*rpcs)[ibc.Chain1.ChainName].Timeout, ClientID: ibc.Chain1.ClientID, } @@ -58,6 +59,7 @@ func GetClientsInfo(ibc *config.IBCData, rpcs *map[string]config.RPC) (ClientsIn cdB := chain.Info{ ChainID: (*rpcs)[ibc.Chain2.ChainName].ChainID, RPCAddr: (*rpcs)[ibc.Chain2.ChainName].URL, + Timeout: (*rpcs)[ibc.Chain2.ChainName].Timeout, ClientID: ibc.Chain2.ClientID, } @@ -116,6 +118,7 @@ func GetChannelsInfo(ibc *config.IBCData, rpcs *map[string]config.RPC) (Channels cdA := chain.Info{ ChainID: (*rpcs)[ibc.Chain1.ChainName].ChainID, RPCAddr: (*rpcs)[ibc.Chain1.ChainName].URL, + Timeout: (*rpcs)[ibc.Chain1.ChainName].Timeout, ClientID: ibc.Chain1.ClientID, } @@ -127,6 +130,7 @@ func GetChannelsInfo(ibc *config.IBCData, rpcs *map[string]config.RPC) (Channels cdB := chain.Info{ ChainID: (*rpcs)[ibc.Chain2.ChainName].ChainID, RPCAddr: (*rpcs)[ibc.Chain2.ChainName].URL, + Timeout: (*rpcs)[ibc.Chain2.ChainName].Timeout, ClientID: ibc.Chain2.ClientID, }