connect-mongostore
connect-mongostore copied to clipboard
Issue with multiple concurrent reads
When an express instance using connect-mongostore is accessed for the first time, and there are multiple concurrent requests to that server, connect-mongostore will fail with an error. If I wait a while after the initial error, the server seems to run fine. Also, no problems will occur if the first request is allowed to be processed fully before new requests come in.
I suspect that while connect-mongostore is creating the MongoDB connection, the state of the mongostore code is such that requests coming in while the connection is being made will fail.
The error is: Error: Error setting TTL index on collection : sessions2 <Error: Error setting TTL index on collection : sessions2 <Error: No replica set primary available for query with ReadPreference PRIMARY>> at /home/v2/git/spatineo-monitor/node_modules/connect-mongostore/lib/connect-mongostore.js:115:24 at /home/v2/git/spatineo-monitor/node_modules/connect-mongostore/node_modules/mongodb/lib/mongodb/db.js:1436:28 at /home/v2/git/spatineo-monitor/node_modules/connect-mongostore/node_modules/mongodb/lib/mongodb/db.js:1558:28 at /home/v2/git/spatineo-monitor/node_modules/connect-mongostore/node_modules/mongodb/lib/mongodb/cursor.js:160:22 at Cursor.nextObject (/home/v2/git/spatineo-monitor/node_modules/connect-mongostore/node_modules/mongodb/lib/mongodb/cursor.js:733:16) at Cursor.toArray (/home/v2/git/spatineo-monitor/node_modules/connect-mongostore/node_modules/mongodb/lib/mongodb/cursor.js:159:10) at Cursor.toArray (/home/v2/git/spatineo-monitor/node_modules/connect-mongostore/node_modules/mongodb/lib/mongodb/scope.js:10:20) at Db.indexInformation (/home/v2/git/spatineo-monitor/node_modules/connect-mongostore/node_modules/mongodb/lib/mongodb/db.js:1557:63) at Db.ensureIndex (/home/v2/git/spatineo-monitor/node_modules/connect-mongostore/node_modules/mongodb/lib/mongodb/db.js:1435:8) at Collection.ensureIndex (/home/v2/git/spatineo-monitor/node_modules/connect-mongostore/node_modules/mongodb/lib/mongodb/collection/index.js:65:11) at /home/v2/git/spatineo-monitor/node_modules/connect-mongostore/lib/connect-mongostore.js:114:23 at Db.collection (/home/v2/git/spatineo-monitor/node_modules/connect-mongostore/node_modules/mongodb/lib/mongodb/db.js:502:44) at MongoStore.getCollection (/home/v2/git/spatineo-monitor/node_modules/connect-mongostore/lib/connect-mongostore.js:109:13) at MongoStore.get (/home/v2/git/spatineo-monitor/node_modules/connect-mongostore/lib/connect-mongostore.js:133:10) at Object.session as handle ...
I'm using a three member replica set and express (3.x).
Was there ever a solution to this? I seem to have exactly the same setup and the same issue.
I don't think there was ever a solution for the module itself. In my case, I made a small middleware to make sure there is no concurrency in the session middleware for the first request.
The middleware is divided into two parts surrounding express.session() in the middleware chain. There are three stages of work: "no requests", "queue" and "normal operations". Both parts share the stage and queue variables.
The first part works as follows:
- When in "no requests", it will switch to the "queue" stage and call next().
- In the "queue" stage, the next functions passed to the this middleware is pushed into the queue but not executed.
- In "normal operations", next() is called
The second part works as follows:
- If there are queued functions, call each one emptying the queue
- Set stage to "normal operations"
- Call next()
Makes sense, thanks.