2014-04-18 133 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並重置變量。這爲進程提供了一個安全的緩衝時間和完成時間,而不消耗所有資源。