S
S
SlavaMorg2021-09-01 14:49:13
PHP
SlavaMorg, 2021-09-01 14:49:13

How can I understand that I really managed to make a keep-alive connection on curl?

Good afternoon.
Inspired by the article https://habr.com/en/post/184302/ , I decided to redo a couple of classes that are designed to integrate the site with 1c. The essence is simple: when the user has entered all the data on the site, 5 requests are sent to the 1C server to create / check some entities.
Before "optimization": For each of the 5 requests, curl_init was executed before the request and curl_close after the request.
After "optimization": Made a Singleton class (or something like that) that always returns the same curl handler. Then each of the request classes puts its url and some put post-fields (and other get requests), and send requests. And in the end, of course, we don’t destroy anything, because in theory, after execution, everything should be cleared by itself.
Requests use information from each other, so there is no point in parallelizing them.

I timed the time, it decreased a little, but still I didn’t fully understand whether I managed to create a permanent connection or not. Explain who understands. I used the CURLOPT_VERBOSE option to get everything logged, but I'm not sure if I'm reading the logs correctly.
curl log before optimization - https://codepen.io/Aris96/pen/jOwqLER
curl log after optimization - https://codepen.io/Aris96/pen/RwgaZWx
Server IP changed to ***host***

Another thing that worries me is that I don't use the CURLOPT_FORBID_REUSE and CURLOPT_TCP_KEEPALIVE options. Tried to use it but nothing changed. I don’t understand whether they need to be installed and what their default values ​​are. At the moment, without url and post-fields, my settings look like this:

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
            curl_setopt($ch, CURLOPT_USERPWD, AbstractRequest::ONE_C_LOGIN . ':' . AbstractRequest::ONE_C_PASSWORD);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            curl_setopt($ch, CURLOPT_STDERR, $file);
            curl_setopt($ch, CURLOPT_VERBOSE, true);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Syomov, 2021-09-02
@SlavaMorg

Re-using existing connection! (#0) with host ***host***
* Connected to ***host*** (***host***) port 80 (#0)

Here you can see that the connection is being reused.
CURLOPT_FORBID_REUSEBy default, it is not active and it is not necessary to set it, why do you need to prohibit the reuse of connections?
CURLOPT_TCP_KEEPALIVEThis is the sending of packets to maintain a connection. In your case, it is hardly necessary - it makes sense if requests are rare, and more time can elapse between them than the keepalive timeout on the server (usually tens of seconds).
The fact that the difference is not great is not surprising. It is very noticeable if there are many requests with very fast responses, where the connection setup time takes a significant amount of time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question