Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak connection handling for better failure detection #191

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bekarau
Copy link

@bekarau bekarau commented Mar 31, 2016

Tweak import lines.

Make connection more serial, giving small default timeout; allows for better failure checking. The way it currently stands, the connection is backgrounded and not made until the next action is done making the exception handler around mongo_connect redundant.

This patch forces a command to happen immediately to ensure that there is a connection by the time mongo_connect() returns.

The default timeouts were also large, gave the limited ability to override timeouts on the check command line.

@warrenpnz
Copy link
Contributor

Nice work.
A couple of things to maybe look at, at line 291, the user/password auth might need a try/except to catch an incorrect password like:

        if user and passwd:
            db = con[authdb]
            try:
              #if not db.authenticate(user, password=passwd):
              db.authenticate(user, password=passwd)
            except PyMongoError:
                sys.exit("Username/Password incorrect")

Also the pymongo docs now recommend not doing background connect rather using "connect=false" then doing a low usage command to initiate the connection (https://api.mongodb.org/python/current/migrate-to-pymongo3.html#mongoclient-connects-asynchronously) e.g:

        if pymongo.version >= "2.3":
            if replica is None:
                con = pymongo.MongoClient(host, port, ssl=useSSL, connectTimeoutMS=5000, connect=False)
            else:
                con = pymongo.MongoClient(host, port, ssl=useSSL, connectTimeoutMS=5000, connect=False, read_preference=pymongo.ReadPreference.SECONDARY, replicaSet=replica )
        else:
            if replica is None:
                con = pymongo.Connection(host, port, slave_okay=True, network_timeout=10)
            else:
                con = pymongo.Connection(host, port, slave_okay=True, network_timeout=10)
                #con = pymongo.Connection(host, port, slave_okay=True, replicaSet=replica, network_timeout=10)

        try:
          result = con.admin.command("ismaster")
        except ConnectionFailure:
          print("Server not available")
          sys.exit(1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants