I have a PHP application that receives a lot of ajax calls.
I noticed that when two or more calls happen at the same time, they are not being executed at the same time, the first one must stop and the second one is being executed.
I developed a small test to try to fix this problem. I have html:
<html> <head> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> </head> <body> <a href="test.php">Test</a> <button>Click</button> <button>Click</button> <button>Click</button> <button>Click</button> <script type="text/javascript"> $('button').click(function() { $.get("test.php"); }); </script> </body> </html>
The file test.php contains:
<?php sleep(2); echo 'Test'; ?>
So, after executing $ ('button'). click () on the developer's console (or firebug) requests are executed at the same time, but return answers 2 seconds after the last one, and not all four that return at the same time (2 seconds).
Well, I tried to open this file in other browsers, at the same time, and it worked, it was executed at the same time. I mean, by running this on chrome and firefox, opening the page in a tab, show "Test" in 2 seconds, and the responses from chrome ajax requests come 2 seconds after the last request is completed, so another browser request does not affect the first one call (which proves that this is a session problem, but see below).
The same thing happens if I open several tabs for test.php.
I tried changing the session to memcache. People said in other matters that the session might be blocked. This makes a lot of sense, but even after changing the files on memcache, the problem persists (and the script has no session, and session_autostart is disabled)
I really understand the configuration problem with apache, since for the first time after a while, if I execute the script, it will make all 4 ajax calls at once! I am thinking of some kind of DoS protection or something like that. In any case, any help would be greatly appreciated!
Ok, solved the problem.
In the end, it was a session blocking problem, the problem is that memcached is also blocking the session!
You will have to install php_memcached 2.0.1 with the lock flag in memcached.ini and use this version. Solved the problem.
Although when the request refers to the same URL (in the above example, I used test.php 4 times), it still executes one at a time. I am sure that the correct behavior, since it does not make sense to repeatedly request a resource. My example for request test1.php, test2.php etc. has been changed, And it worked fine. Also works if the string changes.
Thanks for your help!
PS: I have no reputation to answer my own question so early, and here is the answer