mirror of
https://github.com/outline/outline.git
synced 2026-11-21 05:24:03 +03:00
* fix: Make count-based limit checks reliable under concurrency Limits enforced by counting rows before an insert could be exceeded by requests that ran at the same time, as each one read the same count before any of them had written. Add a LockHelper that takes a Postgres advisory lock for the remainder of the transaction, and use it to serialize the check for webhook subscriptions, team domains, share subscriptions and pins. The counts now also run on the transaction so they include rows written earlier in the same request. * Remove excessive tests * Also guard simultaneous row removal
63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import { sleep } from "@shared/utils/timers";
|
|
import { sequelize } from "./database";
|
|
import { LockHelper } from "./LockHelper";
|
|
|
|
describe("LockHelper", () => {
|
|
describe("acquire", () => {
|
|
it("should block another transaction holding the same lock", async () => {
|
|
const events: string[] = [];
|
|
let release: () => void = () => undefined;
|
|
const held = new Promise<void>((resolve) => {
|
|
release = resolve;
|
|
});
|
|
|
|
const first = sequelize.transaction(async (transaction) => {
|
|
await LockHelper.acquire(sequelize, "limit:one", transaction);
|
|
events.push("first");
|
|
await held;
|
|
});
|
|
|
|
while (!events.length) {
|
|
await sleep(10);
|
|
}
|
|
|
|
const second = sequelize.transaction(async (transaction) => {
|
|
await LockHelper.acquire(sequelize, "limit:one", transaction);
|
|
events.push("second");
|
|
});
|
|
|
|
await sleep(250);
|
|
expect(events).toEqual(["first"]);
|
|
|
|
release();
|
|
await Promise.all([first, second]);
|
|
expect(events).toEqual(["first", "second"]);
|
|
});
|
|
|
|
it("should not block a transaction holding a different lock", async () => {
|
|
let release: () => void = () => undefined;
|
|
const held = new Promise<void>((resolve) => {
|
|
release = resolve;
|
|
});
|
|
|
|
const first = sequelize.transaction(async (transaction) => {
|
|
await LockHelper.acquire(sequelize, "limit:two", transaction);
|
|
await held;
|
|
});
|
|
|
|
await sequelize.transaction(async (transaction) => {
|
|
await LockHelper.acquire(sequelize, "limit:three", transaction);
|
|
});
|
|
|
|
release();
|
|
await first;
|
|
});
|
|
|
|
it("should not throw without a transaction", async () => {
|
|
await expect(
|
|
LockHelper.acquire(sequelize, "limit:four")
|
|
).resolves.toBeUndefined();
|
|
});
|
|
});
|
|
});
|