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.
在正常情况下,这完全符合预期,但是当我在PHP脚本中添加了一个“sleep(10)”,试图找出它在重载服务器负载下的功能时,我发现脚本只能处理几个请求,然后才会中止。
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);
}
}
2 个解决方案
#1
3
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.
原因如下:浏览器一次最多允许向给定的主机发出两个并发请求。任何额外的请求都将被放入一个对您的应用程序透明的队列中。然而,这些AJAX请求超时。前两个没问题。接下来的两个必须等待一段时间,但是它们会在超时之前被发送和接收。随后的代码被添加到浏览器的队列中,但是由于它们在可以发送之前超时,所以您看不到任何活动。
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
您可以通过确保对成功/失败执行一个setTimeout来测试它,或者使用其他方法来管理在任何给定情况下有多少等待请求
#2
0
After some poking around, I found that my PHP script was in fact timing out, and responding with the message:
在进行了一些探索之后,我发现我的PHP脚本实际上是超时的,并对消息进行了响应:
Maximum execution time of 30 seconds exceeded.
超过最大执行时间30秒。
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:
这是有道理的,因为这是我要测试的东西。我发现捕获这个错误的唯一方法是在成功时让我的PHP脚本输出“TRUE”,并将我的AJAX脚本格式化如下:
$.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!
谢谢你们的帮助!
#1
3
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.
原因如下:浏览器一次最多允许向给定的主机发出两个并发请求。任何额外的请求都将被放入一个对您的应用程序透明的队列中。然而,这些AJAX请求超时。前两个没问题。接下来的两个必须等待一段时间,但是它们会在超时之前被发送和接收。随后的代码被添加到浏览器的队列中,但是由于它们在可以发送之前超时,所以您看不到任何活动。
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
您可以通过确保对成功/失败执行一个setTimeout来测试它,或者使用其他方法来管理在任何给定情况下有多少等待请求
#2
0
After some poking around, I found that my PHP script was in fact timing out, and responding with the message:
在进行了一些探索之后,我发现我的PHP脚本实际上是超时的,并对消息进行了响应:
Maximum execution time of 30 seconds exceeded.
超过最大执行时间30秒。
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:
这是有道理的,因为这是我要测试的东西。我发现捕获这个错误的唯一方法是在成功时让我的PHP脚本输出“TRUE”,并将我的AJAX脚本格式化如下:
$.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!
谢谢你们的帮助!