@@ -479,28 +479,29 @@ export class DebugProxyCaptureStore {
|
479 | 479 | } |
480 | 480 | } |
481 | 481 | |
482 | | -let cachedStore: DebugProxyCaptureStore | null = null; |
483 | | -let cachedKey = ""; |
484 | | -let cachedStoreLeases = 0; |
| 482 | +type CachedStoreEntry = { |
| 483 | +store: DebugProxyCaptureStore; |
| 484 | +leases: number; |
| 485 | +}; |
| 486 | + |
| 487 | +const cachedStores = new Map<string, CachedStoreEntry>(); |
485 | 488 | |
486 | 489 | export function getDebugProxyCaptureStore(dbPath: string, blobDir: string): DebugProxyCaptureStore { |
487 | 490 | const key = `${dbPath}:${blobDir}`; |
488 | | -if (!cachedStore || cachedStore.isClosed || cachedKey !== key) { |
489 | | -cachedStore = new DebugProxyCaptureStore(dbPath, blobDir); |
490 | | -cachedKey = key; |
491 | | -cachedStoreLeases = 0; |
| 491 | +const cached = cachedStores.get(key); |
| 492 | +if (cached && !cached.store.isClosed) { |
| 493 | +return cached.store; |
492 | 494 | } |
493 | | -return cachedStore; |
| 495 | +const store = new DebugProxyCaptureStore(dbPath, blobDir); |
| 496 | +cachedStores.set(key, { store, leases: 0 }); |
| 497 | +return store; |
494 | 498 | } |
495 | 499 | |
496 | 500 | export function closeDebugProxyCaptureStore(): void { |
497 | | -if (!cachedStore) { |
498 | | -return; |
| 501 | +for (const cached of cachedStores.values()) { |
| 502 | +cached.store.close(); |
499 | 503 | } |
500 | | -cachedStore.close(); |
501 | | -cachedStore = null; |
502 | | -cachedKey = ""; |
503 | | -cachedStoreLeases = 0; |
| 504 | +cachedStores.clear(); |
504 | 505 | } |
505 | 506 | |
506 | 507 | // Lease API keeps one cached synchronous SQLite connection alive across related |
@@ -509,9 +510,13 @@ export function acquireDebugProxyCaptureStore(
|
509 | 510 | dbPath: string, |
510 | 511 | blobDir: string, |
511 | 512 | ): { store: DebugProxyCaptureStore; release: () => void } { |
| 513 | +const key = `${dbPath}:${blobDir}`; |
512 | 514 | const store = getDebugProxyCaptureStore(dbPath, blobDir); |
513 | | -const key = cachedKey; |
514 | | -cachedStoreLeases += 1; |
| 515 | +const cached = cachedStores.get(key); |
| 516 | +if (!cached || cached.store !== store) { |
| 517 | +throw new Error("debug proxy capture store cache changed while acquiring a lease"); |
| 518 | +} |
| 519 | +cached.leases += 1; |
515 | 520 | let released = false; |
516 | 521 | return { |
517 | 522 | store, |
@@ -520,9 +525,14 @@ export function acquireDebugProxyCaptureStore(
|
520 | 525 | return; |
521 | 526 | } |
522 | 527 | released = true; |
523 | | -cachedStoreLeases = Math.max(0, cachedStoreLeases - 1); |
524 | | -if (cachedStoreLeases === 0 && cachedStore === store && cachedKey === key) { |
525 | | -closeDebugProxyCaptureStore(); |
| 528 | +const current = cachedStores.get(key); |
| 529 | +if (!current || current.store !== store) { |
| 530 | +return; |
| 531 | +} |
| 532 | +current.leases = Math.max(0, current.leases - 1); |
| 533 | +if (current.leases === 0) { |
| 534 | +current.store.close(); |
| 535 | +cachedStores.delete(key); |
526 | 536 | } |
527 | 537 | }, |
528 | 538 | }; |
|