0

I have an application that accepts requests and process them.

I'm planning to add to this script execution a curl to forward the call to another service (for service migration and testing purposes)

I want to be sure that this request forwarding via curl won't slow down application performance or server in general.

Some pseudo code:

 //Step 1: existing code

 //handle the request
 $req = new requestHandler($_REQUEST);
 //process the request
 $req->process();


 //Step 2 (TODO): add request forwarding here (unique request do not need to perform multi curls)
 $forward = new requestForwarding($_REQUEST);
 $forward->fire();

 //step3 (don't want this to be delayed by step 2)
 $req->printAnswer();

What can I do at step 2 to not affect or minimise the general impact of the request forward?

Am I worrying for nothing and I can implement it with a simple curl? Or should I take some precautions?

Thanks

koalaok
  • 5,075
  • 11
  • 47
  • 91

3 Answers3

0

PHP Executes sequentually. Any long running code will block the output to the end-user (web browser). So if the curl call, say, takes 4 seconds, it will take 4 more seconds before the headers are sent to the client.

There are numeral ways to avoid this. By adding it to a job queue, and executing it from a cron job.

If you are running PHP as a stand alone, like PHP-FPM, you can use something like fastcgi_finish_request

You could also set up a worker that processes these requests, if you dont want them to block the response to the client.

There might be more solutions to this also. Just mentioning some of what I know.

Ole Haugset
  • 3,709
  • 3
  • 23
  • 44
0

Using standard CURL functionality could indeed slow down your script depending on the whole infrastructure (connection to server, requested server speed etc.). If your requested server is stable and an the same network, you might be worrying too much.

if

$req->printAnswer();

requires sth. from the request in step 2, you simply don't have a choice.

If not, there are several possibilities for you:

  • Ole Haugset just offered one possibility.
  • Maybe it is possible to change order of step 2 and 3?
  • Add a cronjob that works on a queue of requests and that is not part of your script that should not be delayed.
  • There are (advanced) possibilities to run curl async, see Async curl request in PHP

Just a few thoughts.

iquellis
  • 979
  • 1
  • 8
  • 26
0

When you have an input/output operation, an this is the case to make a request to an external site, is going to block the execution. Depends of the type of the script... could be done asyncronously. A good option for this is use GuzzleClient and set in async mode. http://docs.guzzlephp.org/en/stable/

luis moyano
  • 100
  • 4