2016-05-17 86 views
1

我在Amazon Web Services中使用Lambda函數發現了一個奇怪的行爲。關閉Mongoose連接Lambda

我使用節點4.3和貓鼬4.4.17

的想法是測試,並與LAMBDA的能力發揮。

我做了一個簡單的模型,我將它存儲在一個Ec2實例中。代碼工作正常,直到我嘗試關閉連接。我知道,更好的做法是說:「不要關閉你的連接,讓游泳池處理。」那麼,這適用於一個正常的應用程序,但Lambda是一個無狀態函數,所以如果我不關閉連接,這會保持開放,消耗資源。如果每秒有數千個請求,這可能會非常糟糕。

所以,這是我的代碼。

'use strict'; 
let mongoose = require('mongoose'); 
//I add this options, because this close my connections 
//faster than the 30 min by default 
let options = { server: { socketOptions: { keepAlive: 30000, connectTimeoutMS: 30000 } }}; 
let db = mongoose.createConnection('mongodb://myInternalServerEC2:27017/myDB', options); 
let Schema = require('mongoose').Schema; 
let TempSchema =new Schema({name:{type:String,required:true}}); 
//This is a copy paste from an another project, 
//but i can remove, but i don't think this has nothing 
//with my problem. 
personSchema.set('autoIndex', false); 
personSchema.index({name:1}); 

let tempDB = db.model('tempcol', TempSchema); 
exports.handler = (event, context, callback) => { 
    tempDB.find(function (err, data) { 
     if (typeof(data) === 'object' && data.length === 0) { 
      data = null; 
     } 
     if (!err && data !== null) { 
      callback(null, data); 
     } else if (!err) { 
      error = new Error("No data found"); 
      callback(error); 
     } else { 
      callback(err); 
     } 
    }).populate('_typeId'); 
}; 

此代碼沒有問題。

現在...讓我們嘗試關閉連接。 (哈哈)

我在IFS的任何情況下使用,在若結束後,如果發現功能裏面,等

db.close(); 
callback(null, data); 

mongoose.disconnect(); 
callback('Some error');  

//This finish inside the find function 

finish(db, function(){ 
    callback(error, data); 
}); 
// A finish function with a callback, 
// so i can call the parent callback 
function finish(db, cb){ 
    db.close(function(){ 
     cb(); 
    }); 
} 

在每一個案件。 Lambda函數永遠不會返回錯誤,只返回NULL。

任何人都有一些線索爲什麼這種行爲在Lambda?在本地模式下,這種行爲從來沒有發生過我。

如果我刪除了關閉指令,lambda函數提前

回答

3

從我蒙戈服務器

THKS返回數據,我發現這個問題。

問題在於上下文。和回調。我更改代碼以在處理程序中包含createConnection事件。

https://aws.amazon.com/es/blogs/compute/getting-nodejs-and-lambda-to-play-nicely/

此代碼有效。

'use strict'; 
let mongoose = require('mongoose'); 
let options = { server: { socketOptions: { keepAlive: 30000, connectTimeoutMS: 30000 } }}; 

let Schema = require('mongoose').Schema; 
let TempSchema =new Schema({name:{type:String,required:true}}); 
TempSchema.set('autoIndex', false); 
TempSchema.index({name:1}); 


exports.handler = (event, context) => { 
    let db = mongoose.createConnection('mongodb://myInternalServerEC2:27017/myDB', options); 
    let tempDB = db.model('tempcol', TempSchema); 

    function closeBD(cbk){ 
     console.log("Close BD"); 
     db.close(function(){ 
      cbk(); 
     }); 
    } 
    tempDB.find(function (err, data) { 
     if (typeof(data) === 'object' && data.length === 0) { 
      data = null; 
     } 
     if (!err && data !== null) { 
      context.succeed(data); 
     } else if (!err) { 
      let error = new Error("No data found"); 
      context.fail(error); 
     } else { 
      context.fail(err); 
     } 
     closeBD(function(){ 
      context.done(); 
     }); 
    }); 
}; 

希望有人找到這個有用的。