Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lambda-tiler): do not keep failed tiffs in memory #3331

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions packages/lambda-tiler/src/util/__test__/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,31 @@ import { fsa, FsMemory } from '@chunkd/fs';
import { SourceCache } from '../source.cache.js';

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

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

await cache.getCog(tiffLoc).catch(() => null);
assert.equal(cache.cache.currentSize, 1);
let failCount = 0;
await cache.getCog(tiffLoc).catch(() => failCount++);
assert.equal(cache.cache.currentSize, 0);
assert.equal(failCount, 1);
});

it('should not exit if a promise rejection happens for tar', async () => {
const cache = new SourceCache(5);

const mem = new FsMemory();
const tiffLoc = new URL('memory://foo/bar.tar');
await mem.write(tiffLoc, Buffer.from('ABC123'));
fsa.register('memory://', mem);

let failCount = 0;
await cache.getCotar(tiffLoc).catch(() => failCount++);
assert.equal(cache.cache.currentSize, 0);
assert.equal(failCount, 1);
});
});
2 changes: 2 additions & 0 deletions packages/lambda-tiler/src/util/source.cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class SourceCache {
}
const value = Tiff.create(fsa.source(location));
this.cache.set(location.href, { type: 'cog', value, size: 1 });
value.catch(() => this.cache.delete(location.href));
return value;
}

Expand All @@ -43,6 +44,7 @@ export class SourceCache {
}
const value = Cotar.fromTar(fsa.source(location));
this.cache.set(location.href, { type: 'cotar', value, size: 1 });
value.catch(() => this.cache.delete(location.href));
return value;
}
}
Expand Down
5 changes: 5 additions & 0 deletions packages/lambda-tiler/src/util/swapping.lru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export class SwappingLru<T extends { size: number }> {
this.clears++;
}

delete(id: string): void {
this.cacheA.delete(id);
this.cacheB.delete(id);
}

set(id: string, tiff: T): void {
this.cacheA.set(id, tiff);
this.check();
Expand Down
Loading