payservedb 9.9.5 → 9.9.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +16 -12
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -5,6 +5,18 @@ require("dotenv").config();
|
|
|
5
5
|
// Maintain a record of open connections for each database
|
|
6
6
|
const connections = {};
|
|
7
7
|
|
|
8
|
+
// Consuming services (e.g. backend_main) typically run one connection per
|
|
9
|
+
// cluster worker process. Without an explicit cap here, the MongoDB Node
|
|
10
|
+
// driver defaults maxPoolSize to 100 *per connection* — so N worker
|
|
11
|
+
// processes each calling connectToMongoDB() can reserve up to N*100
|
|
12
|
+
// sockets on the server even under light load. Capping it keeps a single
|
|
13
|
+
// process's footprint bounded and predictable.
|
|
14
|
+
const defaultMongoOptions = {
|
|
15
|
+
useNewUrlParser: true,
|
|
16
|
+
maxPoolSize: 20,
|
|
17
|
+
minPoolSize: 2,
|
|
18
|
+
};
|
|
19
|
+
|
|
8
20
|
// Utility function to connect to MongoDB
|
|
9
21
|
async function connectToMongoDB(
|
|
10
22
|
dbName,
|
|
@@ -18,17 +30,13 @@ async function connectToMongoDB(
|
|
|
18
30
|
if (secured === false) {
|
|
19
31
|
const url_ = url + ":" + port;
|
|
20
32
|
const connectionString = `mongodb://${url_}/${dbName}`;
|
|
21
|
-
await mongoose.connect(connectionString,
|
|
22
|
-
useNewUrlParser: true,
|
|
23
|
-
});
|
|
33
|
+
await mongoose.connect(connectionString, defaultMongoOptions);
|
|
24
34
|
console.log("Connected to MongoDB");
|
|
25
35
|
} else {
|
|
26
36
|
const url_ = `${username}:${password}@${url}:${port}`;
|
|
27
37
|
const source = "?authSource=admin";
|
|
28
38
|
const connectionString = `mongodb://${url_}/${dbName}${source}`;
|
|
29
|
-
await mongoose.connect(connectionString,
|
|
30
|
-
useNewUrlParser: true,
|
|
31
|
-
});
|
|
39
|
+
await mongoose.connect(connectionString, defaultMongoOptions);
|
|
32
40
|
console.log("Connected to MongoDB");
|
|
33
41
|
}
|
|
34
42
|
} catch (err) {
|
|
@@ -53,9 +61,7 @@ async function switchDB(dbName) {
|
|
|
53
61
|
const source = "?authSource=admin";
|
|
54
62
|
|
|
55
63
|
const connectionString = `mongodb://${username}:${password}@${url}:${port}/${dbName}${source}`;
|
|
56
|
-
const dbConnection = await mongoose.createConnection(connectionString,
|
|
57
|
-
useNewUrlParser: true,
|
|
58
|
-
});
|
|
64
|
+
const dbConnection = await mongoose.createConnection(connectionString, defaultMongoOptions);
|
|
59
65
|
|
|
60
66
|
connections[dbName] = dbConnection; // Store the connection for reuse
|
|
61
67
|
console.log(`Switched to database: ${dbName}`);
|
|
@@ -387,9 +393,7 @@ async function connectToMoveInDB(dbName, secured, username, password, url, port)
|
|
|
387
393
|
connectionString = `mongodb://${username}:${password}@${url}:${port}/${dbName}${source}`;
|
|
388
394
|
}
|
|
389
395
|
|
|
390
|
-
const conn = await mongoose.createConnection(connectionString,
|
|
391
|
-
useNewUrlParser: true,
|
|
392
|
-
});
|
|
396
|
+
const conn = await mongoose.createConnection(connectionString, defaultMongoOptions);
|
|
393
397
|
|
|
394
398
|
console.log(`Connected to Move-In DB: ${dbName}`);
|
|
395
399
|
|