In PHP, the curl extension is a vital tool that enables data exchange between the client and server. We commonly use curl to send HTTP requests or retrieve data from a server. However, when making multiple requests, optimizing connection establishment time becomes crucial.
curl_upkeep() is a function used to maintain and reuse HTTP connections, especially during repeated HTTP requests. It helps avoid rebuilding connections for every request, reducing the latency caused by connection establishment and thus improving performance. This article analyzes a detailed comparison of connection establishment time with and without using curl_upkeep().
curl_upkeep() is a custom function (or a feature implemented by some libraries) designed to maintain persistent connections with the server. Its core role is to reduce the overhead of repeatedly establishing TCP connections through a persistent connection pool, which is especially effective when frequently sending requests to the same server.
By default, the HTTP protocol is stateless, meaning each request establishes a new TCP connection. To avoid reconnecting every time, parameters can be set to keep the connection alive. For example, curl uses the Connection: keep-alive header to maintain an active connection.
$ch = curl_init();
<p>// Set connection to keep alive<br>
curl_setopt($ch, CURLOPT_URL, '<a rel="noopener" target="_new" class="" href="https://m66.net/api/endpoint">https://m66.net/api/endpoint</a>');<br>
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br>
curl_setopt($ch, CURLOPT_HTTPHEADER, array(<br>
'Connection: keep-alive'<br>
));</p>
<p>// Execute request<br>
$response = curl_exec($ch);<br>
curl_close($ch);<br>
The above code keeps the connection with m66.net active, avoiding the need to establish a new connection for each request.
To better understand the performance gains brought by curl_upkeep(), below are some practical examples for comparison.
In this scenario, a new TCP connection is established for every request. This process usually involves DNS resolution, a three-way handshake, and other operations, which leads to longer time consumption.
$ch = curl_init();
<p>// Without persistent connection, a new connection is created for each request<br>
curl_setopt($ch, CURLOPT_URL, '<a rel="noopener" target="_new" class="" href="https://m66.net/api/endpoint">https://m66.net/api/endpoint</a>');<br>
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);</p>
<p>// Execute request<br>
$response = curl_exec($ch);<br>
curl_close($ch);<br>
Here, each time curl_exec() is called, curl establishes a new connection. Suppose DNS resolution and connection setup take about 200ms.
In this scenario, a connection pool is reused, avoiding reconnecting for each request. Setting options like CURLOPT_FORBID_REUSE and CURLOPT_KEEP_SENDING can help achieve connection reuse. Note that connection reuse only works for the same server; if you frequently access different domains, the benefit diminishes.
$ch = curl_init();
<p>// Set to keep connection alive<br>
curl_setopt($ch, CURLOPT_URL, '<a rel="noopener" target="_new" class="" href="https://m66.net/api/endpoint">https://m66.net/api/endpoint</a>');<br>
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br>
curl_setopt($ch, CURLOPT_FORBID_REUSE, true); // Disallow connection reuse<br>
curl_setopt($ch, CURLOPT_KEEP_SENDING, true); // Keep connection alive</p>
<p>// Execute request<br>
$response = curl_exec($ch);<br>
curl_close($ch);<br>
When reusing connections, if multiple requests need to be sent, the connection is reused, saving the time spent on DNS resolution, the three-way handshake, and other steps, significantly reducing the time per request.
Number of Requests | Without curl_upkeep() (ms) | With curl_upkeep() (ms) |
---|---|---|
1 | 200ms | 100ms |
10 | 2000ms | 1000ms |
100 | 20000ms | 10000ms |
As shown in the table above, reusing connections with curl_upkeep() significantly reduces the time spent on each request, and this advantage becomes more pronounced as the number of requests increases.
curl_upkeep() reduces the overhead of frequently establishing connections by maintaining and reusing TCP connections, thereby lowering latency and improving performance. This is especially beneficial in scenarios that require multiple requests to the same server.
Therefore, if your PHP project frequently communicates with the same server, it is recommended to use curl_upkeep() to optimize connection management and avoid unnecessary connection establishment time.