-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
perf(webapp): throttle PAT + OAT lastAccessedAt writes to once per 5 min #3493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| area: webapp | ||
| type: improvement | ||
| --- | ||
|
|
||
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; | ||
|
|
||
| const { findFirstMock, updateManyMock } = vi.hoisted(() => ({ | ||
| findFirstMock: vi.fn(), | ||
| updateManyMock: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("~/db.server", () => ({ | ||
| prisma: { | ||
| organizationAccessToken: { | ||
| findFirst: findFirstMock, | ||
| updateMany: updateManyMock, | ||
| }, | ||
| }, | ||
| $replica: {}, | ||
| })); | ||
|
|
||
| vi.mock("~/utils/tokens.server", () => ({ | ||
| hashToken: (t: string) => `hashed:${t}`, | ||
| })); | ||
|
|
||
| vi.mock("./logger.server", () => ({ | ||
| logger: { warn: vi.fn(), error: vi.fn() }, | ||
| })); | ||
|
|
||
| import { | ||
| authenticateOrganizationAccessToken, | ||
| OAT_LAST_ACCESSED_THROTTLE_MS, | ||
| } from "~/services/organizationAccessToken.server"; | ||
|
|
||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| vi.setSystemTime(new Date("2026-01-01T00:00:00.000Z")); | ||
| findFirstMock.mockReset(); | ||
| updateManyMock.mockReset(); | ||
| updateManyMock.mockResolvedValue({ count: 1 }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| describe("authenticateOrganizationAccessToken — lastAccessedAt throttle", () => { | ||
| test("issues a conditional updateMany that skips writes when lastAccessedAt is recent", async () => { | ||
| findFirstMock.mockResolvedValueOnce({ | ||
| id: "oat_123", | ||
| organizationId: "org_1", | ||
| hashedToken: "hashed:tr_oat_validtoken", | ||
| }); | ||
|
|
||
| const result = await authenticateOrganizationAccessToken("tr_oat_validtoken"); | ||
|
|
||
| expect(result).toEqual({ organizationId: "org_1" }); | ||
| expect(updateManyMock).toHaveBeenCalledTimes(1); | ||
|
|
||
| const call = updateManyMock.mock.calls[0][0]; | ||
| expect(call.where.id).toBe("oat_123"); | ||
| expect(call.where.revokedAt).toBeNull(); | ||
| expect(call.data.lastAccessedAt).toBeInstanceOf(Date); | ||
|
|
||
| // The WHERE clause should require the existing lastAccessedAt to be null | ||
| // or strictly older than the throttle window — that's the entire point. | ||
| expect(call.where.OR).toEqual([ | ||
| { lastAccessedAt: null }, | ||
| { lastAccessedAt: { lt: expect.any(Date) } }, | ||
| ]); | ||
|
|
||
| // With fake timers, the cutoff lands exactly throttle-ms before "now". | ||
| const cutoff = call.where.OR[1].lastAccessedAt.lt as Date; | ||
| expect(cutoff.getTime()).toBe(Date.now() - OAT_LAST_ACCESSED_THROTTLE_MS); | ||
| }); | ||
|
|
||
| test("skips updateMany when token is not found", async () => { | ||
| findFirstMock.mockResolvedValueOnce(null); | ||
|
|
||
| const result = await authenticateOrganizationAccessToken("tr_oat_validtoken"); | ||
|
|
||
| expect(result).toBeUndefined(); | ||
| expect(updateManyMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test("skips updateMany when token doesn't start with prefix", async () => { | ||
| const result = await authenticateOrganizationAccessToken("not_an_oat"); | ||
|
|
||
| expect(result).toBeUndefined(); | ||
| expect(findFirstMock).not.toHaveBeenCalled(); | ||
| expect(updateManyMock).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; | ||
|
|
||
| const { findFirstMock, updateManyMock } = vi.hoisted(() => ({ | ||
| findFirstMock: vi.fn(), | ||
| updateManyMock: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("~/db.server", () => ({ | ||
| prisma: { | ||
| personalAccessToken: { | ||
| findFirst: findFirstMock, | ||
| updateMany: updateManyMock, | ||
| }, | ||
| }, | ||
| $replica: {}, | ||
| })); | ||
|
|
||
| vi.mock("~/env.server", () => ({ | ||
| env: { ENCRYPTION_KEY: "0".repeat(64) }, | ||
| })); | ||
|
|
||
| vi.mock("~/utils/tokens.server", () => ({ | ||
| hashToken: (t: string) => `hashed:${t}`, | ||
| encryptToken: () => ({ nonce: "n", ciphertext: "c", tag: "t" }), | ||
| decryptToken: () => "tr_pat_validtoken", | ||
| })); | ||
|
|
||
| vi.mock("./logger.server", () => ({ | ||
| logger: { warn: vi.fn(), error: vi.fn() }, | ||
| })); | ||
|
|
||
| import { | ||
| authenticatePersonalAccessToken, | ||
| PAT_LAST_ACCESSED_THROTTLE_MS, | ||
| } from "~/services/personalAccessToken.server"; | ||
|
|
||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| vi.setSystemTime(new Date("2026-01-01T00:00:00.000Z")); | ||
| findFirstMock.mockReset(); | ||
| updateManyMock.mockReset(); | ||
| updateManyMock.mockResolvedValue({ count: 1 }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| describe("authenticatePersonalAccessToken — lastAccessedAt throttle", () => { | ||
| test("issues a conditional updateMany that skips writes when lastAccessedAt is recent", async () => { | ||
| findFirstMock.mockResolvedValueOnce({ | ||
| id: "pat_123", | ||
| userId: "user_1", | ||
| hashedToken: "hashed:tr_pat_validtoken", | ||
| encryptedToken: { nonce: "n", ciphertext: "c", tag: "t" }, | ||
| }); | ||
|
|
||
| const result = await authenticatePersonalAccessToken("tr_pat_validtoken"); | ||
|
|
||
| expect(result).toEqual({ userId: "user_1" }); | ||
| expect(updateManyMock).toHaveBeenCalledTimes(1); | ||
|
|
||
| const call = updateManyMock.mock.calls[0][0]; | ||
| expect(call.where.id).toBe("pat_123"); | ||
| expect(call.where.revokedAt).toBeNull(); | ||
| expect(call.data.lastAccessedAt).toBeInstanceOf(Date); | ||
|
|
||
| // The WHERE clause should require the existing lastAccessedAt to be null | ||
| // or strictly older than the throttle window — that's the entire point. | ||
| expect(call.where.OR).toEqual([ | ||
| { lastAccessedAt: null }, | ||
| { lastAccessedAt: { lt: expect.any(Date) } }, | ||
| ]); | ||
|
|
||
| // With fake timers, the cutoff lands exactly throttle-ms before "now". | ||
| const cutoff = call.where.OR[1].lastAccessedAt.lt as Date; | ||
| expect(cutoff.getTime()).toBe(Date.now() - PAT_LAST_ACCESSED_THROTTLE_MS); | ||
| }); | ||
|
|
||
| test("skips updateMany when token is not found", async () => { | ||
| findFirstMock.mockResolvedValueOnce(null); | ||
|
|
||
| const result = await authenticatePersonalAccessToken("tr_pat_validtoken"); | ||
|
|
||
| expect(result).toBeUndefined(); | ||
| expect(updateManyMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test("skips updateMany when token doesn't start with prefix", async () => { | ||
| const result = await authenticatePersonalAccessToken("not_a_pat"); | ||
|
|
||
| expect(result).toBeUndefined(); | ||
| expect(findFirstMock).not.toHaveBeenCalled(); | ||
| expect(updateManyMock).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.