Skip to content

Commit 89e72ea

Browse files
authored
fix(lambda-tiler): do not keep failed tiffs in memory (#3331)
### Motivation when requests fail, sometimes they can be retried later and succeed, right now we are caching the error state. ### Modifications On error remove the cached tiff/tar from the cache ### Verification added unit tests.
1 parent b9fe33f commit 89e72ea

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

packages/lambda-tiler/src/util/__test__/cache.test.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,31 @@ import { fsa, FsMemory } from '@chunkd/fs';
66
import { SourceCache } from '../source.cache.js';
77

88
describe('CoSourceCache', () => {
9-
it('should not exit if a promise rejection happens', async () => {
9+
it('should not exit if a promise rejection happens for tiff', async () => {
1010
const cache = new SourceCache(5);
1111

1212
const mem = new FsMemory();
1313
const tiffLoc = new URL('memory://foo/bar.tiff');
1414
await mem.write(tiffLoc, Buffer.from('ABC123'));
1515
fsa.register('memory://', mem);
1616

17-
await cache.getCog(tiffLoc).catch(() => null);
18-
assert.equal(cache.cache.currentSize, 1);
17+
let failCount = 0;
18+
await cache.getCog(tiffLoc).catch(() => failCount++);
19+
assert.equal(cache.cache.currentSize, 0);
20+
assert.equal(failCount, 1);
21+
});
22+
23+
it('should not exit if a promise rejection happens for tar', async () => {
24+
const cache = new SourceCache(5);
25+
26+
const mem = new FsMemory();
27+
const tiffLoc = new URL('memory://foo/bar.tar');
28+
await mem.write(tiffLoc, Buffer.from('ABC123'));
29+
fsa.register('memory://', mem);
30+
31+
let failCount = 0;
32+
await cache.getCotar(tiffLoc).catch(() => failCount++);
33+
assert.equal(cache.cache.currentSize, 0);
34+
assert.equal(failCount, 1);
1935
});
2036
});

packages/lambda-tiler/src/util/source.cache.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class SourceCache {
3131
}
3232
const value = Tiff.create(fsa.source(location));
3333
this.cache.set(location.href, { type: 'cog', value, size: 1 });
34+
value.catch(() => this.cache.delete(location.href));
3435
return value;
3536
}
3637

@@ -43,6 +44,7 @@ export class SourceCache {
4344
}
4445
const value = Cotar.fromTar(fsa.source(location));
4546
this.cache.set(location.href, { type: 'cotar', value, size: 1 });
47+
value.catch(() => this.cache.delete(location.href));
4648
return value;
4749
}
4850
}

packages/lambda-tiler/src/util/swapping.lru.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ export class SwappingLru<T extends { size: number }> {
4141
this.clears++;
4242
}
4343

44+
delete(id: string): void {
45+
this.cacheA.delete(id);
46+
this.cacheB.delete(id);
47+
}
48+
4449
set(id: string, tiff: T): void {
4550
this.cacheA.set(id, tiff);
4651
this.check();

0 commit comments

Comments
 (0)