Skip to content

Commit eb23282

Browse files
committed
cloud_functions: add retries to ntt related requests
Signed-off-by: bingyuyap <bingyu.yap.21@gmail.com>
1 parent b9e2dd7 commit eb23282

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

cloud_functions/src/computeNTTRateLimits.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
NTTEvmChain,
1111
NTTChain,
1212
nttChains,
13+
retry,
1314
} from '@wormhole-foundation/wormhole-monitor-common';
1415
import { EvmPlatform } from '@wormhole-foundation/sdk-evm';
1516
import { SolanaPlatform } from '@wormhole-foundation/sdk-solana';
@@ -76,7 +77,7 @@ async function computeNTTRateLimits_(
7677
let totalInboundCapacity = 0n;
7778
const inboundRateLimits = await Promise.all(
7879
inboundChains.map(async (inboundChain): Promise<NTTRateLimit> => {
79-
const inboundCapacity = await ntt.getCurrentInboundCapacity(inboundChain);
80+
const inboundCapacity = await retry(() => ntt.getCurrentInboundCapacity(inboundChain));
8081
totalInboundCapacity += inboundCapacity;
8182

8283
return {
@@ -91,7 +92,7 @@ async function computeNTTRateLimits_(
9192
})
9293
);
9394

94-
const outboundCapacity = await ntt.getCurrentOutboundCapacity();
95+
const outboundCapacity = await retry(() => ntt.getCurrentOutboundCapacity());
9596

9697
return {
9798
tokenName: token,

common/src/evm.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import axios from 'axios';
22
import { keccak256 } from '@wormhole-foundation/sdk-definitions';
3+
import { retry } from './utils';
34

45
export async function callContractMethod(
56
rpc: string,
@@ -23,7 +24,7 @@ export async function callContractMethod(
2324
};
2425

2526
try {
26-
const response = await axios.post(rpc, payload);
27+
const response = await retry(() => axios.post(rpc, payload));
2728
if (response.data.error) {
2829
throw new Error(`Error calling contract method: ${response.data.error.message}`);
2930
}

common/src/solana.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import axios from 'axios';
1010
import { decode } from 'bs58';
1111
import { encoding } from '@wormhole-foundation/sdk-base';
1212
import { TokenAmount } from './types';
13+
import { retry } from './utils';
1314

1415
export const isLegacyMessage = (message: Message | MessageV0): message is Message => {
1516
return message.version === 'legacy';
@@ -43,7 +44,7 @@ export const getSolanaTokenDecimals = async (rpc: string, mintAddress: string):
4344
};
4445

4546
try {
46-
const response = await axios.post(rpc, payload);
47+
const response = await retry(() => axios.post(rpc, payload));
4748
const result = response.data.result;
4849
if (result?.value?.data?.parsed?.info?.decimals !== undefined) {
4950
return result.value.data.parsed.info.decimals;
@@ -106,7 +107,7 @@ export async function makeRpcCall(
106107
],
107108
};
108109

109-
const response = await axios.post(rpcUrl, payload);
110+
const response = await retry(() => axios.post(rpcUrl, payload));
110111
if (response.data.error) {
111112
throw new Error(`Error fetching ${method} account: ${response.data.error.message}`);
112113
}

common/src/utils.ts

+21
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,24 @@ export async function sendToPagerDuty(info: PagerDutyInfo): Promise<any> {
214214
}
215215
return response.data.data;
216216
}
217+
218+
// Retry utility function
219+
export async function retry<T>(
220+
fn: () => Promise<T>,
221+
retries = 3,
222+
delay = 1000,
223+
retryCount = 0
224+
): Promise<T> {
225+
try {
226+
return await fn();
227+
} catch (error) {
228+
if (retryCount >= retries) {
229+
console.error(`Failed after ${retries} retries:`, error);
230+
throw error;
231+
} else {
232+
console.warn(`Retrying (${retryCount + 1}/${retries})...`, error);
233+
await new Promise((resolve) => setTimeout(resolve, delay * (retryCount + 1)));
234+
return retry(fn, retries, delay, retryCount + 1);
235+
}
236+
}
237+
}

0 commit comments

Comments
 (0)