share / share/sharedb-mongo

Provide Constructor for MongoDB Connection Reusing

Open
#132 0 comments 0 reactions 0 assignees View on GitHub

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:

https://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html#mongoclient-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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.