Skip to content

Conversation

@karlseguin
Copy link
Collaborator

Async HTTP request work by emitting a "Progress" object to a callback. This object has a "done" flag which, when true, indicates that all data has been emitting and no future "Progress" objects will be sent.

Callers like XHR buffer the response and wait for "done = true" to then process the request.

The HTTP client relies on two important object pools: the connection and the state (with all the buffers for reading/writing).

In its current implementation, the async flow does not release these pooled objects until the final callback has returned. At best, this is inefficient: we're keeping the connection and state objects checked out for longer than they have to be. At worse, it can lead to a deadlock. If the calling code issues a new request when done == true, we'll eventually run out of state objects in the pool.

This commit now releases the state objects before emit the final "done" Progress message. For this to work, this final message will always have null data and an empty header object.

Async HTTP request work by emitting a "Progress" object to a callback. This
object has a "done" flag which, when `true`, indicates that all data has been
emitting and no future "Progress" objects will be sent.

Callers like XHR buffer the response and wait for "done = true" to then process
the request.

The HTTP client relies on two important object pools: the connection and the
state (with all the buffers for reading/writing).

In its current implementation, the async flow does not release these pooled
objects until the final callback has returned. At best, this is inefficient:
we're keeping the connection and state objects checked out for longer than they
have to be. At worse, it can lead to a deadlock. If the calling code issues a
new request when done == true, we'll eventually run out of state objects in the
pool.

This commit now releases the state objects before emit the final "done" Progress
message. For this to work, this final message will always have null data and
an empty header object.
@karlseguin karlseguin merged commit 478d919 into main Jun 6, 2025
11 checks passed
@karlseguin karlseguin deleted the http_client_optimization branch June 6, 2025 05:21
@github-actions github-actions bot locked and limited conversation to collaborators Jun 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants