fix: race condition while logging chargeAmount

This commit is contained in:
yanlong.wang 2024-05-16 18:26:18 +08:00
parent 6f4819bc49
commit 8b0916f858
No known key found for this signature in database
GPG Key ID: C0A623C0BADF9F37
3 changed files with 7 additions and 13 deletions

View File

@ -425,9 +425,7 @@ ${this.content}
auth.reportUsage(chargeAmount, 'reader-crawl').catch((err) => {
this.logger.warn(`Unable to report usage for ${uid}`, { err: marshalErrorLike(err) });
});
apiRoll._ref?.set({
chargeAmount,
}, { merge: true }).catch((err) => this.logger.warn(`Failed to log charge amount in apiRoll`, { err }));
apiRoll.chargeAmount = chargeAmount;
}
});
} else if (ctx.req.ip) {
@ -724,7 +722,7 @@ ${this.content}
}
}
getChargeAmount(formatted: { [k: string]: any; }) {
getChargeAmount(formatted: FormattedPage) {
if (!formatted) {
return undefined;
}
@ -735,7 +733,7 @@ ${this.content}
return estimateToken(textContent);
}
const imageContent = formatted.screenshotUrl || formatted?.screenshot;
const imageContent = formatted.screenshotUrl || (formatted as any)?.screenshot;
if (imageContent) {
// OpenAI image token count for 1024x1024 image

View File

@ -169,9 +169,7 @@ export class SearcherHost extends RPCHost {
auth.reportUsage(chargeAmount, 'reader-search').catch((err) => {
this.logger.warn(`Unable to report usage for ${uid}`, { err: marshalErrorLike(err) });
});
apiRoll._ref?.set({
chargeAmount,
}, { merge: true }).catch((err) => this.logger.warn(`Failed to log charge amount in apiRoll`, { err }));
apiRoll.chargeAmount = chargeAmount;
}
});
} else if (ctx.req.ip) {
@ -184,9 +182,7 @@ export class SearcherHost extends RPCHost {
);
rpcReflect.finally(() => {
if (chargeAmount) {
apiRoll._ref?.set({
chargeAmount,
}, { merge: true }).catch((err) => this.logger.warn(`Failed to log charge amount in apiRoll`, { err }));
apiRoll.chargeAmount = chargeAmount;
}
});
}
@ -415,7 +411,7 @@ ${this.content}
return resultArray;
}
getChargeAmount(formatted: any[]) {
getChargeAmount(formatted: FormattedPage[]) {
return _.sum(
formatted.map((x) => this.crawler.getChargeAmount(x) || 0)
);

@ -1 +1 @@
Subproject commit 2f2cdcff7b2738be33ee5aca858ef2d65eba29ed
Subproject commit fc3545e3a7ae27968e69f351f109d3ffb535f963