Reputation: 926
I'm using the following script to asynchronously submit instant messages to my database.
function sendMessage(content, thread_id, ghost_id)
{
var url = "ajax_submit_message.php";
var data = { content: content, thread_id: thread_id };
var type= "POST";
$.ajax({
url:url,
type:type,
data:data,
success:function(){ unGhost(ghost_id); refreshMessages(); },
error:function(){alert("FAIL");}
});
}
This behaves exactly as expected under normal conditions, but when I added a 'sleep(10)' to my PHP script to try to find out how it would function under heavy server load, I found that the script could only handle a few requests before it would abort.
The script will finish three successful requests, but on the fourth it will fail every time. It doesn't call the error function, but instead calls the success function. I'm at a loss as to how to debug this, as no kinds of errors or exceptions are being thrown anywhere. Can anyone tell me what might be causing this?
Edit: Due to request here is how I'm calling the function:
function checkKey(e)
{
if (e.keyCode == 13)
{
//Get message and thread_id
var message_field = $(':focus');
//Validate and escape content
var content = message_field.val();
if (content == "") {return;}
//Grab thread_id
var id = message_field.attr('id');
var last_underscore = id.lastIndexOf("_");
var thread_id = id.substring(last_underscore+1);
//Clear input
message_field.val("");
//Add message to page
var ghost = insertGhost(thread_id,content,window.user_id);
//Send message on to server
sendMessage(content,thread_id,ghost);
}
}
Upvotes: 1
Views: 483
Reputation: 926
After some poking around, I found that my PHP script was in fact timing out, and responding with the message:
Maximum execution time of 30 seconds exceeded.
Which makes sense, since that's the kind of thing I was testing for. The only way I found of catching this error was to have my PHP script output "TRUE" on success, and to format my AJAX script as follows:
$.ajax({
url:url,
type:type,
data:data,
success:function(msg)
{
if ($.trim(msg) != "TRUE")
{
//graceful error handling magic
}
unGhost(ghost_id);
refreshMessages();
},
error:function(){alert("FAIL");}
});
Not the most elegant thing I've ever done, but it should allow for me to accommodate for situations where the server is being overloaded.
Thanks for the help guys!
Upvotes: 0
Reputation: 2511
If you are polling at regular intervals (rather than manually queueing polls after each actions) you're probably running into timeout behavior. Probably the 4th is not failing, actually the 4th is succeeding, and the 5th is silently failing.
Here's why: The browser will allow a maximum of 2 concurrent requests to a given host at a time. Any additional requests will be put into a queue that's transparent to your application. However, these AJAX requests time out. So the first two are fine. The next two have to wait a little while, but they get sent and received before the timeout applies. The subsequent ones were added to the browser's queue, but since they timed out before they could even be sent, you see no activity.
You could test this by making sure to do a setTimeout on success/failure, or some other method of managing how many pending requests you have going at any give
Upvotes: 3