mirror of
https://github.com/documenso/documenso.git
synced 2026-08-25 15:52:29 +10:00
fix: reduce write throughput
This commit is contained in:
@@ -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,10 +99,15 @@ export const getApiTokenByToken = async ({ token, bypassRateLimit = false }: Get
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
void prisma.apiToken
|
||||||
.update({
|
.updateMany({
|
||||||
where: {
|
where: {
|
||||||
id: apiToken.id,
|
id: apiToken.id,
|
||||||
|
// Optimistic guard: skip if another request beat us
|
||||||
|
lastUsedAt: apiToken.lastUsedAt,
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
lastUsedAt: new Date(),
|
lastUsedAt: new Date(),
|
||||||
@@ -113,6 +120,7 @@ export const getApiTokenByToken = async ({ token, bypassRateLimit = false }: Get
|
|||||||
err,
|
err,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...apiToken,
|
...apiToken,
|
||||||
|
|||||||
Reference in New Issue
Block a user