diversario / connect-mongostore

MongoDB session store for Connect/Express.
57 stars 16 forks source link

Issue with multiple concurrent reads #21

Open sampov2 opened 10 years ago

sampov2 commented 10 years ago

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 > 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).

petefredricks commented 9 years ago

Was there ever a solution to this? I seem to have exactly the same setup and the same issue.

sampov2 commented 9 years ago

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:

The second part works as follows:

petefredricks commented 9 years ago

Makes sense, thanks.