1
0
Fork 0
mirror of https://github.com/seejohnrun/haste-server.git synced 2024-11-01 03:21: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,
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 } }
]
}, {
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
}
}, {
upsert: true
}, function (err, existing) {
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 });
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) {
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 });
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 && that.expire && !skipExpire) {
db.collection('entries').updateOne({
'entry_id': key
}, {
if (
entry !== null &&
entry.expiration !== -1 &&
this.expire &&
!skipExpire
) {
db.collection("entries").updateOne(
{
entry_id: key,
},
{
$set: {
'expiration': that.expire + now
expiration: this.expire + now,
},
},
() => {
client.close();
}
}, function (err, result) { });
);
}
});
}
);
});
};
MongoDocumentStore.prototype.safeConnect = function (callback) {
MongoClient.connect(this.connectionUrl, { useUnifiedTopology: true }, function (err, client) {
MongoClient.connect(
this.connectionUrl,
{ useUnifiedTopology: true },
(err, client) => {
if (err) {
winston.error('error connecting to mongodb', { error: err });
winston.error("error connecting to mongodb", { error: err });
callback(err);
} else {
callback(undefined, client.db(this.connectionName));
callback(undefined, client.db(this.connectionName), client);
}
});
}
);
};
module.exports = MongoDocumentStore;