Skip to content

[Low] S3Client instantiated on every R2 operation — no singleton #101

@bmersereau

Description

@bmersereau

Problem

backend/src/lib/storage.tsgetClient() creates a new S3Client(...) on every call, and every exported function calls it:

function getClient(): S3Client {
  return new S3Client({ region: "auto", endpoint: ..., credentials: ... });
}

export async function uploadFile(...) {
  const client = getClient();  // new instance every upload
  ...
}

AWS SDK clients are heavyweight — they initialise an HTTP agent, retry logic, and credential resolution per instantiation. Creating one per request prevents keep-alive connection reuse and adds unnecessary GC pressure, especially during upload spikes (50/hr rate limit window).

Fix

Hoist the client to a module-level singleton, lazy-initialised on first use:

let _client: S3Client | null = null;
function getClient(): S3Client {
  if (!_client) _client = new S3Client({ ... });
  return _client;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions