I have implemented a simple do/while loop to handle a http request to a third-party. I don’t know when the data I receive will have elements in its array so I retry up to 10 times until the array given to me has data which then I stop and store the data in my database.
My question is in terms of performance is my API going to be affected by having this retry mechanism using a simple do/while loop? If so what is a better way to implement a retry mechanism like this??
public MAX_RETRY = 10;
public async processMetrics(sessionId: any, side: any): Promise <any> {
try {
let metrics;
let retryAttempts = 0;
do {
await new Promise((done) => setTimeout(done, 2000));
metrics = await this.getMetrics(session._id, sessionRequest._id);
retryAttempts++;
} while (!metrics.body.metrics.length && (retryAttempts < this.MAX_RETRY));
// Store in DB
} catch (err) {
}
}
public async getMetrics(sessionId: any, requestId: any): Promise <any> {
const url = this.config.backendUrl + "/check/metrics";
const options = {
uri: url,
headers: {
"X-IDCHECK-SESSION_ID": sessionId,
},
body: {},
json: true,
resolveWithFullResponse: true,
};
const metrics = await request.get(options);
return metrics;
}
I am calling processMetrics from a different async function. processMetrics is calling a backend (getMetrics) every 2 seconds, it will retry 10 times to see if the result is ready. If it is I will store something in the database and then return.