Skip to content

Commit 89c6332

Browse files
committed
fix(cache): prevent unbounded TTLCache memory growth
1 parent d6f2ce9 commit 89c6332

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

lib/github.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,20 @@ interface GitHubUserProfile {
110110
plan?: { name?: string } | null;
111111
}
112112

113-
const contributionsCache = new TTLCache<ContributionCalendar>();
114-
const profileCache = new TTLCache<GitHubUserProfile>();
115-
const reposCache = new TTLCache<GitHubRepo[]>();
113+
// Named constants to avoid magic numbers and allow future tuning
114+
const MAX_CONTRIBUTIONS_CACHE_SIZE = 1000;
115+
const MAX_PROFILE_CACHE_SIZE = 1000;
116+
const MAX_REPOS_CACHE_SIZE = 500;
117+
118+
// Bounded capacity controls to prevent unbounded heap memory leaks (OOM).
119+
// Under continuous crawler/bot scanning or viral peaks, unbounded cache size
120+
// allocations will exhaust Node/Vercel serverless RAM.
121+
// Specifying explicit capacity limits enforces a First-In, First-Out (FIFO)
122+
// eviction strategy (since standard ES6 Map maintains key insertion order) and
123+
// bounds max memory consumption to stable, predictable boundaries.
124+
const contributionsCache = new TTLCache<ContributionCalendar>(MAX_CONTRIBUTIONS_CACHE_SIZE);
125+
const profileCache = new TTLCache<GitHubUserProfile>(MAX_PROFILE_CACHE_SIZE);
126+
const reposCache = new TTLCache<GitHubRepo[]>(MAX_REPOS_CACHE_SIZE);
116127

117128
function cacheKey(
118129
kind: 'contributions' | 'profile' | 'repos',

0 commit comments

Comments
 (0)