From dc6fccc9fe98812a1605ba18dfdc28f196a91258 Mon Sep 17 00:00:00 2001 From: Andrew Molchanov Date: Wed, 22 Dec 2021 01:13:50 +0300 Subject: [PATCH] fixed bug with connection limit --- lib/document_stores/mongodb.js | 147 +++++++++++++++++++-------------- 1 file changed, 84 insertions(+), 63 deletions(-) diff --git a/lib/document_stores/mongodb.js b/lib/document_stores/mongodb.js index 456fc4c..0f4ac03 100644 --- a/lib/document_stores/mongodb.js +++ b/lib/document_stores/mongodb.js @@ -1,5 +1,5 @@ -const MongoClient = require('mongodb').MongoClient, - winston = require('winston'); +const { MongoClient } = require("mongodb"), + winston = require("winston"); const MongoDocumentStore = function (options) { this.expire = options.expire; @@ -8,82 +8,103 @@ const MongoDocumentStore = function (options) { }; MongoDocumentStore.prototype.set = function (key, data, callback, skipExpire) { - const now = Math.floor(new Date().getTime() / 1000), - that = this; + const now = Math.floor(new Date().getTime() / 1000); - this.safeConnect(function (err, db) { - if (err) - return callback(false); + this.safeConnect((err, db, client) => { + if (err) return callback(false); - db.collection('entries').updateOne({ - 'entry_id': key, - $or: [ - { expiration: -1 }, - { expiration: { $gt: now } } - ] - }, { - $set: { - 'entry_id': key, - 'value': data, - 'expiration': that.expire && !skipExpire ? that.expire + now : -1 + db.collection("entries").updateOne( + { + entry_id: key, + $or: [{ expiration: -1 }, { expiration: { $gt: now } }], + }, + { + $set: { + entry_id: key, + value: data, + expiration: + this.expire && !skipExpire ? this.expire + now : -1, + }, + }, + { + upsert: true, + }, + (err /*, existing*/) => { + client.close(); + + if (err) { + winston.error("error persisting value to mongodb", { + error: err, + }); + return callback(false); + } + + callback(true); } - }, { - upsert: true - }, function (err, existing) { - if (err) { - winston.error('error persisting value to mongodb', { error: err }); - return callback(false); - } - - callback(true); - }); + ); }); }; MongoDocumentStore.prototype.get = function (key, callback, skipExpire) { - const now = Math.floor(new Date().getTime() / 1000), - that = this; + const now = Math.floor(new Date().getTime() / 1000); - this.safeConnect(function (err, db) { - if (err) - return callback(false); + this.safeConnect((err, db, client) => { + if (err) return callback(false); - db.collection('entries').findOne({ - 'entry_id': key, - $or: [ - { expiration: -1 }, - { expiration: { $gt: now } } - ] - }, function (err, entry) { - if (err) { - winston.error('error persisting value to mongodb', { error: err }); - return callback(false); + db.collection("entries").findOne( + { + entry_id: key, + $or: [{ expiration: -1 }, { expiration: { $gt: now } }], + }, + (err, entry) => { + if (err) { + winston.error("error persisting value to mongodb", { + error: err, + }); + client.close(); + return callback(false); + } + + callback(entry === null ? false : entry.value); + + if ( + entry !== null && + entry.expiration !== -1 && + this.expire && + !skipExpire + ) { + db.collection("entries").updateOne( + { + entry_id: key, + }, + { + $set: { + expiration: this.expire + now, + }, + }, + () => { + client.close(); + } + ); + } } - - callback(entry === null ? false : entry.value); - - if (entry !== null && entry.expiration !== -1 && that.expire && !skipExpire) { - db.collection('entries').updateOne({ - 'entry_id': key - }, { - $set: { - 'expiration': that.expire + now - } - }, function (err, result) { }); - } - }); + ); }); }; MongoDocumentStore.prototype.safeConnect = function (callback) { - MongoClient.connect(this.connectionUrl, { useUnifiedTopology: true }, function (err, client) { - if (err) { - winston.error('error connecting to mongodb', { error: err }); - callback(err); - } else { - callback(undefined, client.db(this.connectionName)); + MongoClient.connect( + this.connectionUrl, + { useUnifiedTopology: true }, + (err, client) => { + if (err) { + winston.error("error connecting to mongodb", { error: err }); + callback(err); + } else { + callback(undefined, client.db(this.connectionName), client); + } } - }); + ); }; module.exports = MongoDocumentStore;