fix(nostr): bound seen tracker timer options (#97133) · openclaw/openclaw@8fa2432
zhangguiping
·
2026-06-27
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -232,6 +232,55 @@ describe("SeenTracker", () => {
|
232 | 232 | tracker.stop(); |
233 | 233 | vi.useRealTimers(); |
234 | 234 | }); |
| 235 | + |
| 236 | +it.each([-1, 0])("falls back to default TTL for non-positive ttlMs %s", (ttlMs) => { |
| 237 | +vi.useFakeTimers(); |
| 238 | +const tracker = createTracker({ ttlMs, pruneIntervalMs: 10 * 60 * 1000 }); |
| 239 | + |
| 240 | +try { |
| 241 | +tracker.add("id1"); |
| 242 | +vi.advanceTimersByTime(1); |
| 243 | +expect(tracker.peek("id1")).toBe(true); |
| 244 | +} finally { |
| 245 | +tracker.stop(); |
| 246 | +vi.useRealTimers(); |
| 247 | +} |
| 248 | +}); |
| 249 | + |
| 250 | +it("falls back to default TTL for infinite ttlMs", () => { |
| 251 | +vi.useFakeTimers(); |
| 252 | +const tracker = createTracker({ |
| 253 | +ttlMs: Number.POSITIVE_INFINITY, |
| 254 | +pruneIntervalMs: 10 * 60 * 1000, |
| 255 | +}); |
| 256 | + |
| 257 | +try { |
| 258 | +tracker.add("id1"); |
| 259 | +vi.advanceTimersByTime(60 * 60 * 1000 + 1); |
| 260 | +expect(tracker.peek("id1")).toBe(false); |
| 261 | +} finally { |
| 262 | +tracker.stop(); |
| 263 | +vi.useRealTimers(); |
| 264 | +} |
| 265 | +}); |
| 266 | + |
| 267 | +it.each([-1, 0, Number.POSITIVE_INFINITY])( |
| 268 | +"uses the default prune interval for unsafe pruneIntervalMs %s", |
| 269 | +(pruneIntervalMs) => { |
| 270 | +vi.useFakeTimers(); |
| 271 | +const setIntervalSpy = vi.spyOn(globalThis, "setInterval"); |
| 272 | +const tracker = createTracker({ pruneIntervalMs }); |
| 273 | + |
| 274 | +try { |
| 275 | +expect(setIntervalSpy).toHaveBeenCalledTimes(1); |
| 276 | +expect(setIntervalSpy.mock.calls[0]?.[1]).toBe(10 * 60 * 1000); |
| 277 | +} finally { |
| 278 | +tracker.stop(); |
| 279 | +setIntervalSpy.mockRestore(); |
| 280 | +vi.useRealTimers(); |
| 281 | +} |
| 282 | +}, |
| 283 | +); |
235 | 284 | }); |
236 | 285 | }); |
237 | 286 | |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -3,7 +3,10 @@
|
3 | 3 | * Prevents unbounded memory growth under high load or abuse. |
4 | 4 | */ |
5 | 5 | |
6 | | -import { resolveIntegerOption } from "openclaw/plugin-sdk/number-runtime"; |
| 6 | +import { |
| 7 | +resolveIntegerOption, |
| 8 | +resolvePositiveTimerTimeoutMs, |
| 9 | +} from "openclaw/plugin-sdk/number-runtime"; |
7 | 10 | |
8 | 11 | interface SeenTrackerOptions { |
9 | 12 | /** Maximum number of entries to track (default: 100,000) */ |
@@ -45,8 +48,8 @@ interface Entry {
|
45 | 48 | */ |
46 | 49 | export function createSeenTracker(options?: SeenTrackerOptions): SeenTracker { |
47 | 50 | const maxEntries = resolveIntegerOption(options?.maxEntries, 100_000, { min: 1 }); |
48 | | -const ttlMs = options?.ttlMs ?? 60 * 60 * 1000; // 1 hour |
49 | | -const pruneIntervalMs = options?.pruneIntervalMs ?? 10 * 60 * 1000; // 10 minutes |
| 51 | +const ttlMs = resolvePositiveTimerTimeoutMs(options?.ttlMs, 60 * 60 * 1000); |
| 52 | +const pruneIntervalMs = resolvePositiveTimerTimeoutMs(options?.pruneIntervalMs, 10 * 60 * 1000); |
50 | 53 | |
51 | 54 | // Main storage |
52 | 55 | const entries = new Map<string, Entry>(); |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。