1
0
Fork 0
mirror of https://github.com/seejohnrun/haste-server.git synced 2024-11-24 13:31: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:
'expiration': that.expire && !skipExpire ? that.expire + now : -1 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) { 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", {
winston.error('error persisting value to mongodb', { error: err }); error: err,
return callback(false); });
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) { MongoDocumentStore.prototype.safeConnect = function (callback) {
MongoClient.connect(this.connectionUrl, { useUnifiedTopology: true }, function (err, client) { MongoClient.connect(
if (err) { this.connectionUrl,
winston.error('error connecting to mongodb', { error: err }); { useUnifiedTopology: true },
callback(err); (err, client) => {
} else { if (err) {
callback(undefined, client.db(this.connectionName)); winston.error("error connecting to mongodb", { error: err });
callback(err);
} else {
callback(undefined, client.db(this.connectionName), client);
}
} }
}); );
}; };
module.exports = MongoDocumentStore; module.exports = MongoDocumentStore;