Replies: 4 comments 8 replies
-
Hi, @vitsw Maybe you need to set the middleware before polling the request.? $responses = Http::withMiddleware(Middleware::log)->poll(fn (Pool $pool) => [
$pool->as('first')->post('https://test1.com'),
$pool->as('second')->post('https://test2.com')
]); |
Beta Was this translation helpful? Give feedback.
-
When Http::pool($callback) is called it instantiates an instance of PendingRequest and calls its framework/src/Illuminate/Http/Client/Factory.php Lines 390 to 392 in c275cdc The So here we can add requests to the Pool $responses = Http::withMiddleware(Middleware::log)->poll(fn (Pool $pool) => [
$pool->as('first')->post('https://test1.com'),
$pool->as('second')->post('https://test2.com')
]); Pool instantiates new PendingRequest using framework/src/Illuminate/Http/Client/Pool.php Lines 56 to 69 in c275cdc
framework/src/Illuminate/Http/Client/PendingRequest.php Lines 1088 to 1095 in c275cdc This is how handlerStack is configured. Some internal handler are pushed to the stack as well as middleware. Particulary when we add a request to the pool the setHandler() method is called on a fresh instance of the PendingRequest so the middleware collection is empty. framework/src/Illuminate/Http/Client/PendingRequest.php Lines 860 to 871 in c275cdc After that framework/src/Illuminate/Http/Client/PendingRequest.php Lines 1012 to 1017 in c275cdc So at this point PendingRequest is configured (client is build and handlerStack is set up), marked as async and added to the Pool. When Pool runs requests framework/src/Illuminate/Http/Client/PendingRequest.php Lines 742 to 754 in c275cdc So when request was marked async, it would reuse client which had been already created and configured in this case using setHandler() method. That means if we add middleware to the request they won't be pushed to the handlerStack. framework/src/Illuminate/Http/Client/PendingRequest.php Lines 803 to 828 in c275cdc I've solved this by extending the Pool, but I am curious about the reasons it was designed in this way. Or may be I missed something. |
Beta Was this translation helpful? Give feedback.
-
Will this be fixed? It seems like middleware should be able to work with pooled requests... |
Beta Was this translation helpful? Give feedback.
-
@vitsw Could you please share you workaround for extending Pool class? |
Beta Was this translation helpful? Give feedback.
-
I have to push a log middleware to the asynchronous requests. When I add a request to the Pool it initializes PendingRequest by calling setHandler() method and async() method. It means that the client is build, all handlers are pushed to the handlerStack and client is marked as reusable. So no middleware will be pushed to the handlerStack afterwards.
For example, those middleware won't be pushed to the handlerStack:
Is it intended behavior? Is there some pitfalls with using middleware with async requests? Or it works in this way just because it was unnecessary feature?
Beta Was this translation helpful? Give feedback.
All reactions