Minor changes to logic, was retrying after queue pop
This commit is contained in:
parent
0a28a20de7
commit
532169d748
2 changed files with 33 additions and 30 deletions
|
|
@ -7,17 +7,19 @@ const queue = new PQueue({ interval: 1000, intervalCap: 10 });
|
|||
|
||||
export async function fetchRate(pair) {
|
||||
const key = pair.toUpperCase();
|
||||
const max_retries = 3;
|
||||
|
||||
return queue.add(async () => {
|
||||
for (let attempt = 1; attempt <= 3; attempt++) {
|
||||
for (let attempt = 1; attempt <= max_retries; attempt++) {
|
||||
let timeout;
|
||||
try {
|
||||
const res = await queue.add(async () => {
|
||||
const controller = new AbortController();
|
||||
timeout = setTimeout(() => controller.abort(), 5000);
|
||||
|
||||
const res = await fetch(`https://api.uphold.com/v0/ticker/${key}`, {
|
||||
return fetch(`https://api.uphold.com/v0/ticker/${key}`, {
|
||||
signal: controller.signal,
|
||||
});
|
||||
});
|
||||
|
||||
clearTimeout(timeout);
|
||||
|
||||
|
|
@ -46,7 +48,6 @@ export async function fetchRate(pair) {
|
|||
await new Promise((r) => setTimeout(r, 500 * Math.pow(2, attempt - 1)));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export async function prefetchRates(pairs) {
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ export class Bot {
|
|||
this.lastPrice = price;
|
||||
logger.info(`[${this.pair}] Initial price: ${price.toFixed(2)}`);
|
||||
} else {
|
||||
// Using BigNumber
|
||||
const change = price
|
||||
.minus(this.lastPrice)
|
||||
.dividedBy(this.lastPrice)
|
||||
|
|
@ -69,6 +70,7 @@ export class Bot {
|
|||
};
|
||||
|
||||
try {
|
||||
// onAlert must be fast
|
||||
await this.onAlert(alertData);
|
||||
} catch (err) {
|
||||
logger.error(
|
||||
|
|
|
|||
Loading…
Reference in a new issue