Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide Constructor for MongoDB Connection Reusing #132

Open
ihsanciftci opened this issue Sep 1, 2022 · 0 comments
Open

Provide Constructor for MongoDB Connection Reusing #132

ihsanciftci opened this issue Sep 1, 2022 · 0 comments

Comments

@ihsanciftci
Copy link

ihsanciftci commented Sep 1, 2022

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 #56 (comment) for another discussion about this feature request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant