Skip to content

Commit 0f7a505

Browse files
authored
fix: cut down per request garbage (#1202)
Signed-off-by: ferhat elmas <elmas.ferhat@gmail.com>
1 parent a809286 commit 0f7a505

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

src/http/plugins/storage.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const { storageBackendType, storageS3Bucket } = getConfig()
1919

2020
const storageBackend = createStorageBackend(storageBackendType)
2121

22+
// TenantLocation is immutable so singleton not to allocate per request.
23+
const tenantLocation = new TenantLocation(storageS3Bucket)
24+
2225
export const storage = fastifyPlugin(
2326
async function storagePlugin(fastify) {
2427
fastify.decorateRequest('storage')
@@ -35,7 +38,7 @@ export const storage = fastifyPlugin(
3538

3639
const location = request.isIcebergBucket
3740
? new PassThroughLocation(request.internalIcebergBucketName!)
38-
: new TenantLocation(storageS3Bucket)
41+
: tenantLocation
3942

4043
request.backend = storageBackend
4144
request.storage = new Storage(storageBackend, database, location)

src/storage/storage.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import { AssetRenderer, HeadRenderer, ImageRenderer } from './renderer'
2121

2222
const { emptyBucketMax } = getConfig()
2323

24+
function assertNever(value: never): never {
25+
throw new Error(`Unexpected renderer type: ${String(value)}`)
26+
}
27+
2428
/**
2529
* Storage
2630
* interacts with the storage backend of choice and the database
@@ -56,14 +60,18 @@ export class Storage {
5660
* @param type
5761
*/
5862
renderer(type: 'asset' | 'head' | 'image' | 'info') {
59-
const renderers = {
60-
asset: () => new AssetRenderer(this.backend),
61-
head: () => new HeadRenderer(),
62-
image: () => new ImageRenderer(this.backend),
63-
info: () => new InfoRenderer(),
63+
switch (type) {
64+
case 'asset':
65+
return new AssetRenderer(this.backend)
66+
case 'head':
67+
return new HeadRenderer()
68+
case 'image':
69+
return new ImageRenderer(this.backend)
70+
case 'info':
71+
return new InfoRenderer()
72+
default:
73+
return assertNever(type)
6474
}
65-
66-
return renderers[type]()
6775
}
6876

6977
/**

0 commit comments

Comments
 (0)