Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Do not execute beforeSending callbacks twice #37116

Merged
merged 1 commit into from
Apr 26, 2021
Merged

[8.x] Do not execute beforeSending callbacks twice #37116

merged 1 commit into from
Apr 26, 2021

Conversation

rodrigopedra
Copy link
Contributor

As #37105 outlines, the HTTP client executes the beforeSending callbacks twice when sending a request.

This is due to both buildBeforeSendingHandler and buildRecordHandler running the runBeforeSendingCallbacks method, and both this build* methods being added to the request middleware stack.

public function buildHandlerStack()
{
return tap(HandlerStack::create(), function ($stack) {
$stack->push($this->buildBeforeSendingHandler());
$stack->push($this->buildRecorderHandler());
$stack->push($this->buildStubHandler());
$this->middleware->each(function ($middleware) use ($stack) {
$stack->push($middleware);
});
});
}

public function buildBeforeSendingHandler()
{
return function ($handler) {
return function ($request, $options) use ($handler) {
return $handler($this->runBeforeSendingCallbacks($request, $options), $options);
};
};
}

public function buildRecorderHandler()
{
return function ($handler) {
return function ($request, $options) use ($handler) {
$promise = $handler($this->runBeforeSendingCallbacks($request, $options), $options);
return $promise->then(function ($response) use ($request, $options) {
optional($this->factory)->recordRequestResponsePair(
(new Request($request))->withData($options['laravel_data']),
new Response($response)
);
return $response;
});
};
};
}

This PR removes the redundant call from buildRecordHandler (as it is ran after buildBeforeSendingHandler in the middleware stack) and simply calling $promise = $handler($request, $options); to keep it as a "after" middleware, using the example in Guzzle docs:

https://docs.guzzlephp.org/en/stable/handlers-and-middleware.html#middleware

Closes #37105

@taylorotwell taylorotwell merged commit fe54c3f into laravel:8.x Apr 26, 2021
@taylorotwell
Copy link
Member

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Http Client duplucate effort when attaching "beforeSending" closure callback
2 participants