Skip to content

Do not dispose client and keep only one client #23083

Description

@JPeer264

Problem Statement

Every request creates a new client and creating a new client means more overhead for the CPU. This means that after every response we also have to .dispose the client to prevent memory leaks, which brings other downsides as in DOs other async work could potentially be triggered at a later point in time, where the client would be disposed already (see #22545).

Also currently we create a new isolation scope per request, but it wouldn't matter, as there would only be 1 isolation scope in this case.

Solution Brainstorm

Potentially we don't need to create multiple clients, but could reuse a client if it was already created. We would keep the same structure as now but at the time we would create a client, we just need to reuse it. This would solve e.g. #22545 and #21950 as the client wouldn't be disposed.

But it'd bring other challenges, as there is no timer to flush the events (by default we flush after an 5 second interval, which wouldn't work OOTB in Cloudflare as the timing works differently in there). For that we need points in the SDK where we know we can flush. A way I could think of:

  1. flush after every request, as we already do
  2. After the request is done we start listening on events, like afterSpanEnd for spans or afterCaptureLog for logs, so we don't miss out on these, but still have a way to register these events

In the future this could be simplified with tail workers, see #23081

Additional Context

No response

Priority

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions