Skip to content

Commit

Permalink
Provide production-ready memory store for eypress-session (#5212)
Browse files Browse the repository at this point in the history
The `express-session` library comes with a session storage meant for
testing by default. That is why you get a message like this when you
start up LibreChat with OIDC enabled:

    Warning: connect.session() MemoryStore is not
    designed for a production environment, as it will leak
    memory, and will not scale past a single process.

LibreChat can already use Redis as a session storage, although Redis support
is still marked as experimental. It also makes the set-up more complex, since
you will need to configure and run yet another service.

This pull request provides a simple alternative by using a in-memory session
store marked as a production-ready alternative by the guys from
`express-session`¹. You can still configure Redis, but this provides a simple,
good default for everyone else.

See also #1014

¹⁾ https://github.com/expressjs/session?tab=readme-ov-file#compatible-session-stores
  • Loading branch information
lkiesow authored Jan 9, 2025
1 parent 69a9b8b commit dd92758
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"librechat-mcp": "*",
"lodash": "^4.17.21",
"meilisearch": "^0.38.0",
"memorystore": "^1.6.7",
"mime": "^3.0.0",
"module-alias": "^2.2.3",
"mongoose": "^8.8.3",
Expand Down
5 changes: 5 additions & 0 deletions api/server/socialLogins.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Redis = require('ioredis');
const passport = require('passport');
const session = require('express-session');
const MemoryStore = require('memorystore')(session)
const RedisStore = require('connect-redis').default;
const {
setupOpenId,
Expand Down Expand Up @@ -48,6 +49,10 @@ const configureSocialLogins = (app) => {
.on('ready', () => logger.info('ioredis successfully initialized.'))
.on('reconnecting', () => logger.info('ioredis reconnecting...'));
sessionOptions.store = new RedisStore({ client, prefix: 'librechat' });
} else {
sessionOptions.store = new MemoryStore({
checkPeriod: 86400000 // prune expired entries every 24h
})
}
app.use(session(sessionOptions));
app.use(passport.session());
Expand Down
36 changes: 36 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dd92758

Please sign in to comment.