[英]Connection goes idle and disconnects Mongodb from AWS Lambda
MongoDB 空閑問題讓我做噩夢。 它無緣無故地失敗,它不會觸發斷開事件或錯誤。 它只是在 API 調用過程中斷開連接。
我也嘗試過閱讀博客和舊問題並嘗試了所有方法,但情況仍然沒有任何幫助。
這是我的數據庫連接代碼:
function connect () {
return mongoose.connect(process.env.MONGO_DB_URI, {
useNewUrlParser: true,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 1000,
bufferMaxEntries: 0, // and MongoDB driver buffering
bufferCommands: false, // Disable mongoose buffering
ha: true, // Make sure the high availability checks are on
haInterval: 10000 // Run every 10 seconds
})
}
if (process.env.MONGO_DB_URI) {
connect()
}
有了它,我還記錄了數據庫的幾個事件。
db.on('connecting', () => {
debug(`mongoose : connecting `)
})
db.on('connected', () => {
debug(`mongoose : connected`)
})
db.on('open', () => {
debug(`mongoose : open`)
})
db.on('disconnecting', () => {
debug(`mongoose : disconnecting`)
})
db.on('disconnected', () => {
debug(`mongoose : disconnected`)
})
db.on('close', () => {
debug(`mongoose : close`)
})
db.on('reconnected', () => {
debug(`mongoose : reconnected`)
})
db.on('error', (err) => {
debug(`mongoose : error`, err)
})
db.on('all', () => {
debug('mongoose: all')
})
db.on('fullsetup', () => {
debug(`mongoose : fullsetup`)
})
同樣在 API 調用之前,我從中間件檢查數據庫連接。
app.use(async (req, res, next) => {
log.info('Ready State: ' + mongoose.connection.readyState, {
action: 'App.ConnectionCheck'
})
if (mongoose.connection.readyState !== 1) {
log.info('try to reconnect', {
action: 'App.ConnectionCheck'
})
await connect()
next()
} else if (mongoose.connection && mongoose.connection.db) {
await Promise.race([
mongoose.connection.db.admin().ping(),
new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('Race condition failed'))
}, 1000)
})
])
.then(async d => {
log.info('ping successful: ', d)
if (d.ok !== 1) {
await connect()
}
})
.catch(async e => {
log.error('Ping Failed ', {
error: e
})
await connect()
})
next()
} else {
next()
}
})
但有時仍然會打來電話,並進行第一次查詢,但我沒有得到回復,我的 API 超時。
這就是我的日志的樣子,請注意重新連接所需的時間。
2020-08-10 16:30:55.465 (+05:00) 34c5812c-5446-4c0f-bddf-c4ca8a0117cb INFO ----------------- BODY END ------------
2020-08-10 16:30:55.466 (+05:00) 34c5812c-5446-4c0f-bddf-c4ca8a0117cb INFO Mongoose: meetings.countDocuments({ appntId: '<APP_ID_HERE>', '$or': [ { deleted: true }, { createdAt: { '$gt': new Date("Mon, 10 Aug 2020 11:30:45 GMT") } {})
Mon, 10 Aug 2020 11:30:55 GMT timify:db-handle:index mongoose : disconnected
Mon, 10 Aug 2020 11:31:15 GMT timify:db-handle:index mongoose : connected
Mon, 10 Aug 2020 11:31:15 GMT timify:db-handle:index mongoose : reconnected
END RequestId: 34c5812c-5446-4c0f-bddf-c4ca8a0117cb
END RequestId: 34c5812c-5446-4c0f-bddf-c4ca8a0117cb
REPORT RequestId: 34c5812c-5446-4c0f-bddf-c4ca8a0117cb Duration: 30030.13 ms Billed Duration: 30000 ms Memory Size: 10
Max Memory Used: 172 MB
版本:
我看過另一篇關於這個問題的帖子,但我的環境不同,這個問題是從 2016 年開始的。
我在 AWS Lambda 上運行 Express 應用程序時遇到了類似的問題。
我得到的錯誤信息是:
MongooseError: Cannot call `users.find()` before initial connection is complete if `bufferCommands = false`. Make sure you `await mongoose.connect()` if you have `bufferCommands = false`.
我當前有效的連接設置是
mongoose
.connect(mongoConnectionString, {
useNewUrlParser: true ,
useCreateIndex: true,
useUnifiedTopology: true,
keepAlive : true,
bufferMaxEntries: 0 // and MongoDB driver buffering
}
);
通過反復試驗,我設法找到了連接錯誤
緩沖區命令:假
命令並對其進行評論使其對我有用。
通過有關緩沖選項的mongoose 文檔,我找不到此問題背后的任何原因,但您可以嘗試通過以下方式全局關閉緩沖,
mongoose.set('bufferCommands', false);
希望這個解決方案有所幫助。
編輯:將 bufferCommands 全局設置為 false,導致相同的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.