Does Zend Http Client Provide a Graceful Cancellation API?
0 reputation · 18 Oct 2024, 03:50 UTC
0 reputation · 18 Oct 2024, 03:50 UTC
Gracefully abort HTTP requests in Zend Framework while respecting timeouts.
The timeout option limits only socket read/write. No native cancel API exists. Adapter‑specific behavior: Curl honours timeout for the whole request, Socket may ignore DNS/redirects.
Should the timeout encompass DNS resolution, redirects, and SSL handshakes? How to expose a cancellation method without breaking backward compatibility?
34775 reputation · 18 Oct 2024, 07:33 UTC
No. The Zend (Laminas) HTTP Client does not expose a cancel() method or any built-in mechanism to abort an in-flight request from application code. Once request() is called, the process blocks until the adapter returns a response or the socket-level timeout fires.
timeout option is passed to the underlying adapter and governs socket read/write limits, not a global request-cycle timer.The client was designed for synchronous, fire-and-forget use in traditional PHP request/response cycles. Because PHP historically lacks a standard event loop or thread model, there was no cross-platform way to signal an abort from another context. Adapters therefore rely on the OS socket layer, which behaves differently between libcurl and PHP's stream sockets.
$client = new Zend\Http\Client($uri, [
'adapter' => 'Zend\Http\Client\Adapter\Curl',
'timeout' => 10, // seconds, applies to whole request
]);
proc_open or a job queue) and terminate the child process if the parent decides to cancel. This isolates memory leaks and unstable state.connect_timeout (Curl-only option) to fail fast on unreachable hosts.
$client->getAdapter()->setCurlOption(CURLOPT_CONNECTTIMEOUT, 3);
pcntl_alarm (CLI only) or set_time_limit, but be aware these kill the entire script, not just the HTTP call.| Phase | Curl Adapter | Socket Adapter |
|---|---|---|
| DNS resolution | Covered by timeout | Usually not covered |
| TCP connect | Covered | Covered per read/write |
| TLS handshake | Covered | May exceed timeout |
| Redirects | Covered (total time) | Each hop resets timer |
| Body transfer | Covered | Covered per chunk |
Which adapter are you currently using, and does your workload involve long DNS lookups, many redirects, or slow TLS handshakes? The answer changes whether switching to the Curl adapter alone solves the problem or whether you need process isolation.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 18 Oct 2024, 09:45 UTC
When evaluating cancellation in Zend_Http_Client, it helps to separate the transport boundary from the client boundary. The timeout setting is consumed by the adapter, not enforced by the client itself. In the Curl adapter, this generally delegates to libcurl's transfer timeout, which spans DNS, connect, and redirect phases. In the Socket adapter, the timeout is applied via stream-level settings that govern read/write operations; phases before the stream is established, such as DNS lookup, may remain unbounded unless the underlying PHP stream wrapper enforces its own limits.
This architectural split means a client-level cancel() cannot simply interrupt a blocked adapter call. Since PHP runs synchronously, the request thread is occupied until the adapter returns or times out. Any cancellation mechanism would need the adapter to expose an abort hook or rely on the underlying handle's own cancellation support, which is a substantial change to the existing adapter contract.