mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-21 10:16:44 +00:00
fix(feishu): bound sender name cache (#103513)
This commit is contained in:
@@ -65,4 +65,37 @@ describe("resolveFeishuSenderName", () => {
|
||||
|
||||
expect(get).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("evicts the oldest sender while retaining recent sender names", async () => {
|
||||
const get = vi.fn(async (params: { path: { user_id: string } }) => ({
|
||||
data: { user: { name: `name-${params.path.user_id}` } },
|
||||
}));
|
||||
createFeishuClientMock.mockReturnValue({ contact: { user: { get } } });
|
||||
|
||||
for (let index = 0; index < 501; index += 1) {
|
||||
await resolveFeishuSenderName({
|
||||
account,
|
||||
senderId: `ou_sender_cap_${index}`,
|
||||
log: vi.fn(),
|
||||
});
|
||||
}
|
||||
await resolveFeishuSenderName({
|
||||
account,
|
||||
senderId: "ou_sender_cap_0",
|
||||
log: vi.fn(),
|
||||
});
|
||||
await resolveFeishuSenderName({
|
||||
account,
|
||||
senderId: "ou_sender_cap_500",
|
||||
log: vi.fn(),
|
||||
});
|
||||
|
||||
expect(get).toHaveBeenCalledTimes(502);
|
||||
expect(
|
||||
get.mock.calls.filter(([params]) => params.path.user_id === "ou_sender_cap_0"),
|
||||
).toHaveLength(2);
|
||||
expect(
|
||||
get.mock.calls.filter(([params]) => params.path.user_id === "ou_sender_cap_500"),
|
||||
).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
// Feishu plugin module implements bot sender name behavior.
|
||||
import { pruneMapToMaxSize } from "openclaw/plugin-sdk/collection-runtime";
|
||||
import {
|
||||
asDateTimestampMs,
|
||||
resolveExpiresAtMsFromDurationMs,
|
||||
@@ -29,6 +30,7 @@ const FEISHU_SCOPE_CORRECTIONS: Record<string, string> = {
|
||||
"contact:contact.base:readonly": "contact:user.base:readonly",
|
||||
};
|
||||
const SENDER_NAME_TTL_MS = 10 * 60 * 1000;
|
||||
const SENDER_NAME_CACHE_MAX_SIZE = 500;
|
||||
const senderNameCache = new Map<string, { name: string; expireAt: number }>();
|
||||
|
||||
function correctFeishuScopeInUrl(url: string): string {
|
||||
@@ -117,6 +119,7 @@ export async function resolveFeishuSenderName(params: {
|
||||
const expireAt = resolveExpiresAtMsFromDurationMs(SENDER_NAME_TTL_MS);
|
||||
if (expireAt !== undefined) {
|
||||
senderNameCache.set(normalizedSenderId, { name, expireAt });
|
||||
pruneMapToMaxSize(senderNameCache, SENDER_NAME_CACHE_MAX_SIZE);
|
||||
}
|
||||
return { name };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user