feat: 支持多实例客户端异常限流
This commit is contained in:
1 parent
4cd3749806
commit
2fe108d66a
18 files changed
+432
-90
No files matched your search
@@ -12,6 +12,11 @@ import { createApp } from "../dist/main.js";
|
||||
import { readIdentity, signIdentity } from "../dist/auth.js";
|
||||
import { WechatAuthService } from "../dist/wechat-auth.js";
|
||||
import { AdminMiniAppReleaseService } from "../dist/modules/admin/admin-miniapp-release.service.js";
|
||||
import { PostgresClientErrorThrottleStore } from "../dist/modules/admin/client-error-throttle.postgres.store.js";
|
||||
import {
|
||||
CLIENT_ERROR_DEDUPE_TTL_MS,
|
||||
CLIENT_ERROR_RATE_WINDOW_MS,
|
||||
} from "../dist/modules/admin/client-error-throttle.store.js";
|
||||
|
||||
let app;
|
||||
const prisma = new PrismaClient();
|
||||
@@ -1928,6 +1933,88 @@ test("admin client error reports are authenticated, validated, and deduplicated"
|
||||
assert.equal(invalid.statusCode, 400);
|
||||
});
|
||||
|
||||
test("postgres client error store deduplicates and rate limits across instances", async () => {
|
||||
const storeA = new PostgresClientErrorThrottleStore(prisma);
|
||||
const storeB = new PostgresClientErrorThrottleStore(prisma);
|
||||
const subjectPrefix = `pg-client-error-${randomUUID()}`;
|
||||
const dedupeSubject = `${subjectPrefix}-dedupe`;
|
||||
const concurrentSubject = `${subjectPrefix}-concurrent`;
|
||||
const rateSubject = `${subjectPrefix}-rate`;
|
||||
const lateSubject = `${subjectPrefix}-late`;
|
||||
const now = Date.now();
|
||||
try {
|
||||
assert.deepEqual(await storeA.record({ subject: dedupeSubject, errorId: "same" }, now), {
|
||||
duplicate: false,
|
||||
});
|
||||
assert.deepEqual(await storeB.record({ subject: dedupeSubject, errorId: "same" }, now), {
|
||||
duplicate: true,
|
||||
});
|
||||
assert.deepEqual(
|
||||
await storeB.record(
|
||||
{ subject: dedupeSubject, errorId: "same" },
|
||||
now + CLIENT_ERROR_DEDUPE_TTL_MS + 1_000,
|
||||
),
|
||||
{ duplicate: false },
|
||||
);
|
||||
|
||||
const concurrent = await Promise.all(
|
||||
Array.from({ length: 6 }, (_, index) =>
|
||||
(index % 2 === 0 ? storeA : storeB).record(
|
||||
{ subject: concurrentSubject, errorId: "concurrent-same" },
|
||||
now,
|
||||
),
|
||||
),
|
||||
);
|
||||
assert.equal(concurrent.filter((result) => !result.duplicate).length, 1);
|
||||
|
||||
for (let index = 0; index < 10; index += 1) {
|
||||
const store = index % 2 === 0 ? storeA : storeB;
|
||||
assert.deepEqual(
|
||||
await store.record({ subject: rateSubject, errorId: `rate-${index}` }, now),
|
||||
{ duplicate: false },
|
||||
);
|
||||
}
|
||||
await assert.rejects(
|
||||
storeB.record({ subject: rateSubject, errorId: "rate-overflow" }, now),
|
||||
(error) => error.getStatus?.() === 429,
|
||||
);
|
||||
assert.deepEqual(
|
||||
await storeA.record(
|
||||
{ subject: rateSubject, errorId: "rate-next-window" },
|
||||
now + CLIENT_ERROR_RATE_WINDOW_MS,
|
||||
),
|
||||
{ duplicate: false },
|
||||
);
|
||||
|
||||
// 旧窗口的迟到请求不得把窗口指针回拨,否则会额外放行一整窗口。
|
||||
await storeA.record(
|
||||
{ subject: lateSubject, errorId: "late-first" },
|
||||
now + CLIENT_ERROR_RATE_WINDOW_MS,
|
||||
);
|
||||
await storeB.record({ subject: lateSubject, errorId: "late-old" }, now);
|
||||
for (let index = 0; index < 9; index += 1) {
|
||||
assert.deepEqual(
|
||||
await storeA.record(
|
||||
{ subject: lateSubject, errorId: `late-next-${index}` },
|
||||
now + CLIENT_ERROR_RATE_WINDOW_MS,
|
||||
),
|
||||
{ duplicate: false },
|
||||
);
|
||||
}
|
||||
await assert.rejects(
|
||||
storeB.record(
|
||||
{ subject: lateSubject, errorId: "late-overflow" },
|
||||
now + CLIENT_ERROR_RATE_WINDOW_MS,
|
||||
),
|
||||
(error) => error.getStatus?.() === 429,
|
||||
);
|
||||
} finally {
|
||||
const subjects = [dedupeSubject, concurrentSubject, rateSubject, lateSubject];
|
||||
await prisma.clientErrorDedupe.deleteMany({ where: { subject: { in: subjects } } });
|
||||
await prisma.clientErrorThrottleWindow.deleteMany({ where: { subject: { in: subjects } } });
|
||||
}
|
||||
});
|
||||
|
||||
async function assertProtectedRouteBoundary(url, options = {}) {
|
||||
const { method = "GET", payload, forbiddenRole = "customer", headers = {} } = options;
|
||||
const requestOptions = payload === undefined ? { method, headers } : { method, headers, payload };
|
||||
|
||||
Reference in new issue
Block a user