Parallel Apache connections coming from the same client


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

+6
source share
2 answers

in my case function:

session_write_close();

solved my problem

I have the following program, the main page, which calls the same ajax file called ajax.php . It is called every "n" seconds.

And in my form there is a button that returns the report that he called through ajax (** reporte.php **). It is called only at the user's request (click), and it may take a minute to get the result.

In most cases, ajax.php takes 20 ms to start, but if report.php works, ajax.php freezes until the reporte.php task completes. And worst of all, ajax.php can be stacked.

reporte.php (before)

 <?php // Some PHP Code goes here. // The rest of the PHP code (mostly, the slow part of the process). ?> 

reporte.php (after correction)

 <?php // Some PHP Code goes here. (ideal if it is the fast part). session_write_close(); // fix for concurrent ajax // The rest of the PHP code (mostly, the slow part of the process). ?> 

Before solving (screenshot of Firefox Firebug plugins): before

After decision after

Check, as before the solution, reporte.php and ajax.php both are still loading (and ajax fits into several calls). And as after the solution, reporte.php loads while all consecutive ajax calls are already loaded.

Note that after the session_write_close () function, it is impossible (or recommended) to use any function and session value.

+6
source

HTTP requests are independent of each other - by definition, the HTTP protocol has no status. Each $.get() that you execute is a completely separate and new HTTP request, so you get 4 HTTP requests, 4 separate sleep() calls, and 4 separate server exits.

-2
source

Source: https://habr.com/ru/post/912875/


All Articles