1
0
Fork 0
mirror of https://github.com/seejohnrun/haste-server.git synced 2024-11-24 21:41:21 +00:00

fixed bug with connection limit

This commit is contained in:
Andrew Molchanov 2021-12-22 01:13:50 +03:00
parent c3c1f85ce1
commit dc6fccc9fe
No known key found for this signature in database
GPG key ID: 7C75AA362C5C27FC

View file

@ -1,5 +1,5 @@
const MongoClient = require('mongodb').MongoClient, const { MongoClient } = require("mongodb"),
winston = require('winston'); winston = require("winston");
const MongoDocumentStore = function (options) { const MongoDocumentStore = function (options) {
this.expire = options.expire; this.expire = options.expire;
@ -8,82 +8,103 @@ const MongoDocumentStore = function (options) {
}; };
MongoDocumentStore.prototype.set = function (key, data, callback, skipExpire) { MongoDocumentStore.prototype.set = function (key, data, callback, skipExpire) {
const now = Math.floor(new Date().getTime() / 1000), const now = Math.floor(new Date().getTime() / 1000);
that = this;
this.safeConnect(function (err, db) { this.safeConnect((err, db, client) => {
if (err) if (err) return callback(false);
return callback(false);
db.collection('entries').updateOne({ db.collection("entries").updateOne(
'entry_id': key, {
$or: [ entry_id: key,
{ expiration: -1 }, $or: [{ expiration: -1 }, { expiration: { $gt: now } }],
{ expiration: { $gt: now } } },
] {
}, {
$set: { $set: {
'entry_id': key, entry_id: key,
'value': data, value: data,
'expiration': that.expire && !skipExpire ? that.expire + now : -1 expiration:
} this.expire && !skipExpire ? this.expire + now : -1,
}, { },
upsert: true },
}, function (err, existing) { {
upsert: true,
},
(err /*, existing*/) => {
client.close();
if (err) { if (err) {
winston.error('error persisting value to mongodb', { error: err }); winston.error("error persisting value to mongodb", {
error: err,
});
return callback(false); return callback(false);
} }
callback(true); callback(true);
}); }
);
}); });
}; };
MongoDocumentStore.prototype.get = function (key, callback, skipExpire) { MongoDocumentStore.prototype.get = function (key, callback, skipExpire) {
const now = Math.floor(new Date().getTime() / 1000), const now = Math.floor(new Date().getTime() / 1000);
that = this;
this.safeConnect(function (err, db) { this.safeConnect((err, db, client) => {
if (err) if (err) return callback(false);
return callback(false);
db.collection('entries').findOne({ db.collection("entries").findOne(
'entry_id': key, {
$or: [ entry_id: key,
{ expiration: -1 }, $or: [{ expiration: -1 }, { expiration: { $gt: now } }],
{ expiration: { $gt: now } } },
] (err, entry) => {
}, function (err, entry) {
if (err) { if (err) {
winston.error('error persisting value to mongodb', { error: err }); winston.error("error persisting value to mongodb", {
error: err,
});
client.close();
return callback(false); return callback(false);
} }
callback(entry === null ? false : entry.value); callback(entry === null ? false : entry.value);
if (entry !== null && entry.expiration !== -1 && that.expire && !skipExpire) { if (
db.collection('entries').updateOne({ entry !== null &&
'entry_id': key entry.expiration !== -1 &&
}, { this.expire &&
!skipExpire
) {
db.collection("entries").updateOne(
{
entry_id: key,
},
{
$set: { $set: {
'expiration': that.expire + now expiration: this.expire + now,
},
},
() => {
client.close();
} }
}, function (err, result) { }); );
} }
}); }
);
}); });
}; };
MongoDocumentStore.prototype.safeConnect = function (callback) { MongoDocumentStore.prototype.safeConnect = function (callback) {
MongoClient.connect(this.connectionUrl, { useUnifiedTopology: true }, function (err, client) { MongoClient.connect(
this.connectionUrl,
{ useUnifiedTopology: true },
(err, client) => {
if (err) { if (err) {
winston.error('error connecting to mongodb', { error: err }); winston.error("error connecting to mongodb", { error: err });
callback(err); callback(err);
} else { } else {
callback(undefined, client.db(this.connectionName)); callback(undefined, client.db(this.connectionName), client);
} }
}); }
);
}; };
module.exports = MongoDocumentStore; module.exports = MongoDocumentStore;