Skip to content

Commit 04bdf4b

Browse files
authored
perf(webapp): throttle PAT + OAT lastAccessedAt writes to once per 5 min (#3493)
## Summary Each successful PAT (`PersonalAccessToken`) or OAT (`OrganizationAccessToken`) authentication issues a `prisma.X.update({ lastAccessedAt: new Date() })` to bump the timestamp. For tokens used at high frequency (CLI clients, integrations) this generates a per-request DB write that is mostly redundant — the `lastAccessedAt` field is only surfaced on the settings page so users can decide which tokens to revoke, and "within the last 5 minutes" is plenty of granularity for that. ## Design Replace each unconditional `update` with a conditional `updateMany` whose `WHERE` requires the existing `lastAccessedAt` to be `NULL` or strictly older than 5 minutes: ```ts await prisma.personalAccessToken.updateMany({ where: { id: personalAccessToken.id, OR: [ { lastAccessedAt: null }, { lastAccessedAt: { lt: new Date(Date.now() - PAT_LAST_ACCESSED_THROTTLE_MS) } }, ], }, data: { lastAccessedAt: new Date() }, }); ``` The conditional runs inside the SQL `UPDATE`, so concurrent auths can't race into a double-write. No schema change. No migration. No new infrastructure. Throttle is a hardcoded constant (`5 * 60 * 1000`) — easy to revisit. ## Test plan - [x] `pnpm run typecheck --filter webapp` - [x] `pnpm vitest run ./test/services/personalAccessToken.test.ts ./test/services/organizationAccessToken.test.ts` — 6/6 pass, verifying the throttle `WHERE` clause is constructed correctly and the `update` is skipped on token-not-found / wrong-prefix paths
1 parent 1acdc50 commit 04bdf4b

5 files changed

Lines changed: 225 additions & 2 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
area: webapp
3+
type: improvement
4+
---
5+
6+
Throttle `PersonalAccessToken.lastAccessedAt` and `OrganizationAccessToken.lastAccessedAt` writes to at most once per 5 minutes per token. Eliminates ~95% of writes on two narrow hot tables that were autovacuuming every ~5 minutes — same denormalization-on-the-hot-path shape as the schedule engine fix in TRI-8891. The settings UI continues to display "last used" with at most 5-minute lag.

apps/webapp/app/services/organizationAccessToken.server.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ const tokenValueLength = 40;
88
//lowercase only, removed 0 and l to avoid confusion
99
const tokenGenerator = customAlphabet("123456789abcdefghijkmnopqrstuvwxyz", tokenValueLength);
1010

11+
// Skip the lastAccessedAt write if the existing value is already within this
12+
// window. Eliminates per-auth UPDATE churn on a small narrow hot table; the
13+
// settings UI reads this field at human granularity so a few-minute
14+
// staleness is fine.
15+
export const OAT_LAST_ACCESSED_THROTTLE_MS = 5 * 60 * 1000;
16+
1117
type CreateOrganizationAccessTokenOptions = {
1218
name: string;
1319
organizationId: string;
@@ -105,9 +111,19 @@ export async function authenticateOrganizationAccessToken(
105111
return;
106112
}
107113

108-
await prisma.organizationAccessToken.update({
114+
// Conditional updateMany — only writes if the existing lastAccessedAt is
115+
// null or older than the throttle window. The WHERE runs inside the UPDATE
116+
// so concurrent auths don't race into a double-write. `revokedAt: null`
117+
// matches the findFirst guard above so a token revoked between the read
118+
// and write doesn't get a stale lastAccessedAt update.
119+
await prisma.organizationAccessToken.updateMany({
109120
where: {
110121
id: organizationAccessToken.id,
122+
revokedAt: null,
123+
OR: [
124+
{ lastAccessedAt: null },
125+
{ lastAccessedAt: { lt: new Date(Date.now() - OAT_LAST_ACCESSED_THROTTLE_MS) } },
126+
],
111127
},
112128
data: {
113129
lastAccessedAt: new Date(),

apps/webapp/app/services/personalAccessToken.server.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ const tokenValueLength = 40;
1010
//lowercase only, removed 0 and l to avoid confusion
1111
const tokenGenerator = customAlphabet("123456789abcdefghijkmnopqrstuvwxyz", tokenValueLength);
1212

13+
// Skip the lastAccessedAt write if the existing value is already within this
14+
// window. Eliminates per-auth UPDATE churn on a small narrow hot table; the
15+
// /account/tokens UI reads this field at human granularity so a few-minute
16+
// staleness is fine.
17+
export const PAT_LAST_ACCESSED_THROTTLE_MS = 5 * 60 * 1000;
18+
1319
type CreatePersonalAccessTokenOptions = {
1420
name: string;
1521
userId: string;
@@ -205,9 +211,19 @@ export async function authenticatePersonalAccessToken(
205211
return;
206212
}
207213

208-
await prisma.personalAccessToken.update({
214+
// Conditional updateMany — only writes if the existing lastAccessedAt is
215+
// null or older than the throttle window. The WHERE runs inside the UPDATE
216+
// so concurrent auths don't race into a double-write. `revokedAt: null`
217+
// matches the findFirst guard above so a token revoked between the read
218+
// and write doesn't get a stale lastAccessedAt update.
219+
await prisma.personalAccessToken.updateMany({
209220
where: {
210221
id: personalAccessToken.id,
222+
revokedAt: null,
223+
OR: [
224+
{ lastAccessedAt: null },
225+
{ lastAccessedAt: { lt: new Date(Date.now() - PAT_LAST_ACCESSED_THROTTLE_MS) } },
226+
],
211227
},
212228
data: {
213229
lastAccessedAt: new Date(),
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
2+
3+
const { findFirstMock, updateManyMock } = vi.hoisted(() => ({
4+
findFirstMock: vi.fn(),
5+
updateManyMock: vi.fn(),
6+
}));
7+
8+
vi.mock("~/db.server", () => ({
9+
prisma: {
10+
organizationAccessToken: {
11+
findFirst: findFirstMock,
12+
updateMany: updateManyMock,
13+
},
14+
},
15+
$replica: {},
16+
}));
17+
18+
vi.mock("~/utils/tokens.server", () => ({
19+
hashToken: (t: string) => `hashed:${t}`,
20+
}));
21+
22+
vi.mock("./logger.server", () => ({
23+
logger: { warn: vi.fn(), error: vi.fn() },
24+
}));
25+
26+
import {
27+
authenticateOrganizationAccessToken,
28+
OAT_LAST_ACCESSED_THROTTLE_MS,
29+
} from "~/services/organizationAccessToken.server";
30+
31+
beforeEach(() => {
32+
vi.useFakeTimers();
33+
vi.setSystemTime(new Date("2026-01-01T00:00:00.000Z"));
34+
findFirstMock.mockReset();
35+
updateManyMock.mockReset();
36+
updateManyMock.mockResolvedValue({ count: 1 });
37+
});
38+
39+
afterEach(() => {
40+
vi.useRealTimers();
41+
});
42+
43+
describe("authenticateOrganizationAccessToken — lastAccessedAt throttle", () => {
44+
test("issues a conditional updateMany that skips writes when lastAccessedAt is recent", async () => {
45+
findFirstMock.mockResolvedValueOnce({
46+
id: "oat_123",
47+
organizationId: "org_1",
48+
hashedToken: "hashed:tr_oat_validtoken",
49+
});
50+
51+
const result = await authenticateOrganizationAccessToken("tr_oat_validtoken");
52+
53+
expect(result).toEqual({ organizationId: "org_1" });
54+
expect(updateManyMock).toHaveBeenCalledTimes(1);
55+
56+
const call = updateManyMock.mock.calls[0][0];
57+
expect(call.where.id).toBe("oat_123");
58+
expect(call.where.revokedAt).toBeNull();
59+
expect(call.data.lastAccessedAt).toBeInstanceOf(Date);
60+
61+
// The WHERE clause should require the existing lastAccessedAt to be null
62+
// or strictly older than the throttle window — that's the entire point.
63+
expect(call.where.OR).toEqual([
64+
{ lastAccessedAt: null },
65+
{ lastAccessedAt: { lt: expect.any(Date) } },
66+
]);
67+
68+
// With fake timers, the cutoff lands exactly throttle-ms before "now".
69+
const cutoff = call.where.OR[1].lastAccessedAt.lt as Date;
70+
expect(cutoff.getTime()).toBe(Date.now() - OAT_LAST_ACCESSED_THROTTLE_MS);
71+
});
72+
73+
test("skips updateMany when token is not found", async () => {
74+
findFirstMock.mockResolvedValueOnce(null);
75+
76+
const result = await authenticateOrganizationAccessToken("tr_oat_validtoken");
77+
78+
expect(result).toBeUndefined();
79+
expect(updateManyMock).not.toHaveBeenCalled();
80+
});
81+
82+
test("skips updateMany when token doesn't start with prefix", async () => {
83+
const result = await authenticateOrganizationAccessToken("not_an_oat");
84+
85+
expect(result).toBeUndefined();
86+
expect(findFirstMock).not.toHaveBeenCalled();
87+
expect(updateManyMock).not.toHaveBeenCalled();
88+
});
89+
});
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
2+
3+
const { findFirstMock, updateManyMock } = vi.hoisted(() => ({
4+
findFirstMock: vi.fn(),
5+
updateManyMock: vi.fn(),
6+
}));
7+
8+
vi.mock("~/db.server", () => ({
9+
prisma: {
10+
personalAccessToken: {
11+
findFirst: findFirstMock,
12+
updateMany: updateManyMock,
13+
},
14+
},
15+
$replica: {},
16+
}));
17+
18+
vi.mock("~/env.server", () => ({
19+
env: { ENCRYPTION_KEY: "0".repeat(64) },
20+
}));
21+
22+
vi.mock("~/utils/tokens.server", () => ({
23+
hashToken: (t: string) => `hashed:${t}`,
24+
encryptToken: () => ({ nonce: "n", ciphertext: "c", tag: "t" }),
25+
decryptToken: () => "tr_pat_validtoken",
26+
}));
27+
28+
vi.mock("./logger.server", () => ({
29+
logger: { warn: vi.fn(), error: vi.fn() },
30+
}));
31+
32+
import {
33+
authenticatePersonalAccessToken,
34+
PAT_LAST_ACCESSED_THROTTLE_MS,
35+
} from "~/services/personalAccessToken.server";
36+
37+
beforeEach(() => {
38+
vi.useFakeTimers();
39+
vi.setSystemTime(new Date("2026-01-01T00:00:00.000Z"));
40+
findFirstMock.mockReset();
41+
updateManyMock.mockReset();
42+
updateManyMock.mockResolvedValue({ count: 1 });
43+
});
44+
45+
afterEach(() => {
46+
vi.useRealTimers();
47+
});
48+
49+
describe("authenticatePersonalAccessToken — lastAccessedAt throttle", () => {
50+
test("issues a conditional updateMany that skips writes when lastAccessedAt is recent", async () => {
51+
findFirstMock.mockResolvedValueOnce({
52+
id: "pat_123",
53+
userId: "user_1",
54+
hashedToken: "hashed:tr_pat_validtoken",
55+
encryptedToken: { nonce: "n", ciphertext: "c", tag: "t" },
56+
});
57+
58+
const result = await authenticatePersonalAccessToken("tr_pat_validtoken");
59+
60+
expect(result).toEqual({ userId: "user_1" });
61+
expect(updateManyMock).toHaveBeenCalledTimes(1);
62+
63+
const call = updateManyMock.mock.calls[0][0];
64+
expect(call.where.id).toBe("pat_123");
65+
expect(call.where.revokedAt).toBeNull();
66+
expect(call.data.lastAccessedAt).toBeInstanceOf(Date);
67+
68+
// The WHERE clause should require the existing lastAccessedAt to be null
69+
// or strictly older than the throttle window — that's the entire point.
70+
expect(call.where.OR).toEqual([
71+
{ lastAccessedAt: null },
72+
{ lastAccessedAt: { lt: expect.any(Date) } },
73+
]);
74+
75+
// With fake timers, the cutoff lands exactly throttle-ms before "now".
76+
const cutoff = call.where.OR[1].lastAccessedAt.lt as Date;
77+
expect(cutoff.getTime()).toBe(Date.now() - PAT_LAST_ACCESSED_THROTTLE_MS);
78+
});
79+
80+
test("skips updateMany when token is not found", async () => {
81+
findFirstMock.mockResolvedValueOnce(null);
82+
83+
const result = await authenticatePersonalAccessToken("tr_pat_validtoken");
84+
85+
expect(result).toBeUndefined();
86+
expect(updateManyMock).not.toHaveBeenCalled();
87+
});
88+
89+
test("skips updateMany when token doesn't start with prefix", async () => {
90+
const result = await authenticatePersonalAccessToken("not_a_pat");
91+
92+
expect(result).toBeUndefined();
93+
expect(findFirstMock).not.toHaveBeenCalled();
94+
expect(updateManyMock).not.toHaveBeenCalled();
95+
});
96+
});

0 commit comments

Comments
 (0)