Skip to content

Commit db9e051

Browse files
committed
Avoid cache "evicted" error.
This is relevant when there are more concurrent requests than the cache size.
1 parent 2b818ae commit db9e051

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

packages/service-core/src/storage/ChecksumCache.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ export class ChecksumCache {
8989
}
9090
},
9191

92-
noDisposeOnSet: true
92+
noDisposeOnSet: true,
93+
94+
// When we have more fetches than the cache size, complete the fetches instead
95+
// of failing with Error('evicted').
96+
ignoreFetchAbort: true
9397
});
9498
}
9599

packages/service-core/test/src/checksum_cache.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,40 @@ describe('checksum cache', function () {
397397
[{ bucket: 'test', end: '123' }]
398398
]);
399399
});
400+
401+
it('should handle concurrent requests greater than cache size', async function () {
402+
// This will not be cached efficiently, but we test that we don't get errors at least.
403+
let lookups: FetchPartialBucketChecksum[][] = [];
404+
const cache = new ChecksumCache({
405+
fetchChecksums: async (batch) => {
406+
lookups.push(batch);
407+
return fetchTestChecksums(batch);
408+
},
409+
maxSize: 2
410+
});
411+
412+
const p3 = cache.getChecksums('123', ['test3']);
413+
const p4 = cache.getChecksums('123', ['test4']);
414+
const p1 = cache.getChecksums('123', ['test']);
415+
const p2 = cache.getChecksums('123', ['test2']);
416+
417+
expect(await p1).toEqual([TEST_123]);
418+
expect(await p2).toEqual([TEST2_123]);
419+
expect(await p3).toEqual([TEST3_123]);
420+
expect(await p4).toEqual([
421+
{
422+
bucket: 'test4',
423+
checksum: 1004797863,
424+
count: 123
425+
}
426+
]);
427+
428+
// The lookup should be deduplicated, even though it's in progress
429+
expect(lookups).toEqual([
430+
[{ bucket: 'test3', end: '123' }],
431+
[{ bucket: 'test4', end: '123' }],
432+
[{ bucket: 'test', end: '123' }],
433+
[{ bucket: 'test2', end: '123' }]
434+
]);
435+
});
400436
});

0 commit comments

Comments
 (0)