Encapsulate curl_ Multi allows requests to be executed in parallel with business logic

Time:2021-7-30

Other requests are required in PHP developmentHTTP (s) service interfaceMany people will directly use the built-incurlTools. amongcurl_multiYou can combine the previous individualCurl requestIt is added to a batch to execute in parallel, which is implemented in terms of time overheadMax (interface 1, interface 2, interface 3...)It can reduce the sense of waiting for users when our business needs multi service data aggregation.

So what can this article bring?

  • Time optimization is more thanMax (interface 1, interface 2, interface 3...), reachableMax (business code, interface 1, interface 2, interface 3...), so that the waiting period after the request can also be used to execute business code.
  • yescurl curl_multiFriendly use of encapsulation, object encapsulation to simplify all kinds of codecurl_*Memory and clutter of functions.

environment

  • Win10 x64PHP8.0.0 TS x64

preface

  • curl_multiCreate a batch and transfer multiplecurlHandle is added and the batch is executed
  • curl_multi_exec()Handle to perform the operation required in the batch.
    • asSend request/Receive processing response, no action is required while waiting for a response
  • curl_multi_select()Block wait untilPending handle/overtime
    • If a handle receives a response.
    • This function can avoid dead cycle idling exec during the waiting period. The thread should be in a suspended state and awakened by the underlying trigger.

analysis

  • The core of optimization iscurl_multi_exec()andcurl_multi_select()
  • Debug execution found throughcurl_multi_exec()The first time is to send HTTP request message. It is speculated that the execution granularity of exec is the processing of HTTP request / response message.
  • Therefore, we can first send the request, then execute our own business logic, and finally obtain the execution result when necessary. In this way, the waiting time for response can be reused.
  • becauseredirect/HTTPSWill be involved many timesHTTP messageInteraction, optional optimization parameters are provided in the encapsulation as far as possible.

Test results (figure below)

  • Encapsulation & test code warehousegitee.com/VwenX/curl-mut
  • Local usenodeOpened ahttpServer (the code is shown in the figure below, and it can be used if you look for it on the Internet)
  • step
    • targetHTTP serviceWaiting after receiving the request is set in300msThe response is made only after, and the actual waiting time is output in the response
    • PHP requests three interfaces as a client and simulates the execution of business for 1 second at the same time
    • Finally, we can see that the time consumption of the three interfaces is more than 310ms.
    • If it is called as usual, the overall time consumption isMax (a, B, c) + business=Over 1317ms. However, the overall time-consuming after our processing is only 1017ms, and the three 310 + MS requests hardly take up any time overhead!

Test result diagram

Encapsulate curl_ Multi allows requests to be executed in parallel with business logic

This work adoptsCC agreement, reprint must indicate the author and the link to this article