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