fix: reduce write throughput

This commit is contained in:
David Nguyen
2026-08-25 12:57:57 +10:00
parent 73eb5af042
commit 443807b529
@@ -5,6 +5,8 @@ import { logger } from '../../utils/logger';
import { hashString } from '../auth/hash'; import { hashString } from '../auth/hash';
import { assertOrganisationRatesAndLimits } from '../rate-limit/assert-organisation-rates-and-limits'; import { assertOrganisationRatesAndLimits } from '../rate-limit/assert-organisation-rates-and-limits';
const LAST_USED_AT_UPDATE_INTERVAL = 60_000; // 1 minute
type GetApiTokenByTokenOptions = { type GetApiTokenByTokenOptions = {
token: string; token: string;
@@ -97,22 +99,28 @@ export const getApiTokenByToken = async ({ token, bypassRateLimit = false }: Get
}); });
} }
void prisma.apiToken // Only update the lastUsedAt after X amount of time has passed to reduce
.update({ // the number of writes to the database
where: { if (!apiToken.lastUsedAt || apiToken.lastUsedAt.getTime() < Date.now() - LAST_USED_AT_UPDATE_INTERVAL) {
id: apiToken.id, void prisma.apiToken
}, .updateMany({
data: { where: {
lastUsedAt: new Date(), id: apiToken.id,
}, // Optimistic guard: skip if another request beat us
}) lastUsedAt: apiToken.lastUsedAt,
.catch((err) => { },
logger.warn({ data: {
msg: 'Failed to update API token lastUsedAt', lastUsedAt: new Date(),
apiTokenId: apiToken.id, },
err, })
.catch((err) => {
logger.warn({
msg: 'Failed to update API token lastUsedAt',
apiTokenId: apiToken.id,
err,
});
}); });
}); }
return { return {
...apiToken, ...apiToken,