feat: 支持多实例客户端异常限流
This commit is contained in:
1 parent
4cd3749806
commit
2fe108d66a
18 files changed
+432
-90
No files matched your search
@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { HttpException } from "@nestjs/common";
|
||||
import { AdminClientErrorService } from "../src/modules/admin/admin-client-error.service.js";
|
||||
import { MemoryClientErrorThrottleStore } from "../src/modules/admin/client-error-throttle.store.js";
|
||||
|
||||
function report(errorId: string, subject = "admin:user-1") {
|
||||
return {
|
||||
@@ -15,40 +16,42 @@ function report(errorId: string, subject = "admin:user-1") {
|
||||
};
|
||||
}
|
||||
|
||||
test("首次上报记录,相同 errorId 在窗口内按管理员去重", () => {
|
||||
const service = new AdminClientErrorService();
|
||||
assert.deepEqual(service.record(report("admin-error-1")), {
|
||||
function service() {
|
||||
return new AdminClientErrorService(new MemoryClientErrorThrottleStore());
|
||||
}
|
||||
|
||||
test("首次上报记录,相同 errorId 在窗口内按管理员去重", async () => {
|
||||
const errors = service();
|
||||
assert.deepEqual(await errors.record(report("admin-error-1")), {
|
||||
received: true,
|
||||
duplicate: false,
|
||||
});
|
||||
assert.deepEqual(service.record(report("admin-error-1")), {
|
||||
assert.deepEqual(await errors.record(report("admin-error-1")), {
|
||||
received: true,
|
||||
duplicate: true,
|
||||
});
|
||||
// 不同管理员使用相同 errorId 时互不影响
|
||||
assert.deepEqual(service.record(report("admin-error-1", "admin:user-2")), {
|
||||
assert.deepEqual(await errors.record(report("admin-error-1", "admin:user-2")), {
|
||||
received: true,
|
||||
duplicate: false,
|
||||
});
|
||||
});
|
||||
|
||||
test("同一管理员 60 秒内最多接受 10 条不同错误", () => {
|
||||
const service = new AdminClientErrorService();
|
||||
test("同一管理员 60 秒内最多接受 10 条不同错误", async () => {
|
||||
const errors = service();
|
||||
for (let index = 0; index < 10; index += 1) {
|
||||
assert.deepEqual(service.record(report(`admin-rate-${index}`)), {
|
||||
assert.deepEqual(await errors.record(report(`admin-rate-${index}`)), {
|
||||
received: true,
|
||||
duplicate: false,
|
||||
});
|
||||
}
|
||||
try {
|
||||
service.record(report("admin-rate-overflow"));
|
||||
assert.fail("应当触发限流");
|
||||
} catch (error) {
|
||||
await assert.rejects(errors.record(report("admin-rate-overflow")), (error) => {
|
||||
assert.ok(error instanceof HttpException);
|
||||
assert.equal(error.getStatus(), 429);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
// 另一个管理员不受影响
|
||||
assert.deepEqual(service.record(report("admin-rate-other", "admin:user-2")), {
|
||||
assert.deepEqual(await errors.record(report("admin-rate-other", "admin:user-2")), {
|
||||
received: true,
|
||||
duplicate: false,
|
||||
});
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { createClientErrorThrottleStore } from "../src/modules/admin/client-error-throttle.factory.js";
|
||||
import { PostgresClientErrorThrottleStore } from "../src/modules/admin/client-error-throttle.postgres.store.js";
|
||||
import { MemoryClientErrorThrottleStore } from "../src/modules/admin/client-error-throttle.store.js";
|
||||
|
||||
const prisma = {} as never;
|
||||
|
||||
test("默认使用进程内存储", () => {
|
||||
const store = createClientErrorThrottleStore(prisma, "memory");
|
||||
assert.ok(store instanceof MemoryClientErrorThrottleStore);
|
||||
});
|
||||
|
||||
test("多实例可切换为 Postgres 共享存储", () => {
|
||||
const store = createClientErrorThrottleStore(prisma, "postgres");
|
||||
assert.ok(store instanceof PostgresClientErrorThrottleStore);
|
||||
});
|
||||
|
||||
test("未知存储模式在启动时快速失败", () => {
|
||||
assert.throws(
|
||||
() => createClientErrorThrottleStore(prisma, "redis"),
|
||||
/Unsupported CLIENT_ERROR_THROTTLE_STORE/,
|
||||
);
|
||||
});
|
||||
@@ -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 };
|
||||
|
||||
@@ -45,7 +45,6 @@ async function main() {
|
||||
.filter((entry) => entry.isDirectory() && entry.name !== targetMigration)
|
||||
.map((entry) => entry.name)
|
||||
.sort();
|
||||
assert.equal(migrationDirs.at(-1), "20261019100000_remove_brand_profile_defaults");
|
||||
await admin.$executeRawUnsafe(`CREATE SCHEMA "${schema}"`);
|
||||
createdSchema = true;
|
||||
|
||||
|
||||
Reference in new issue
Block a user