0

Recently my Apache server's CPU has been at 100% (all day, that is). I think I have found my issue.

On a page, a user has the ability to click a button and send a POST request to a PHP file on my server. Sometimes the PHP file takes a LONG (and I mean VERY long) time to respond or sometimes does not respond at all:

function buttonFunction() {
    $.post("http://ipaddress/core/file.php",{username:username, password:pword, coins:coins}, function(data) {
        // Stuff
    });
}

Hypothesis 1:

I believe that sometimes people might click this button again (while it is still trying to get the result/response from file.php from the previous click), and therefore causing two simultaneous processes from PHP on the Apache server - causing higher CPU usage (I think that's what happens, correct me if I'm wrong because I'm new to this server stuff).

Hypothesis 2:

Another thing that may be causing the high CPU usage (that I believe) is the user refreshing the page while it is still trying to get the result/response from file.php. After 12 seconds (with no response/result), I have a message appear saying "Please refresh if this takes too long." With that being the case (after refreshing the page), the user once again tries to send a post request to file.php while the old one may still be running - causing higher CPU usage (again, I think that's what happens, correct me if I'm wrong because I'm new to this server stuff).

Reasoning:

I'm saying this because on my site it may say that there are only 12 people online (and probably 12 people sending the post requests), however when I run the top command on PuTTY to see what processes are currently running, it shows nearly 30-40+ processes running (some taking as long as 17 minutes).

So, is there a way that I can abort the request on a refresh (if it's still going on) or on the click on the button (again, if the request is still going on)? In fact, can somebody either confirm or deny if my hypotheses (especially hypothesis 2) are correct - if those actually ARE causing the high CPU? Furthermore, if anybody has an idea for a more efficient way that I can go about this (sending these requests), it would be highly appreciated.

Edit 1:

I can fix the possible issue stated in my first hypothesis. However, can somebody please either confirm or deny if my second hypothesis is true/valid?

user3681788
  • 221
  • 4
  • 15
  • 1
    `So, ... or on the click on the button...` Disable the button immediately after it is clicked. – afaolek Jul 22 '14 at 12:16
  • @afaolek Yes, that was my first thought. – user3681788 Jul 22 '14 at 12:17
  • i would put as many locks around `file.php` so that only one request happens per whatever. – Daniel A. White Jul 22 '14 at 12:50
  • The file process is ABSOLUTELY still running when the page is refreshed to send a second request. There is no (reasonable) way of killing a PHP process on the server, when the client refreshes the script. – Mark Jul 22 '14 at 12:51
  • you can use db or client side storage to restrict user to request again when one request is in process – Shovan Jul 22 '14 at 12:54
  • But doing as @ShovanSahu says will still require you to make the second request to see if the first one is open. – Mark Jul 22 '14 at 12:56
  • if it is client storage, it need not to make a server request – Shovan Jul 22 '14 at 12:58
  • @ShovanSahu But if it's client side, the user can modify it to make more requests. The browser is always in the hands of the enemy. – Mark Jul 22 '14 at 13:13
  • 1
    correct this problem first: `Sometimes the PHP file takes a LONG (and I mean VERY long) time to respond or sometimes does not respond at all` BTW: why is this irregular? – Saic Siquot Jul 22 '14 at 14:21
  • I would agree with @LuisSiquot. The real problem here is with the PHP file, not with the UI handling or multiple requests. – Mark Jul 22 '14 at 15:18

0 Answers0