Hi Guys! I hope I’m posting this in correct place…
I’m working with MongoDB using NodeJS mongodb driver. But I can’t make few options to work… My setup:
OS: Windows 10 x64
NodeJS mongodb driver: 3.5.5
MongoDB: 4.2.5
I want to limit:
- Connection time into DB (connectTimeoutMS option)
- And query execution time (socketTimeoutMS option)
Description of connectTimeoutMS from mongodb.github.io: How long to wait for a connection to be established before timing out
Description from jira.mongodb.org: dictates how long to wait for an initial connection before considering it a timeout. This is used exclusively when we create a TCP/TLS socket
So I tried to connect to disabled DB with the following code:
const { MongoClient } = require('mongodb')
const url = 'mongodb://localhost'
const config = {
connectTimeoutMS: 5000,
useUnifiedTopology: true
}
async function mongodb() {
console.time('connect')
try { await MongoClient.connect(url, config) }
catch (error) { console.error(error) }
finally { console.timeEnd('connect') }
}
mongodb()
Which produces the following logs:
MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
connect: 30034.340ms
If I will change useUnifiedTopology to false I will get this log:
(node:19040) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
MongoNetworkError: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
connect: 2025.937ms
The issue that I have connectTimeoutMS set to 5 000 ms (5 secs) but as you can see it gets timed out in 2 or 30 seconds not in 5 seconds.
Next issue is with query execution timeout.
Description of socketTimeoutMS from mongodb.github.io: How long a send or receive on a socket can take before timing out
Description from jira.mongodb.org: dictates how long to wait for any operation on an existing connection before timing out. Once the TCP socket has been connected, we use this for our actual operations
So I tried to make a query into DB with 5 seconds timeout limit:
const { MongoClient } = require('mongodb')
const fetchLimit = 500000
const url = 'mongodb://localhost'
const config = {
connectTimeoutMS: 5000,
socketTimeoutMS: 5000,
useUnifiedTopology: true
}
async function mongodb() {
let client
console.time('connect')
try { client = await MongoClient.connect(url, config) }
catch (error) { console.error(error) }
finally { console.timeEnd('connect') }
const coll = client.db('reps').collection('req')
console.time('find')
try { await coll.find({ p: 'XYZ' }).limit(fetchLimit).toArray() }
catch (error) { console.error(error) }
finally { console.timeEnd('find') }
}
mongodb()
And I got this:
connect: 28.459ms
find: 8731.263ms
I though that after 5 secs I will get timeout error… What’s wrong ?
Even if I will set socketTimeoutMS to 30 ms I will get the same result…
But if I will set it to 20 ms I will get this:
connect: 70.304ms
MongoNetworkError: connection 2 to 127.0.0.1:27017 timed out
find: 1225.078ms
If I replace find
with count
(collection has more than 10 millions docs) and use await coll.countDocuments()
I will get Timeout Error after 60 secs (not after 5)
Can anybody assist me, I dont understand what’s going on…