Provide Constructor for MongoDB Connection Reusing
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 152
- Forks
- 68
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 3
Description
sharedb-mongo provides two ways of accessing a mongodb database:
a) takes a url, creates a client, connects to the server, accesses the database
b) takes a client, connects to the server, accesses the database
The second approach provides better flexibility and saves resource.
In fact, there is a better way:
c) takes a database
The third approach enables creating thousands of sharedb instances without hitting connection limitations since each connect() call creates another connection pool.
I'm not good at NodeJS but I changed the source code and it enabled running 4000 sharedb instances with 150 mongodb connections. I think this is a good result.
if (isLegacyMongoClient(client)) {
self.mongo = self._mongoClient = client;
}
else if(client.s) {
self.mongo = client;
self._mongoClient = client.s.client;
self._dbInstance = true;
}
else {
self.mongo = client.db();
self._mongoClient = client;
}
ShareDbMongo.prototype.close = function(callback) {
if (!callback) {
callback = function(err) {
if (err) throw err;
};
}
var self = this;
this.getDbs(function(err) {
// Ignore "already closed"
if (err && err.code === 5101) return callback();
if (err) return callback(err);
self.closed = true;
if(!self._dbInstance) {
self._mongoClient.close(function(err) {
if (err) return callback(err);
if (!self._mongoPollClient) return callback();
self._mongoPollClient.close(callback);
});
}
});
};
const db = require('sharedb-mongo')({
mongo: function (callback) {
mongoPromise.then(client => {
let mongoDb = client.db(appId);
try {
callback(null, mongoDb);
}
catch (error) {
console.log(error);
}
});
}
});
Notes:
Mongodb documents about connection pooling:
"To reduce the number of connection pools created by your application, we recommend calling MongoClient.connect once and reusing the database variable returned by the callback:"
See https://github.com/share/sharedb-mongo/issues/56#issuecomment-1233156663 for another discussion about this feature request.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the sharedb-mongo constructor and its mongo option, comparing the existing URL and client paths with the database-instance path shown in the issue. Check the proposed close behavior so externally supplied database connections are not closed, and confirm the new path supports creating many ShareDB instances without additional connection pools. Done means database instances can be supplied while existing connection inputs continue to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, mongodb
- Domain
- databases
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100