mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-21 21:50:49 +02:00
🍃 feat: Add MongoDB Connection Pool Configuration Options (#8537)
* 🔧 Feat: Add MongoDB connection pool configuration options to environment variables * 🔧 feat: Add environment variables for automatic index creation and collection creation in MongoDB connection --------- Co-authored-by: Atef Bellaaj <slalom.bellaaj@external.daimlertruck.com>
This commit is contained in:
parent
52e59e40be
commit
aa42759ffd
2 changed files with 46 additions and 1 deletions
14
.env.example
14
.env.example
|
@ -15,6 +15,20 @@ HOST=localhost
|
||||||
PORT=3080
|
PORT=3080
|
||||||
|
|
||||||
MONGO_URI=mongodb://127.0.0.1:27017/LibreChat
|
MONGO_URI=mongodb://127.0.0.1:27017/LibreChat
|
||||||
|
#The maximum number of connections in the connection pool. */
|
||||||
|
MONGO_MAX_POOL_SIZE=
|
||||||
|
#The minimum number of connections in the connection pool. */
|
||||||
|
MONGO_MIN_POOL_SIZE=
|
||||||
|
#The maximum number of connections that may be in the process of being established concurrently by the connection pool. */
|
||||||
|
MONGO_MAX_CONNECTING=
|
||||||
|
#The maximum number of milliseconds that a connection can remain idle in the pool before being removed and closed. */
|
||||||
|
MONGO_MAX_IDLE_TIME_MS=
|
||||||
|
#The maximum time in milliseconds that a thread can wait for a connection to become available. */
|
||||||
|
MONGO_WAIT_QUEUE_TIMEOUT_MS=
|
||||||
|
# Set to false to disable automatic index creation for all models associated with this connection. */
|
||||||
|
MONGO_AUTO_INDEX=
|
||||||
|
# Set to `false` to disable Mongoose automatically calling `createCollection()` on every model created on this connection. */
|
||||||
|
MONGO_AUTO_CREATE=
|
||||||
|
|
||||||
DOMAIN_CLIENT=http://localhost:3080
|
DOMAIN_CLIENT=http://localhost:3080
|
||||||
DOMAIN_SERVER=http://localhost:3080
|
DOMAIN_SERVER=http://localhost:3080
|
||||||
|
|
|
@ -1,11 +1,34 @@
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
|
const { isEnabled } = require('@librechat/api');
|
||||||
|
const { logger } = require('@librechat/data-schemas');
|
||||||
|
|
||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
const MONGO_URI = process.env.MONGO_URI;
|
const MONGO_URI = process.env.MONGO_URI;
|
||||||
|
|
||||||
if (!MONGO_URI) {
|
if (!MONGO_URI) {
|
||||||
throw new Error('Please define the MONGO_URI environment variable');
|
throw new Error('Please define the MONGO_URI environment variable');
|
||||||
}
|
}
|
||||||
|
/** The maximum number of connections in the connection pool. */
|
||||||
|
const maxPoolSize = parseInt(process.env.MONGO_MAX_POOL_SIZE) || undefined;
|
||||||
|
/** The minimum number of connections in the connection pool. */
|
||||||
|
const minPoolSize = parseInt(process.env.MONGO_MIN_POOL_SIZE) || undefined;
|
||||||
|
/** The maximum number of connections that may be in the process of being established concurrently by the connection pool. */
|
||||||
|
const maxConnecting = parseInt(process.env.MONGO_MAX_CONNECTING) || undefined;
|
||||||
|
/** The maximum number of milliseconds that a connection can remain idle in the pool before being removed and closed. */
|
||||||
|
const maxIdleTimeMS = parseInt(process.env.MONGO_MAX_IDLE_TIME_MS) || undefined;
|
||||||
|
/** The maximum time in milliseconds that a thread can wait for a connection to become available. */
|
||||||
|
const waitQueueTimeoutMS = parseInt(process.env.MONGO_WAIT_QUEUE_TIMEOUT_MS) || undefined;
|
||||||
|
/** Set to false to disable automatic index creation for all models associated with this connection. */
|
||||||
|
const autoIndex =
|
||||||
|
process.env.MONGO_AUTO_INDEX != undefined
|
||||||
|
? isEnabled(process.env.MONGO_AUTO_INDEX) || false
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
/** Set to `false` to disable Mongoose automatically calling `createCollection()` on every model created on this connection. */
|
||||||
|
const autoCreate =
|
||||||
|
process.env.MONGO_AUTO_CREATE != undefined
|
||||||
|
? isEnabled(process.env.MONGO_AUTO_CREATE) || false
|
||||||
|
: undefined;
|
||||||
/**
|
/**
|
||||||
* Global is used here to maintain a cached connection across hot reloads
|
* Global is used here to maintain a cached connection across hot reloads
|
||||||
* in development. This prevents connections growing exponentially
|
* in development. This prevents connections growing exponentially
|
||||||
|
@ -26,13 +49,21 @@ async function connectDb() {
|
||||||
if (!cached.promise || disconnected) {
|
if (!cached.promise || disconnected) {
|
||||||
const opts = {
|
const opts = {
|
||||||
bufferCommands: false,
|
bufferCommands: false,
|
||||||
|
...(maxPoolSize ? { maxPoolSize } : {}),
|
||||||
|
...(minPoolSize ? { minPoolSize } : {}),
|
||||||
|
...(maxConnecting ? { maxConnecting } : {}),
|
||||||
|
...(maxIdleTimeMS ? { maxIdleTimeMS } : {}),
|
||||||
|
...(waitQueueTimeoutMS ? { waitQueueTimeoutMS } : {}),
|
||||||
|
...(autoIndex != undefined ? { autoIndex } : {}),
|
||||||
|
...(autoCreate != undefined ? { autoCreate } : {}),
|
||||||
// useNewUrlParser: true,
|
// useNewUrlParser: true,
|
||||||
// useUnifiedTopology: true,
|
// useUnifiedTopology: true,
|
||||||
// bufferMaxEntries: 0,
|
// bufferMaxEntries: 0,
|
||||||
// useFindAndModify: true,
|
// useFindAndModify: true,
|
||||||
// useCreateIndex: true
|
// useCreateIndex: true
|
||||||
};
|
};
|
||||||
|
logger.info('Mongo Connection options');
|
||||||
|
logger.info(JSON.stringify(opts, null, 2));
|
||||||
mongoose.set('strictQuery', true);
|
mongoose.set('strictQuery', true);
|
||||||
cached.promise = mongoose.connect(MONGO_URI, opts).then((mongoose) => {
|
cached.promise = mongoose.connect(MONGO_URI, opts).then((mongoose) => {
|
||||||
return mongoose;
|
return mongoose;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue