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:
- flush after every request, as we already do
- 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.
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
.disposethe 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:
afterSpanEndfor spans orafterCaptureLogfor logs, so we don't miss out on these, but still have a way to register these eventsIn 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
+1orme too, to help us triage it.