I have a client connection to mongo like so -

 self.mongoClient = pymongo.MongoClient(environ.get("MONGO_URL"),
                                                   username=environ.get(
                                                   "MONGO_USERNAME", default=""),
                                                   password=environ.get(
                "MONGO_PASSWORD", default=""),
                authSource=environ.get(
                "MONGO_AUTH_DB", default=""),
                replicaSet=environ.get("MONGO_REPLICATION", default="rs-0"))

My MONGO_URL in this case is --

MONGO_URL=127.0.0.1:27011,127.0.0.1:27012,127.0.0.1:27013,10.10.0.1:27014,127.0.0.1:27015

The assumption made here is that one or more of the services might be down at any given time (this isn't ideal for production, but we're testing something out) Eg, when we try to connect, assume that 127.0.0.1:27012 and 10.10.0.1:27014 is down. As in, there is no mongo server listening on those ports.

I hit upon the error below:

pymongo.errors.ServerSelectionTimeoutError: 127.0.0.1:27012: [Errno 111] Connection refused,10.10.0.1:27014: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 65390c7f67d64862d4e035f5, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('127.0.0.1', 27012) server_type: Unknown, rtt: None, error=AutoReconnect('127.0.0.1:27012: [Errno 111] Connection refused')>, <ServerDescription ('10.10.0.1', 27014) server_type: Unknown, rtt: None, error=AutoReconnect('10.10.0.1:27014: [Errno 111] Connection refused')>]>

Considering that the primary at this point (127.0.0.1:27011) and the rest are alive and working, how do I nudge the client to ignore the ones that give connection refuced and move ahead?

0

There are 0 best solutions below