2014-04-18 624 views
3

我有一个页面,允许用户上传和映射CSV文件。在完成之后,行将通过后续调用发送到不同服务器上的页面。经过近6,00次调用(5787正好)后,我开始出现控制台错误“无法加载资源:net :: ERR_INSUFFICIENT_RESOURCES”。jQuery post调用导致“无法加载资源:net :: ERR_INSUFFICIENT_RESOURCES”

我试着在CSV文件中运行100行的页面,它工作得很好......但是当我尝试一个大列表(超过10,000行)时,它冻结了。

这里是使得后调用的代码:

for (var i = 0; i < manifestRows.length; i++) 
{ 
    $.post('http://www.otherserver.com/subfolder/handler.php', { tblName: 'foobar', thing1: manifestRows[i][0], thing2: manifestRows[i][1], thing3: manifestRows[i][2], thing4: manifestRows[i][3], thing5: manifestRows[i][4], thing6: manifestRows[i][5], thing7: manifestRows[i][6], thing8: manifestRows[i][7], thing9: manifestRows[i][8], thing10: manifestRows[i][9], thing11: manifestRows[i][10], thing12: manifestRows[i][11], thing13: manifestRows[i][12], thing14: manifestRows[i][13], thing15: manifestRows[i][14], thing16: manifestRows[i][15], thing17: manifestRows[i][16], thing18: manifestRows[i][17] }, function(data) { 
    if (data.length == 0) 
    { 
     var currentProcessing = $('#processingCurrent').html(); 
     $('#processingCurrent').html(parseInt(currentProcessing) + 1); 
     var progress = Math.ceil((parseInt(currentProcessing)/manifestRows.length) * 100); 
     if (progress == 99) 
      progress = 100; 
     progress = progress + '%' 
     $("#progressBar").width(progress).html(progress); 
     if (progress == '100%') 
     { 
      $('#processdingDiv').hide(); 
      $('#allDone').show(); 
     } 
    } 
    else 
     alert(data); 
    }); 
} 

有一些代码,我可以把无论是在用户端,或其他的服务器以防止此Insuffiecient资源错误的存在的呢?

回答

2

我在AngularJS应用程序中遇到了几乎完全相同的错误。我所要做的就是批量处理这些请求。批号与您的实例相关,但我一次使用了1000个呼叫。

由于很多这是异步发生的,我不得不创建两个变量。根据您的代码,httpRequestsExpected应该与批量大小相同。在我的,我只是增加了它,每次我打电话给$http.$get();以获得确切的价值。

var batchSize = 1000;
var httpRequestsExpected;
var httpRequestsMade = 0;

然后在HTTP 成功和错误功能,增加httpRequestMade

要解决一批,因为HTTP有时会挂起,我不能做精确匹配,如:
if(httpRequestsMade === httpRequestsExpected)
但不得不提的填充这样的:
if(httpRequestsMade >== httpRequestsExpected - (httpRequestsExpected *0.01))

再开始下一批处理,将起始指针递增batchSize并重置变量。这为进程提供了一个安全的缓冲时间和完成时间,而不消耗所有资源。