Skip to content

Commit 1bf89eb

Browse files
kayanojlehtimaki
andauthored
feat: Add configurable timeout to rpc endpoints (#28)
* feat: Add configurable timeout to rpc endpoints * Update README.md Co-authored-by: Joonas Lehtimäki <[email protected]> * Update README.md Co-authored-by: Joonas Lehtimäki <[email protected]> --------- Co-authored-by: Joonas Lehtimäki <[email protected]>
1 parent 8b74016 commit 1bf89eb

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ rpc:
1717
- chainName: archwaytestnet
1818
chainId: constantine-3
1919
url: https://rpc.constantine.archway.tech:443
20+
timeout: 2s
2021

2122
github:
2223
org: archway-network
@@ -33,6 +34,9 @@ accounts:
3334
During startup it fetches IBC paths from github based on provided config.
3435
If env var GITHUB_TOKEN is provided it will be used to make authenticated requests to GitHub API.
3536
Using provided RPC endpoints it gets clients expiration dates for fetched paths.
37+
Each RCP endpoint can have a different timeout specified.
38+
If env var GLOBAL_RPC_TIMEOUT (default 5s) is provided, it specifies the timeout for endpoints
39+
without having it defined.
3640
3741
For provided accounts it fetches wallet balances using endpoints defined in rpc list.
3842

pkg/chain/chain.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@ type Info struct {
1717
ChainID string
1818
RPCAddr string
1919
ClientID string
20+
Timeout string
2021
}
2122

2223
func PrepChain(info Info) (*relayer.Chain, error) {
2324
logger := zap.NewNop()
25+
26+
timeout := rpcTimeout
27+
if info.Timeout != "" {
28+
timeout = info.Timeout
29+
}
30+
2431
providerConfig := cosmos.CosmosProviderConfig{
2532
ChainID: info.ChainID,
26-
Timeout: rpcTimeout,
33+
Timeout: timeout,
2734
KeyringBackend: keyringBackend,
2835
RPCAddr: info.RPCAddr,
2936
}

pkg/config/config.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ type RPC struct {
3131
ChainName string `yaml:"chainName"`
3232
ChainID string `yaml:"chainId"`
3333
URL string `yaml:"url"`
34+
Timeout string `yaml:"timeout"`
3435
}
3536

3637
type Config struct {
37-
Accounts []Account `yaml:"accounts"`
38-
RPCs []RPC `yaml:"rpc"`
39-
GitHub struct {
38+
Accounts []Account `yaml:"accounts"`
39+
GlobalRPCTimeout string `env:"GLOBAL_RPC_TIMEOUT" envDefault:"5s"`
40+
RPCs []RPC `yaml:"rpc"`
41+
GitHub struct {
4042
Org string `yaml:"org"`
4143
Repo string `yaml:"repo"`
4244
IBCDir string `yaml:"dir"`
@@ -99,6 +101,7 @@ func (a *Account) GetBalance(rpcs *map[string]RPC) error {
99101
chain, err := chain.PrepChain(chain.Info{
100102
ChainID: (*rpcs)[a.ChainName].ChainID,
101103
RPCAddr: (*rpcs)[a.ChainName].URL,
104+
Timeout: (*rpcs)[a.ChainName].Timeout,
102105
})
103106
if err != nil {
104107
return err
@@ -120,6 +123,10 @@ func (c *Config) GetRPCsMap() *map[string]RPC {
120123
rpcs := map[string]RPC{}
121124

122125
for _, rpc := range c.RPCs {
126+
if rpc.Timeout == "" {
127+
rpc.Timeout = c.GlobalRPCTimeout
128+
}
129+
123130
rpcs[rpc.ChainName] = rpc
124131
}
125132

pkg/config/config_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,27 @@ func TestGetRPCsMap(t *testing.T) {
1818
ChainName: "archwaytestnet",
1919
ChainID: "constantine-3",
2020
URL: "https://rpc.constantine.archway.tech:443",
21+
Timeout: "2s",
2122
},
2223
}
2324

24-
cfg := Config{RPCs: rpcs}
25+
cfg := Config{
26+
GlobalRPCTimeout: "5s",
27+
RPCs: rpcs,
28+
}
2529

2630
exp := map[string]RPC{
2731
"archway": {
2832
ChainName: "archway",
2933
ChainID: "archway-1",
3034
URL: "https://rpc.mainnet.archway.io:443",
35+
Timeout: "5s",
3136
},
3237
"archwaytestnet": {
3338
ChainName: "archwaytestnet",
3439
ChainID: "constantine-3",
3540
URL: "https://rpc.constantine.archway.tech:443",
41+
Timeout: "2s",
3642
},
3743
}
3844

pkg/ibc/ibc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func GetClientsInfo(ibc *config.IBCData, rpcs *map[string]config.RPC) (ClientsIn
4545
cdA := chain.Info{
4646
ChainID: (*rpcs)[ibc.Chain1.ChainName].ChainID,
4747
RPCAddr: (*rpcs)[ibc.Chain1.ChainName].URL,
48+
Timeout: (*rpcs)[ibc.Chain1.ChainName].Timeout,
4849
ClientID: ibc.Chain1.ClientID,
4950
}
5051

@@ -58,6 +59,7 @@ func GetClientsInfo(ibc *config.IBCData, rpcs *map[string]config.RPC) (ClientsIn
5859
cdB := chain.Info{
5960
ChainID: (*rpcs)[ibc.Chain2.ChainName].ChainID,
6061
RPCAddr: (*rpcs)[ibc.Chain2.ChainName].URL,
62+
Timeout: (*rpcs)[ibc.Chain2.ChainName].Timeout,
6163
ClientID: ibc.Chain2.ClientID,
6264
}
6365

@@ -116,6 +118,7 @@ func GetChannelsInfo(ibc *config.IBCData, rpcs *map[string]config.RPC) (Channels
116118
cdA := chain.Info{
117119
ChainID: (*rpcs)[ibc.Chain1.ChainName].ChainID,
118120
RPCAddr: (*rpcs)[ibc.Chain1.ChainName].URL,
121+
Timeout: (*rpcs)[ibc.Chain1.ChainName].Timeout,
119122
ClientID: ibc.Chain1.ClientID,
120123
}
121124

@@ -127,6 +130,7 @@ func GetChannelsInfo(ibc *config.IBCData, rpcs *map[string]config.RPC) (Channels
127130
cdB := chain.Info{
128131
ChainID: (*rpcs)[ibc.Chain2.ChainName].ChainID,
129132
RPCAddr: (*rpcs)[ibc.Chain2.ChainName].URL,
133+
Timeout: (*rpcs)[ibc.Chain2.ChainName].Timeout,
130134
ClientID: ibc.Chain2.ClientID,
131135
}
132136

0 commit comments

Comments
 (0)