-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgraphile-cache.ts
More file actions
289 lines (245 loc) · 8.02 KB
/
graphile-cache.ts
File metadata and controls
289 lines (245 loc) · 8.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import { EventEmitter } from 'events';
import { Logger } from '@pgpmjs/logger';
import { LRUCache } from 'lru-cache';
import { pgCache } from 'pg-cache';
import type { Express } from 'express';
import type { Server as HttpServer } from 'http';
import type { PostGraphileInstance } from 'postgraphile';
import type { GrafservBase } from 'grafserv';
const log = new Logger('graphile-cache');
// --- Time Constants ---
export const ONE_HOUR_MS = 1000 * 60 * 60;
export const FIVE_MINUTES_MS = 1000 * 60 * 5;
const ONE_DAY = ONE_HOUR_MS * 24;
const ONE_YEAR = ONE_DAY * 366;
// --- Eviction Types ---
export type EvictionReason = 'lru' | 'ttl' | 'manual';
// --- Cache Event Emitter ---
export interface CacheEvictionEvent {
key: string;
reason: EvictionReason;
entry: GraphileCacheEntry;
}
export class CacheEventEmitter extends EventEmitter {
emitEviction(event: CacheEvictionEvent): void {
this.emit('eviction', event);
}
onEviction(handler: (event: CacheEvictionEvent) => void): void {
this.on('eviction', handler);
}
}
export const cacheEvents = new CacheEventEmitter();
// --- Cache Configuration ---
export interface CacheConfig {
max: number;
ttl: number;
}
/**
* Get cache configuration from environment variables
*
* Supports:
* - GRAPHILE_CACHE_MAX: Maximum number of entries (default: 15)
* - GRAPHILE_CACHE_TTL_MS: TTL in milliseconds
* - Production default: ONE_YEAR
* - Development default: FIVE_MINUTES_MS
*/
export function getCacheConfig(): CacheConfig {
const isDevelopment = process.env.NODE_ENV === 'development';
const max = process.env.GRAPHILE_CACHE_MAX
? parseInt(process.env.GRAPHILE_CACHE_MAX, 10)
: 15;
const ttl = process.env.GRAPHILE_CACHE_TTL_MS
? parseInt(process.env.GRAPHILE_CACHE_TTL_MS, 10)
: isDevelopment
? FIVE_MINUTES_MS
: ONE_YEAR;
return { max, ttl };
}
/**
* Cache entry for PostGraphile v5 instances
*
* Each entry contains:
* - pgl: The PostGraphile instance (manages schema, plugins, etc.)
* - serv: The Grafserv server instance (handles HTTP/WS)
* - handler: Express app for routing requests
* - httpServer: Node HTTP server (required by grafserv)
* - cacheKey: Unique identifier for this entry
* - createdAt: Timestamp when this entry was created
*/
export interface GraphileCacheEntry {
pgl: PostGraphileInstance;
serv: GrafservBase;
handler: Express;
httpServer: HttpServer;
cacheKey: string;
createdAt: number;
}
// Track disposed entries by reference to prevent double-disposal.
// Using a WeakSet keyed on the entry object (rather than the string key)
// avoids the race where closeAllCaches() manually disposes an entry,
// the guard key is cleaned up, and then graphileCache.clear() triggers
// the LRU dispose callback which attempts to release the same
// PostGraphile instance a second time.
const disposedEntries = new WeakSet<GraphileCacheEntry>();
// Track keys that are being manually evicted for accurate eviction reason
const manualEvictionKeys = new Set<string>();
/**
* Dispose a PostGraphile v5 cache entry
*
* Properly releases resources by:
* 1. Closing the HTTP server if listening
* 2. Releasing the PostGraphile instance (which internally releases grafserv)
*
* Uses disposedEntries WeakSet to prevent double-disposal when closeAllCaches()
* explicitly disposes entries and then clear() triggers the dispose callback.
*/
const disposeEntry = async (entry: GraphileCacheEntry, key: string): Promise<void> => {
// Prevent double-disposal (tracked by object reference, not key string)
if (disposedEntries.has(entry)) {
return;
}
disposedEntries.add(entry);
log.debug(`Disposing PostGraphile[${key}]`);
try {
// Close HTTP server if it's listening
if (entry.httpServer?.listening) {
await new Promise<void>((resolve) => {
entry.httpServer.close(() => resolve());
});
}
// Release PostGraphile instance (this also releases grafserv internally)
if (entry.pgl) {
await entry.pgl.release();
}
} catch (err) {
log.error(`Error disposing PostGraphile[${key}]:`, err);
}
};
/**
* Determine the eviction reason for a cache entry
*/
const getEvictionReason = (key: string, entry: GraphileCacheEntry): EvictionReason => {
if (manualEvictionKeys.has(key)) {
manualEvictionKeys.delete(key);
return 'manual';
}
// Check if TTL expired
const age = Date.now() - entry.createdAt;
const config = getCacheConfig();
if (age >= config.ttl) {
return 'ttl';
}
return 'lru';
};
// Get initial cache configuration
const initialConfig = getCacheConfig();
// --- Graphile Cache ---
export const graphileCache = new LRUCache<string, GraphileCacheEntry>({
max: initialConfig.max,
ttl: initialConfig.ttl,
updateAgeOnGet: true,
dispose: (entry, key) => {
// Determine eviction reason before disposal
const reason = getEvictionReason(key, entry);
// Emit eviction event
cacheEvents.emitEviction({ key, reason, entry });
log.debug(`Evicting PostGraphile[${key}] (reason: ${reason})`);
// LRU dispose is synchronous, but v5 disposal is async
// Fire and forget the async cleanup
disposeEntry(entry, key).catch((err) => {
log.error(`Failed to dispose PostGraphile[${key}]:`, err);
});
}
});
// --- Cache Stats ---
export interface CacheStats {
size: number;
max: number;
ttl: number;
keys: string[];
}
/**
* Get current cache statistics
*/
export function getCacheStats(): CacheStats {
const config = getCacheConfig();
return {
size: graphileCache.size,
max: config.max,
ttl: config.ttl,
keys: [...graphileCache.keys()]
};
}
// --- Clear Matching Entries ---
/**
* Clear cache entries matching a regex pattern
*
* @param pattern - RegExp to match against cache keys
* @returns Number of entries cleared
*/
export function clearMatchingEntries(pattern: RegExp): number {
let cleared = 0;
for (const key of graphileCache.keys()) {
if (pattern.test(key)) {
// Mark as manual eviction before deleting
manualEvictionKeys.add(key);
graphileCache.delete(key);
cleared++;
}
}
return cleared;
}
// Register cleanup callback with pgCache
// When a pg pool is disposed, clean up any graphile instances using it
const unregister = pgCache.registerCleanupCallback((pgPoolKey: string) => {
log.debug(`pgPool[${pgPoolKey}] disposed - checking graphile entries`);
// Remove graphile entries that reference this pool key
graphileCache.forEach((entry, k) => {
if (entry.cacheKey.includes(pgPoolKey)) {
log.debug(`Removing graphileCache[${k}] due to pgPool[${pgPoolKey}] disposal`);
manualEvictionKeys.add(k);
graphileCache.delete(k);
}
});
});
// Enhanced close function that handles all caches
const closePromise: { promise: Promise<void> | null } = { promise: null };
/**
* Close all caches and release resources
*
* This function:
* 1. Disposes all PostGraphile v5 instances (async)
* 2. Clears the graphile cache
* 3. Closes all pg pools via pgCache
*
* The function is idempotent - calling it multiple times
* returns the same promise.
*/
export const closeAllCaches = async (verbose = false): Promise<void> => {
if (closePromise.promise) return closePromise.promise;
closePromise.promise = (async () => {
try {
if (verbose) log.info('Closing all server caches...');
// Collect all entries and dispose them properly
const entries = [...graphileCache.entries()];
// Mark all as manual evictions
for (const [key] of entries) {
manualEvictionKeys.add(key);
}
const disposePromises = entries.map(([key, entry]) =>
disposeEntry(entry, key)
);
// Wait for all disposals to complete
await Promise.allSettled(disposePromises);
// Clear the cache after disposal (dispose callback will no-op due to disposedEntries)
graphileCache.clear();
manualEvictionKeys.clear();
// Close pg pools
await pgCache.close();
if (verbose) log.success('All caches disposed.');
} finally {
closePromise.promise = null;
}
})();
return closePromise.promise;
};