2014-01-08 66 views
0

我使用nodejs構建一個http服務器。每個請求收入,http服務器連接到memcache,檢查並返回數據或做其他事情。當我使用Apache Benchmark測試性能時,如:-n 1000.輸出顯示它剛剛完成了約900個請求,然後http服務器不能再服務。 我在做什麼錯?你能解釋發生了什麼?Nodejs HTTP服務器連接memcache

這裏我的代碼:

http.createServer(function(req, res) { 
    var client = new mc.Client(ip, mc.Adapter.binary); 
    client.connect(function() { 
    client.get(key, function(err, response) { 
    if(err) { //not exist 
     .. 
     res.end(result) 
    .. 
    } 
    else { //exist in memcache 
     var result = response[key]; 
     res.end(result); 
    } 
}); 

}); }); 這裏是benchmarch後的結果是:

This is ApacheBench, Version 2.4.1 <$Revision: 655654 $> 
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ 
Licensed to The Apache Software Foundation, http://www.apache.org/ 

Benchmarking 127.0.0.1 (be patient) 
Completed 100 requests 
Completed 200 requests 
Completed 300 requests 
Completed 400 requests 
Completed 500 requests 
Completed 600 requests 
Completed 700 requests 
Completed 800 requests 
apr_poll: The timeout specified has expired (70007) 
Total of 896 requests completed 

更新:我想通了,我也不要斷開memcached的每個請求。問題解決了!!!

+0

我認爲你的if-else邏輯是不正確的。錯誤並不意味着密鑰不存在。當key不存在時,我相信nil會返回。錯誤是由於處理給定的命令失敗。 – user568109

+0

是的,我知道,但是當我使用MC模塊和密鑰不存在它打印錯誤和響應也是空的。如果無法連接memcache,我在mc模塊中找不到任何api,例如timeout。所以我必須先啓動memcache。 – Windranger

回答

0

nodejs應用程序的一個棘手問題是,有一個1.4GB的內存上限。如果您的服務器使用超過允許的限制,它將掛起或崩潰。您可以自定義構建您的nodejs應用程序以增加此值。

確保你垃圾收集你的變量如果不使用(如設置一些模塊範圍變量爲null)。

我沒有足夠的信息來告訴你哪裏出了問題。我會建議看看服務器日誌。

+0

我更新了我的代碼。我不認爲這是內存不足,似乎服務器無法從memcache獲取數據了.. – Windranger