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) => { auth.reportUsage(chargeAmount, 'reader-crawl').catch((err) => {
this.logger.warn(`Unable to report usage for ${uid}`, { err: marshalErrorLike(err) }); this.logger.warn(`Unable to report usage for ${uid}`, { err: marshalErrorLike(err) });
}); });
apiRoll._ref?.set({ apiRoll.chargeAmount = chargeAmount;
chargeAmount,
}, { merge: true }).catch((err) => this.logger.warn(`Failed to log charge amount in apiRoll`, { err }));
} }
}); });
} else if (ctx.req.ip) { } else if (ctx.req.ip) {
@ -724,7 +722,7 @@ ${this.content}
} }
} }
getChargeAmount(formatted: { [k: string]: any; }) { getChargeAmount(formatted: FormattedPage) {
if (!formatted) { if (!formatted) {
return undefined; return undefined;
} }
@ -735,7 +733,7 @@ ${this.content}
return estimateToken(textContent); return estimateToken(textContent);
} }
const imageContent = formatted.screenshotUrl || formatted?.screenshot; const imageContent = formatted.screenshotUrl || (formatted as any)?.screenshot;
if (imageContent) { if (imageContent) {
// OpenAI image token count for 1024x1024 image // 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) => { auth.reportUsage(chargeAmount, 'reader-search').catch((err) => {
this.logger.warn(`Unable to report usage for ${uid}`, { err: marshalErrorLike(err) }); this.logger.warn(`Unable to report usage for ${uid}`, { err: marshalErrorLike(err) });
}); });
apiRoll._ref?.set({ apiRoll.chargeAmount = chargeAmount;
chargeAmount,
}, { merge: true }).catch((err) => this.logger.warn(`Failed to log charge amount in apiRoll`, { err }));
} }
}); });
} else if (ctx.req.ip) { } else if (ctx.req.ip) {
@ -184,9 +182,7 @@ export class SearcherHost extends RPCHost {
); );
rpcReflect.finally(() => { rpcReflect.finally(() => {
if (chargeAmount) { if (chargeAmount) {
apiRoll._ref?.set({ apiRoll.chargeAmount = chargeAmount;
chargeAmount,
}, { merge: true }).catch((err) => this.logger.warn(`Failed to log charge amount in apiRoll`, { err }));
} }
}); });
} }
@ -415,7 +411,7 @@ ${this.content}
return resultArray; return resultArray;
} }
getChargeAmount(formatted: any[]) { getChargeAmount(formatted: FormattedPage[]) {
return _.sum( return _.sum(
formatted.map((x) => this.crawler.getChargeAmount(x) || 0) formatted.map((x) => this.crawler.getChargeAmount(x) || 0)
); );

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