Mongo-go-driver not returning errors

41 views
Skip to first unread message

Dinushka Fernando

unread,
Sep 25, 2018, 9:41:59 PM9/25/18
to mongodb-user
I have the following code and it works fine. It can insert data to local mongodb. checked with terminal. But when I stop the mongod service and run the code, it doesn't return any errors. but it is getting a timeout error when inserting data.

Kevin Adistambha

unread,
Sep 27, 2018, 2:25:41 AM9/27/18
to mongodb-user

Hi Dinushka

I believe the error is not from insertion, but rather from trying to connect. This is an expected behaviour, and is also exhibited by the Python driver:

$ cat test.py
import pymongo
conn = pymongo.MongoClient()
print list(conn.get_database('test').get_collection('test').find())

$ time python test.py
Traceback (most recent call last):
....
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 61] Connection refused
       31.08 real         0.19 user         0.14 sys

The server selection timeout means that the driver tried to connect to the server, but after 30 seconds (default value) it gave up. This is because the driver cannot know if the server is down, or it’s just slow in responding. The full reasoning behind this is written in serverSelectionTimeoutMS spec.

If you rather have a quicker timeout, you can set the server selection timeout to a lower value, e.g.:

opt := clientopt.ServerSelectionTimeout(5 * time.Second)

Note that changing this value would also mean that your app is less resilient to transient network issues, e.g. it can give up on connecting to a server when in fact the server is available, it’s just slow in responding due to network/load issues.

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages