From 443807b529f43a2129885ae9676c106130acc107 Mon Sep 17 00:00:00 2001 From: David Nguyen Date: Tue, 25 Aug 2026 12:57:57 +1000 Subject: [PATCH] fix: reduce write throughput --- .../public-api/get-api-token-by-token.ts | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/packages/lib/server-only/public-api/get-api-token-by-token.ts b/packages/lib/server-only/public-api/get-api-token-by-token.ts index d6f35b887..099029a6c 100644 --- a/packages/lib/server-only/public-api/get-api-token-by-token.ts +++ b/packages/lib/server-only/public-api/get-api-token-by-token.ts @@ -5,6 +5,8 @@ import { logger } from '../../utils/logger'; import { hashString } from '../auth/hash'; import { assertOrganisationRatesAndLimits } from '../rate-limit/assert-organisation-rates-and-limits'; +const LAST_USED_AT_UPDATE_INTERVAL = 60_000; // 1 minute + type GetApiTokenByTokenOptions = { token: string; @@ -97,22 +99,28 @@ export const getApiTokenByToken = async ({ token, bypassRateLimit = false }: Get }); } - void prisma.apiToken - .update({ - where: { - id: apiToken.id, - }, - data: { - lastUsedAt: new Date(), - }, - }) - .catch((err) => { - logger.warn({ - msg: 'Failed to update API token lastUsedAt', - apiTokenId: apiToken.id, - err, + // Only update the lastUsedAt after X amount of time has passed to reduce + // the number of writes to the database + if (!apiToken.lastUsedAt || apiToken.lastUsedAt.getTime() < Date.now() - LAST_USED_AT_UPDATE_INTERVAL) { + void prisma.apiToken + .updateMany({ + where: { + id: apiToken.id, + // Optimistic guard: skip if another request beat us + lastUsedAt: apiToken.lastUsedAt, + }, + data: { + lastUsedAt: new Date(), + }, + }) + .catch((err) => { + logger.warn({ + msg: 'Failed to update API token lastUsedAt', + apiTokenId: apiToken.id, + err, + }); }); - }); + } return { ...apiToken,