1

We are using an application server that supplies our data.

Sometimes the connection to the appserver takes a long time to respond, but there is not an actual connection failure (and the slowness registers as downtime on pingdom, even though its connected successfully)

How can I time the connection in php so that way I can log a slow-to-respond event after a certain amount of time (even if the connection has not responded yet)?

Kristian
  • 21,204
  • 19
  • 101
  • 176
  • You want to log it while it's loading or after? If it's after, you could always take the time before, take the time after, and if you end up with a successful connection, log it. – Rawr Aug 01 '12 at 16:05
  • why should it be in PHP? have a load balancer in between and configure it to do something if the connection takes too long. – eis Aug 01 '12 at 16:10
  • @eis, Ya ya ya, thats not what I'm asking. :) – Kristian Aug 01 '12 at 16:11
  • @rawr even if it has not yet finished, i'd like to still log it, if possible – Kristian Aug 01 '12 at 16:12
  • @Kristian I know, but I'd like to know the reasoning, because it might help with coming up a solution. Although I think it _should_ be what you are asking :) – eis Aug 01 '12 at 16:14
  • @Kristian just to be sure - so you're connecting _from_ PHP server to application server, or your PHP server _is_ the application server? – eis Aug 01 '12 at 16:16
  • @eis, in this scenario, php is the client and is connecting to a .NET appserver – Kristian Aug 01 '12 at 16:22

2 Answers2

1

Alright although, I must agree with eis on this one. PHP probably isn't the best language to be doing this in, here's my suggestion...

First File:

//Write "1" to text file (loading.txt)
//Run secondary php file (loading.php) asyncronously
//I would suggest curl_post_async or exec(url)

//Start Connection

//Upon load finish write "0" to text file (loading.txt)

Here's a nice discussion about requesting a page without waiting for it to load: php request url without waiting for response

Second File (loading.php):

$start=mktime();
while (true) {
    if ("loading.txt" if value = 0) {
        break;
    else if (($start-mktime()) > MAX WAIT TIME) {
        //Log as "slow-to-respond"
        break;
    }
}

You will prob need to modify your php.ini and set the max execution time accordingly.

Again I feel like this is kinda sloppy. I really would suggest using some other language.

Basically, right before you start the connection, you run this other php file which continues to loop until either 1. it reads from the loading.txt file that you've finished the connection OR 2. you're ready to log it as a "slow-to-respond" instance. In which case the 2nd php file ends and you've achieved your desired result.

Community
  • 1
  • 1
Rawr
  • 2,206
  • 3
  • 25
  • 53
1

One idea came to my head. At the beginning of the request, you could set up a database entry or a file that contains the request that you've made, similar to the suggestion that Rawr made. Then, you could trigger unix at command from PHP to be run after your limit has passed - that command would then activate a shell or a PHP script that would check the existance of the file/database entry and act accordingly.

This is equally hacky, but it would have the benefit of not having extra script running and polling stuff all the time, instead activating just once.

Community
  • 1
  • 1
eis
  • 51,991
  • 13
  • 150
  • 199
  • He makes a decent point, how often is this going to happen. If it's once a day or something, hacky might be okay. If it's like 30+ times in an hour a better method should be sought out. – Rawr Aug 01 '12 at 22:48