Skip to content

Commit 9e625aa

Browse files
committed
add lru cache
1 parent fb0b8af commit 9e625aa

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"@mongodb-js/devtools-connect": "^3.7.2",
6363
"@mongosh/service-provider-node-driver": "^3.6.0",
6464
"bson": "^6.10.3",
65+
"lru-cache": "^11.1.0",
6566
"mongodb": "^6.15.0",
6667
"mongodb-log-writer": "^2.4.1",
6768
"mongodb-redact": "^1.1.6",

src/telemetry/eventCache.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
import { BaseEvent } from "./types.js";
2+
import { LRUCache } from "lru-cache";
23

34
/**
45
* Singleton class for in-memory telemetry event caching
56
* Provides a central storage for telemetry events that couldn't be sent
7+
* Uses LRU cache to automatically drop oldest events when limit is exceeded
68
*/
79
export class EventCache {
810
private static instance: EventCache;
9-
private events: BaseEvent[] = [];
11+
private static readonly MAX_EVENTS = 1000;
1012

11-
private constructor() {}
13+
private cache: LRUCache<number, BaseEvent>;
14+
private nextId = 0;
15+
16+
private constructor() {
17+
this.cache = new LRUCache({
18+
max: EventCache.MAX_EVENTS,
19+
// Using FIFO eviction strategy for events
20+
allowStale: false,
21+
updateAgeOnGet: false,
22+
});
23+
}
1224

1325
/**
1426
* Gets the singleton instance of EventCache
@@ -26,21 +38,25 @@ export class EventCache {
2638
* @returns Array of cached BaseEvent objects
2739
*/
2840
public getEvents(): BaseEvent[] {
29-
return [...this.events];
41+
return Array.from(this.cache.values());
3042
}
3143

3244
/**
33-
* Sets the cached events, replacing any existing events
34-
* @param events - The events to cache
45+
* Appends new events to the cached events
46+
* LRU cache automatically handles dropping oldest events when limit is exceeded
47+
* @param events - The events to append
3548
*/
36-
public setEvents(events: BaseEvent[]): void {
37-
this.events = [...events];
49+
public appendEvents(events: BaseEvent[]): void {
50+
for (const event of events) {
51+
this.cache.set(this.nextId++, event);
52+
}
3853
}
3954

4055
/**
4156
* Clears all cached events
4257
*/
4358
public clearEvents(): void {
44-
this.events = [];
59+
this.cache.clear();
60+
this.nextId = 0;
4561
}
4662
}

src/telemetry/telemetry.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,12 @@ export class Telemetry {
110110
return;
111111
}
112112

113-
logger.warning(mongoLogId(1_000_005), "telemetry", `Error sending event to client: ${result.error}`);
114-
this.eventCache.setEvents(allEvents);
113+
logger.warning(
114+
mongoLogId(1_000_005),
115+
"telemetry",
116+
`Error sending event to client: ${result.error instanceof Error ? result.error.message : String(result.error)}`
117+
);
118+
this.eventCache.appendEvents(events);
115119
}
116120

117121
/**

0 commit comments

Comments
 (0)