Python pymongo.errors.ServerSelectionTimeoutError() Examples

The following are code examples for showing how to use pymongo.errors.ServerSelectionTimeoutError(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: smsweb   Author: awangga   File: topology.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def select_server_by_address(self, address,
                                 server_selection_timeout=None):
        """Return a Server for "address", reconnecting if necessary.

        If the server's type is not known, request an immediate check of all
        servers. Time out after "server_selection_timeout" if the server
        cannot be reached.

        :Parameters:
          - `address`: A (host, port) pair.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value
            common.SERVER_SELECTION_TIMEOUT is used.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        return self.select_server(any_server_selector,
                                  server_selection_timeout,
                                  address) 
Example 2
Project: vnpy_crypto   Author: birforce   File: topology.py    MIT License 6 votes vote down vote up
def select_server_by_address(self, address,
                                 server_selection_timeout=None):
        """Return a Server for "address", reconnecting if necessary.

        If the server's type is not known, request an immediate check of all
        servers. Time out after "server_selection_timeout" if the server
        cannot be reached.

        :Parameters:
          - `address`: A (host, port) pair.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value
            common.SERVER_SELECTION_TIMEOUT is used.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        return self.select_server(any_server_selector,
                                  server_selection_timeout,
                                  address) 
Example 3
Project: HyperStream   Author: IRC-SPHERE   File: client.py    MIT License 6 votes vote down vote up
def __init__(self, server_config, auto_connect=True):
        """Initialise the client

        :param server_config: The server configuration
        :param auto_connect: Whether to automatically connect
        """
        self.server_config = server_config

        if auto_connect:
            if ServerSelectionTimeoutError:
                try:
                    self.connect(server_config)
                except ServerSelectionTimeoutError as e:
                    logging.warn(e.message)
                    sys.exit()
            else:
                self.connect(server_config) 
Example 4
Project: satori   Author: leancloud   File: topology.py    Apache License 2.0 6 votes vote down vote up
def select_server_by_address(self, address,
                                 server_selection_timeout=None):
        """Return a Server for "address", reconnecting if necessary.

        If the server's type is not known, request an immediate check of all
        servers. Time out after "server_selection_timeout" if the server
        cannot be reached.

        :Parameters:
          - `address`: A (host, port) pair.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value
            common.SERVER_SELECTION_TIMEOUT is used.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        return self.select_server(any_server_selector,
                                  server_selection_timeout,
                                  address) 
Example 5
Project: icd10   Author: silentday   File: icd_load.py    MIT License 6 votes vote down vote up
def loadMongo(chapter_docs):
    try:
        client = MongoClient('mongodb://localhost:27017/',
                             serverSelectionTimeoutMS=100)
        db = client['icd10']
        col = db['icd2016Codes']
        for chapter in chapter_docs:
            col.insert_one(chapter)
        return col.count()
    except ConnectionFailure as connectError:
        print("Connection Failure to MongoDB: {0}".format(connectError))
        sys.exit(1)
    except ServerSelectionTimeoutError as timeoutErr:
        print("Unable to connect Timeout: {0}".format(timeoutErr))
        sys.exit(1)
    except Exception as err:
        print ("Error: {0}".format(err.args)) 
Example 6
Project: setup   Author: mindbender-studio   File: topology.py    MIT License 6 votes vote down vote up
def select_server_by_address(self, address,
                                 server_selection_timeout=None):
        """Return a Server for "address", reconnecting if necessary.

        If the server's type is not known, request an immediate check of all
        servers. Time out after "server_selection_timeout" if the server
        cannot be reached.

        :Parameters:
          - `address`: A (host, port) pair.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value
            common.SERVER_SELECTION_TIMEOUT is used.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        return self.select_server(any_server_selector,
                                  server_selection_timeout,
                                  address) 
Example 7
Project: setup   Author: mindbender-studio   File: topology.py    MIT License 6 votes vote down vote up
def select_server_by_address(self, address,
                                 server_selection_timeout=None):
        """Return a Server for "address", reconnecting if necessary.

        If the server's type is not known, request an immediate check of all
        servers. Time out after "server_selection_timeout" if the server
        cannot be reached.

        :Parameters:
          - `address`: A (host, port) pair.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value
            common.SERVER_SELECTION_TIMEOUT is used.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        return self.select_server(any_server_selector,
                                  server_selection_timeout,
                                  address) 
Example 8
Project: Peerion   Author: hyperionxtech   File: test_mongo_adapter.py    Apache License 2.0 6 votes vote down vote up
def setUpClass(cls):
        """
        Instantiate the adapter before any tests in the test case run.
        """
        from pymongo.errors import ServerSelectionTimeoutError
        from pymongo import MongoClient

        cls.has_mongo_connection = False

        try:
            client = MongoClient(
                serverSelectionTimeoutMS=0.1
            )
            client.server_info()

            cls.adapter = MongoDatabaseAdapter(
                database_uri='mongodb://localhost:27017/chatterbot_test_database'
            )

            cls.has_mongo_connection = False

        except ServerSelectionTimeoutError:
            pass 
Example 9
Project: connecting_flights   Author: NuttySalmon   File: Database.py    MIT License 6 votes vote down vote up
def __init__(self, ip, port, db_name):
        """Constructor"""

        self._client = MongoClient(ip, port)  # get client
        self._name = db_name
        self._db = self._client[db_name]

        # check if database is connected
        try:
            self._client.server_info()
            print("Connected to {}:{} - {}".format(ip, port, db_name))
        except errors.ServerSelectionTimeoutError:
            print("Failed to connect to database")
            exit()

        self._flights = self._db.flights
        self._airports = self._db.airports
        self._adj = self._db.adj 
Example 10
Project: TitanRover2019   Author: CSUFTitanRover   File: topology.py    GNU General Public License v3.0 6 votes vote down vote up
def select_server_by_address(self, address,
                                 server_selection_timeout=None):
        """Return a Server for "address", reconnecting if necessary.

        If the server's type is not known, request an immediate check of all
        servers. Time out after "server_selection_timeout" if the server
        cannot be reached.

        :Parameters:
          - `address`: A (host, port) pair.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value
            common.SERVER_SELECTION_TIMEOUT is used.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        return self.select_server(any_server_selector,
                                  server_selection_timeout,
                                  address) 
Example 11
Project: learn_python3_spider   Author: wistbean   File: topology.py    MIT License 6 votes vote down vote up
def select_server_by_address(self, address,
                                 server_selection_timeout=None):
        """Return a Server for "address", reconnecting if necessary.

        If the server's type is not known, request an immediate check of all
        servers. Time out after "server_selection_timeout" if the server
        cannot be reached.

        :Parameters:
          - `address`: A (host, port) pair.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value
            common.SERVER_SELECTION_TIMEOUT is used.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        return self.select_server(any_server_selector,
                                  server_selection_timeout,
                                  address) 
Example 12
Project: Astra   Author: flipkart-incubator   File: dbconnection.py    Apache License 2.0 6 votes vote down vote up
def db_connect():
    maxSevSelDelay = 1
    try:
        mongo_host = 'localhost'
        mongo_port = 27017

        if 'MONGO_PORT_27017_TCP_ADDR' in os.environ :
            mongo_host = os.environ['MONGO_PORT_27017_TCP_ADDR']

        if 'MONGO_PORT_27017_TCP_PORT' in os.environ:
            mongo_port = int(os.environ['MONGO_PORT_27017_TCP_PORT'])

        client = MongoClient(mongo_host, mongo_port, serverSelectionTimeoutMS=maxSevSelDelay)
        client.server_info()
        return client

    except ServerSelectionTimeoutError as err:
        exit("Failed to connect to MongoDB.") 
Example 13
Project: DSReadingProject   Author: Bintong   File: topology.py    MIT License 6 votes vote down vote up
def select_server_by_address(self, address,
                                 server_selection_timeout=None):
        """Return a Server for "address", reconnecting if necessary.

        If the server's type is not known, request an immediate check of all
        servers. Time out after "server_selection_timeout" if the server
        cannot be reached.

        :Parameters:
          - `address`: A (host, port) pair.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value
            common.SERVER_SELECTION_TIMEOUT is used.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        return self.select_server(any_server_selector,
                                  server_selection_timeout,
                                  address) 
Example 14
Project: GlosBio   Author: MikolajBalcerek   File: SampleManager.py    MIT License 6 votes vote down vote up
def __init__(self, db_url: str, db_name: str, show_logs: bool = True):
        """
        :param db_url: str - url to MongoDB database, it can contain port eg: 'localhost:27017'
        :param db_name: str - database name
        :param show_logs: bool - used to suppress log messages
        """
        # setup MongoDB database connection
        self.db_url = db_url
        self.db_client = MongoClient(db_url, serverSelectionTimeoutMS=5000)
        self.db_database = self.db_client[db_name]
        self.db_collection = self.db_database.samples
        self.db_tags = self.db_database.tags
        self.db_file_storage = gridfs.GridFS(self.db_database)
        try:
            if show_logs:
                print(f" * #INFO: testing db connection: '{db_url}'...")
            self.db_client.server_info()
        except errors.ServerSelectionTimeoutError as e:
            raise DatabaseException(
                f"Could not connect to MongoDB at '{db_url}'")

        self.show_logs = show_logs 
Example 15
Project: rufeng-finance   Author: changbindu   File: dm.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def load_from_db(self, remove_invalid=True):
        """load stocks from local database only"""
        logging.info('try to load stock data from local database')
        count = 0
        try:
            for stock in self.local_dm.find_stock(show_process=True):
                self.stocks[stock.code] = stock
                count += 1
            logging.info('loaded %d stocks' % count)

            count = 0
            for index in self.local_dm.find_index():
                self.indexes[index.code] = index
                count += 1
            logging.info('loaded %d indexes' % count)
        except mongo_errors.ServerSelectionTimeoutError as e:
            logging.error("Cannot load from database, please make sure your mongodb server is running.")
            logging.error("Pymongo: %s" % str(e))
            return
        except KeyError as e:
            logging.warning('%s, please try to drop database' % str(e))
            return
        if remove_invalid:
            self._remove_unavailable_stocks() 
Example 16
Project: opsbro   Author: naparuba   File: topology.py    MIT License 6 votes vote down vote up
def select_server_by_address(self, address,
                                 server_selection_timeout=None):
        """Return a Server for "address", reconnecting if necessary.

        If the server's type is not known, request an immediate check of all
        servers. Time out after "server_selection_timeout" if the server
        cannot be reached.

        :Parameters:
          - `address`: A (host, port) pair.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value
            common.SERVER_SELECTION_TIMEOUT is used.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        return self.select_server(any_server_selector,
                                  server_selection_timeout,
                                  address) 
Example 17
Project: personal-tracker   Author: NoFishLikeIan   File: db.py    MIT License 6 votes vote down vote up
def connect_to_db():
    try:
        logger.info(f'Connecting to db...')
        mongo_client_location = 'db' if is_production else 'localhost'

        client = MongoClient(mongo_client_location, 27017,
                             serverSelectionTimeoutMS=max_timeout)
        client.server_info()

        logger.info(
            f'...success! Now connected to {db_name}:{collection_name}')

        db = client[db_name]
        collection = db[collection_name]

        return collection, db

    except errors.ServerSelectionTimeoutError as err:
        logger.error(
            f'... connection timed out with the db, is it running? {err}')

        return None, None 
Example 18
Project: docker   Author: getavalon   File: topology.py    MIT License 6 votes vote down vote up
def select_server_by_address(self, address,
                                 server_selection_timeout=None):
        """Return a Server for "address", reconnecting if necessary.

        If the server's type is not known, request an immediate check of all
        servers. Time out after "server_selection_timeout" if the server
        cannot be reached.

        :Parameters:
          - `address`: A (host, port) pair.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value
            common.SERVER_SELECTION_TIMEOUT is used.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        return self.select_server(any_server_selector,
                                  server_selection_timeout,
                                  address) 
Example 19
Project: mongo-rs-controller-swarm   Author: smartsdk   File: replica_ctrl.py    Apache License 2.0 6 votes vote down vote up
def gather_configured_members_ips(mongo_tasks_ips, mongo_port):
    current_ips = set()
    logger = logging.getLogger(__name__)
    for t in mongo_tasks_ips:
        mc = pm.MongoClient(t, mongo_port)
        try:
            config = mc.admin.command("replSetGetConfig")['config']
            for m in config['members']:
                current_ips.add(m['host'].split(":")[0])
            # Let's accept the first configuration found. Read as room for improvement!
            break
        except ServerSelectionTimeoutError as ssete:
            logger.debug("cannot connect to {} to get configuration, failed ({})".format(t,ssete))
        except OperationFailure as of:
            logger.debug("no configuration found in node {} ({})".format(t,of))
        finally:
            mc.close()
    logger.debug("Current members in mongo configurations: {}".format(current_ips))
    return current_ips 
Example 20
Project: mongo-rs-controller-swarm   Author: smartsdk   File: replica_ctrl.py    Apache License 2.0 6 votes vote down vote up
def get_primary_ip(tasks_ips, mongo_port):
    logger = logging.getLogger(__name__)

    primary_ips = []
    for t in tasks_ips:
        mc = pm.MongoClient(t, mongo_port)
        try:
            if mc.is_primary:
                primary_ips.append(t)
        except ServerSelectionTimeoutError as ssete:
            logger.debug("cannot connect to {} check if primary, failed ({})".format(t,ssete))
        except OperationFailure as of:
            logger.debug("no configuration found in node {} ({})".format(t,of))
        finally:
            mc.close()

    if len(primary_ips) > 1:
        logger.warning("Multiple primaries were found ({}). Let's use the first.".format(primary_ips))

    if primary_ips:
        logger.info("Primary is: {}".format(primary_ips[0]))
        return primary_ips[0] 
Example 21
Project: mongodb-monitoring   Author: jruaux   File: topology.py    Apache License 2.0 6 votes vote down vote up
def select_server_by_address(self, address,
                                 server_selection_timeout=None):
        """Return a Server for "address", reconnecting if necessary.

        If the server's type is not known, request an immediate check of all
        servers. Time out after "server_selection_timeout" if the server
        cannot be reached.

        :Parameters:
          - `address`: A (host, port) pair.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value
            common.SERVER_SELECTION_TIMEOUT is used.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        return self.select_server(any_server_selector,
                                  server_selection_timeout,
                                  address) 
Example 22
Project: Kabba_AI   Author: hyperionxtech   File: test_mongo_adapter.py    Apache License 2.0 6 votes vote down vote up
def setUpClass(cls):
        """
        Instantiate the adapter before any tests in the test case run.
        """
        from pymongo.errors import ServerSelectionTimeoutError
        from pymongo import MongoClient

        cls.has_mongo_connection = False

        try:
            client = MongoClient(
                serverSelectionTimeoutMS=0.1
            )
            client.server_info()

            cls.adapter = MongoDatabaseAdapter(
                database_uri='mongodb://localhost:27017/chatterbot_test_database'
            )

            cls.has_mongo_connection = False

        except ServerSelectionTimeoutError:
            pass 
Example 23
Project: arctic   Author: man-group   File: test_decorators_unit.py    GNU Lesser General Public License v2.1 5 votes vote down vote up
def test_ServerSelectionTimeoutError_no_retry():
    error = ServerSelectionTimeoutError('some error')
    with patch('arctic.decorators._log_exception', autospec=True) as le:
        @mongo_retry
        def foo():
            raise error
        with pytest.raises(ServerSelectionTimeoutError) as e:
            foo()
    assert 'some error' in str(e.value)
    assert le.call_count == 1 
Example 24
Project: vnpy_crypto   Author: birforce   File: topology.py    MIT License 5 votes vote down vote up
def select_servers(self,
                       selector,
                       server_selection_timeout=None,
                       address=None):
        """Return a list of Servers matching selector, or time out.

        :Parameters:
          - `selector`: function that takes a list of Servers and returns
            a subset of them.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value common.SERVER_SELECTION_TIMEOUT
            is used.
          - `address`: optional server address to select.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        if server_selection_timeout is None:
            server_timeout = self._settings.server_selection_timeout
        else:
            server_timeout = server_selection_timeout

        with self._lock:
            server_descriptions = self._select_servers_loop(
                selector, server_timeout, address)

            return [self.get_server_by_address(sd.address)
                    for sd in server_descriptions] 
Example 25
Project: vnpy_crypto   Author: birforce   File: topology.py    MIT License 5 votes vote down vote up
def _select_servers_loop(self, selector, timeout, address):
        """select_servers() guts. Hold the lock when calling this."""
        now = _time()
        end_time = now + timeout
        server_descriptions = self._description.apply_selector(
            selector, address)

        while not server_descriptions:
            # No suitable servers.
            if timeout == 0 or now > end_time:
                raise ServerSelectionTimeoutError(
                    self._error_message(selector))

            self._ensure_opened()
            self._request_check_all()

            # Release the lock and wait for the topology description to
            # change, or for a timeout. We won't miss any changes that
            # came after our most recent apply_selector call, since we've
            # held the lock until now.
            self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
            self._description.check_compatible()
            now = _time()
            server_descriptions = self._description.apply_selector(
                selector, address)

        self._description.check_compatible()
        return server_descriptions 
Example 26
Project: jikken   Author: outcastofmusic   File: utils.py    MIT License 5 votes vote down vote up
def check_mongo(uri=None):
    """Check if mongo db is available in the uri provided"""
    from pymongo import MongoClient
    from pymongo.errors import ServerSelectionTimeoutError
    uri = "mongodb://localhost:27019" if uri is None else uri
    client = MongoClient(uri, serverSelectionTimeoutMS=300)
    try:
        client.server_info()
        found_database = True
    except ServerSelectionTimeoutError:
        found_database = False
    return found_database 
Example 27
Project: mongodb_consistent_backup   Author: Percona-Lab   File: DB.py    Apache License 2.0 5 votes vote down vote up
def connect(self):
        try:
            logging.debug("Getting MongoDB connection to %s (replicaSet=%s, readPreference=%s, readPreferenceTags=%s, ssl=%s)" % (
                self.uri,
                self.replset,
                self.read_pref,
                self.do_rp_tags,
                self.do_ssl(),
            ))
            conn = MongoClient(**self.client_opts())
            if self.do_connect:
                conn['admin'].command({"ping": 1})
        except (ConfigurationError, ConnectionFailure, OperationFailure, ServerSelectionTimeoutError), e:
            logging.error("Unable to connect to %s! Error: %s" % (self.uri, e))
            raise DBConnectionError(e) 
Example 28
Project: pyfhirstore   Author: arkhn   File: conftest.py    Apache License 2.0 5 votes vote down vote up
def store():
    client = MongoClient(serverSelectionTimeoutMS=5)
    try:
        client.server_info()
    except ServerSelectionTimeoutError as err:
        print("MongoClient could not reach server, is it running ?")
        raise

    fhirstore = FHIRStore(client, DB_NAME)
    fhirstore.reset()
    fhirstore.bootstrap(depth=4, resource="Patient")
    return fhirstore 
Example 29
Project: s3recon   Author: clarketm   File: mongodb.py    MIT License 5 votes vote down vote up
def is_connected(self):
        try:
            self.client.server_info()
        except ServerSelectionTimeoutError:
            return False
        return True 
Example 30
Project: MINE-Database   Author: tyo-nu   File: __init__.py    MIT License 5 votes vote down vote up
def setup_package():
    print(__name__, '__init__.py : setup_package() ==========================')
    try:
        testdb = MINE("mongotest")
        with open(os.path.dirname(__file__)+'/data/testing_db.json') as infile:
            jsondb = json.load(infile)
        for doc in jsondb[0]:
            testdb.compounds.save(doc)
        for doc in jsondb[1]:
            testdb.reactions.save(doc)
    except ServerSelectionTimeoutError:
        print('No Mongo DB server detected') 
Example 31
Project: Peerion   Author: hyperionxtech   File: base_case.py    Apache License 2.0 5 votes vote down vote up
def setUpClass(cls):
        from pymongo.errors import ServerSelectionTimeoutError
        from pymongo import MongoClient

        # Skip these tests if a mongo client is not running
        try:
            client = MongoClient(
                serverSelectionTimeoutMS=0.1
            )
            client.server_info()

        except ServerSelectionTimeoutError:
            raise SkipTest('Unable to connect to Mongo DB.') 
Example 32
Project: hfos-legacy   Author: Hackerfleet   File: component.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def _read_config(self):
        """Read this component's configuration from the database"""

        try:
            self.config = self.componentmodel.find_one(
                {'name': self.uniquename})
        except ServerSelectionTimeoutError:  # pragma: no cover
            self.log("No database access! Check if mongodb is running "
                     "correctly.", lvl=critical)
        if self.config:
            self.log("Configuration read.", lvl=verbose)
        else:
            self.log("No configuration found.", lvl=warn)
            # self.log(self.config) 
Example 33
Project: yeti   Author: yeti-platform   File: yeti_to_elasticsearch.py    Apache License 2.0 5 votes vote down vote up
def create_mongo_connection(self, hostname="localhost"):
        """
            Creates a connection to YETI's MongoDB.
        :param hostname: Hostname to connect to. Default is "localhost"
        :return: None
        """

        try:
            # Try connecting to MongoDB for 10ms
            self.mongo_client = MongoClient('mongodb://{}:27017/'.format(hostname), serverSelectionTimeoutMS=10)
            self.mongo_client.server_info()
        except errors.ServerSelectionTimeoutError as mongo_conn_err:
            logger.exception(("MongoDB connection issue occurred. "
                              "Error message: " + str(mongo_conn_err)))
            sys.exit(1) 
Example 34
Project: evernote-telegram-bot   Author: djudman   File: test_mongo.py    MIT License 5 votes vote down vote up
def setUp(self):
        self.db_name = 'test_mongo_storage'
        connection_string = 'mongodb://127.0.0.1:27017/?serverSelectionTimeoutMS=100'
        try:
            self.client = Mongo(connection_string, db_name=self.db_name,
                                collection='test')
            list(self.client.get_all({}))
        except ServerSelectionTimeoutError as e:
            raise SkipTest(str(e)) 
Example 35
Project: TitanRover2019   Author: CSUFTitanRover   File: topology.py    GNU General Public License v3.0 5 votes vote down vote up
def select_servers(self,
                       selector,
                       server_selection_timeout=None,
                       address=None):
        """Return a list of Servers matching selector, or time out.

        :Parameters:
          - `selector`: function that takes a list of Servers and returns
            a subset of them.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value common.SERVER_SELECTION_TIMEOUT
            is used.
          - `address`: optional server address to select.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        if server_selection_timeout is None:
            server_timeout = self._settings.server_selection_timeout
        else:
            server_timeout = server_selection_timeout

        with self._lock:
            server_descriptions = self._select_servers_loop(
                selector, server_timeout, address)

            return [self.get_server_by_address(sd.address)
                    for sd in server_descriptions] 
Example 36
Project: TitanRover2019   Author: CSUFTitanRover   File: topology.py    GNU General Public License v3.0 5 votes vote down vote up
def _select_servers_loop(self, selector, timeout, address):
        """select_servers() guts. Hold the lock when calling this."""
        now = _time()
        end_time = now + timeout
        server_descriptions = self._description.apply_selector(
            selector, address, custom_selector=self._settings.server_selector)

        while not server_descriptions:
            # No suitable servers.
            if timeout == 0 or now > end_time:
                raise ServerSelectionTimeoutError(
                    self._error_message(selector))

            self._ensure_opened()
            self._request_check_all()

            # Release the lock and wait for the topology description to
            # change, or for a timeout. We won't miss any changes that
            # came after our most recent apply_selector call, since we've
            # held the lock until now.
            self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
            self._description.check_compatible()
            now = _time()
            server_descriptions = self._description.apply_selector(
                selector, address,
                custom_selector=self._settings.server_selector)

        self._description.check_compatible()
        return server_descriptions 
Example 37
Project: TitanRover2019   Author: CSUFTitanRover   File: test_grid_file.py    GNU General Public License v3.0 5 votes vote down vote up
def test_grid_in_lazy_connect(self):
        client = MongoClient('badhost', connect=False,
                             serverSelectionTimeoutMS=10)
        fs = client.db.fs
        infile = GridIn(fs, file_id=-1, chunk_size=1)
        self.assertRaises(ServerSelectionTimeoutError, infile.write, b'data')
        self.assertRaises(ServerSelectionTimeoutError, infile.close) 
Example 38
Project: isomer   Author: isomeric   File: component.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def _read_config(self):
        """Read this component's configuration from the database"""

        try:
            self.config = self.componentmodel.find_one({"name": self.uniquename})
        except ServerSelectionTimeoutError:  # pragma: no cover
            self.log(
                "No database access! Check if mongodb is running " "correctly.",
                lvl=critical,
            )
        if self.config:
            self.log("Configuration read.", lvl=verbose)
        else:
            self.log("No configuration found.", lvl=warn)
            # self.log(self.config) 
Example 39
Project: csv-2-mongo   Author: clarketm   File: csv_2_mongo.py    MIT License 5 votes vote down vote up
def check_connection(mongo_client, host, port):
    try:
        mongo_client.server_info()
    except ServerSelectionTimeoutError as e:
        click.echo(f"Connection error while attempting to connect to {host}:{port}")
        exit(1) 
Example 40
Project: learn_python3_spider   Author: wistbean   File: topology.py    MIT License 5 votes vote down vote up
def select_servers(self,
                       selector,
                       server_selection_timeout=None,
                       address=None):
        """Return a list of Servers matching selector, or time out.

        :Parameters:
          - `selector`: function that takes a list of Servers and returns
            a subset of them.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value common.SERVER_SELECTION_TIMEOUT
            is used.
          - `address`: optional server address to select.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        if server_selection_timeout is None:
            server_timeout = self._settings.server_selection_timeout
        else:
            server_timeout = server_selection_timeout

        with self._lock:
            server_descriptions = self._select_servers_loop(
                selector, server_timeout, address)

            return [self.get_server_by_address(sd.address)
                    for sd in server_descriptions] 
Example 41
Project: learn_python3_spider   Author: wistbean   File: topology.py    MIT License 5 votes vote down vote up
def _select_servers_loop(self, selector, timeout, address):
        """select_servers() guts. Hold the lock when calling this."""
        now = _time()
        end_time = now + timeout
        server_descriptions = self._description.apply_selector(
            selector, address, custom_selector=self._settings.server_selector)

        while not server_descriptions:
            # No suitable servers.
            if timeout == 0 or now > end_time:
                raise ServerSelectionTimeoutError(
                    self._error_message(selector))

            self._ensure_opened()
            self._request_check_all()

            # Release the lock and wait for the topology description to
            # change, or for a timeout. We won't miss any changes that
            # came after our most recent apply_selector call, since we've
            # held the lock until now.
            self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
            self._description.check_compatible()
            now = _time()
            server_descriptions = self._description.apply_selector(
                selector, address,
                custom_selector=self._settings.server_selector)

        self._description.check_compatible()
        return server_descriptions 
Example 42
Project: DSReadingProject   Author: Bintong   File: topology.py    MIT License 5 votes vote down vote up
def select_servers(self,
                       selector,
                       server_selection_timeout=None,
                       address=None):
        """Return a list of Servers matching selector, or time out.

        :Parameters:
          - `selector`: function that takes a list of Servers and returns
            a subset of them.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value common.SERVER_SELECTION_TIMEOUT
            is used.
          - `address`: optional server address to select.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        if server_selection_timeout is None:
            server_timeout = self._settings.server_selection_timeout
        else:
            server_timeout = server_selection_timeout

        with self._lock:
            server_descriptions = self._select_servers_loop(
                selector, server_timeout, address)

            return [self.get_server_by_address(sd.address)
                    for sd in server_descriptions] 
Example 43
Project: DSReadingProject   Author: Bintong   File: topology.py    MIT License 5 votes vote down vote up
def _select_servers_loop(self, selector, timeout, address):
        """select_servers() guts. Hold the lock when calling this."""
        now = _time()
        end_time = now + timeout
        server_descriptions = self._description.apply_selector(
            selector, address, custom_selector=self._settings.server_selector)

        while not server_descriptions:
            # No suitable servers.
            if timeout == 0 or now > end_time:
                raise ServerSelectionTimeoutError(
                    self._error_message(selector))

            self._ensure_opened()
            self._request_check_all()

            # Release the lock and wait for the topology description to
            # change, or for a timeout. We won't miss any changes that
            # came after our most recent apply_selector call, since we've
            # held the lock until now.
            self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
            self._description.check_compatible()
            now = _time()
            server_descriptions = self._description.apply_selector(
                selector, address,
                custom_selector=self._settings.server_selector)

        self._description.check_compatible()
        return server_descriptions 
Example 44
Project: GlosBio   Author: MikolajBalcerek   File: SampleManager.py    MIT License 5 votes vote down vote up
def is_db_available(self) -> bool:
        """
        check database connection
        """
        try:
            self.db_client.server_info()
            return True
        except errors.AutoReconnect or errors.ServerSelectionTimeoutError:
            return False 
Example 45
Project: YetiToElastic   Author: BreakingMalwareResearch   File: yeti_to_elasticsearch.py    MIT License 5 votes vote down vote up
def create_mongo_connection(self, hostname="localhost"):
        """
            Creates a connection to YETI's MongoDB.
        :param hostname: Hostname to connect to. Default is "localhost"
        :return: None
        """

        try:
            # Try connecting to MongoDB for 10ms
            self.mongo_client = MongoClient('mongodb://{}:27017/'.format(hostname), serverSelectionTimeoutMS=10)
            self.mongo_client.server_info()
        except errors.ServerSelectionTimeoutError as mongo_conn_err:
            logger.exception(("MongoDB connection issue occurred. "
                              "Error message: " + str(mongo_conn_err)))
            sys.exit(1) 
Example 46
Project: cti-taxii-server   Author: oasis-open   File: mongodb_backend.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def catch_mongodb_error(func):
    """Catch mongodb availability error"""

    def api_wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except (ConnectionFailure, ServerSelectionTimeoutError) as e:
            raise MongoBackendError("Unable to connect to MongoDB", 500, e)

    return api_wrapper 
Example 47
Project: sc_spider   Author: wings27   File: pipelines.py    Apache License 2.0 5 votes vote down vote up
def save_item(self, item):
        try:
            self.collection.update({'url': item['url']}, dict(item), upsert=True)
        except ServerSelectionTimeoutError as e:
            logging.error('Fail to connect to mongodb. %s', e) 
Example 48
Project: minion-ci   Author: timofurrer   File: core.py    MIT License 5 votes vote down vote up
def ensure_mongo(func):
    @wraps(func)
    def func_wrapper(*args, **kwargs):
        client = MongoClient(serverSelectionTimeoutMS=500, connectTimeoutMS=500)
        try:
             # The ismaster command is cheap and does not require auth.
            client.admin.command('ismaster')
        except (errors.ServerSelectionTimeoutError, errors.AutoReconnect):
            raise MinionMongoError("Can't connect to mongodb")
        else:
            return func(*args, **kwargs)
        finally:
            client.close()
    return func_wrapper 
Example 49
Project: Kabba_AI   Author: hyperionxtech   File: base_case.py    Apache License 2.0 5 votes vote down vote up
def setUpClass(cls):
        from pymongo.errors import ServerSelectionTimeoutError
        from pymongo import MongoClient

        # Skip these tests if a mongo client is not running
        try:
            client = MongoClient(
                serverSelectionTimeoutMS=0.1
            )
            client.server_info()

        except ServerSelectionTimeoutError:
            raise SkipTest('Unable to connect to Mongo DB.') 
Example 50
Project: ETHdroidBot   Author: lytves   File: database.py    MIT License 5 votes vote down vote up
def __init__(self):

        self.client = MongoClient(serverSelectionTimeoutMS=10000)

        try:
            self.client.admin.command('ismaster')
            self.db = self.client[MONGO_DB_NAME]

            if MONGO_DB_COLLECTION in self.db.collection_names():

                self.collection = self.db[MONGO_DB_COLLECTION]
                self.connectionOK = True

        except ServerSelectionTimeoutError or ConnectionFailure:
            print("Server not available") 
Example 51
Project: smsweb   Author: awangga   File: topology.py    GNU Affero General Public License v3.0 4 votes vote down vote up
def select_servers(self,
                       selector,
                       server_selection_timeout=None,
                       address=None):
        """Return a list of Servers matching selector, or time out.

        :Parameters:
          - `selector`: function that takes a list of Servers and returns
            a subset of them.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value common.SERVER_SELECTION_TIMEOUT
            is used.
          - `address`: optional server address to select.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        if server_selection_timeout is None:
            server_timeout = self._settings.server_selection_timeout
        else:
            server_timeout = server_selection_timeout

        with self._lock:
            self._description.check_compatible()

            now = _time()
            end_time = now + server_timeout
            server_descriptions = self._apply_selector(selector, address)

            while not server_descriptions:
                # No suitable servers.
                if server_timeout == 0 or now > end_time:
                    raise ServerSelectionTimeoutError(
                        self._error_message(selector))

                self._ensure_opened()
                self._request_check_all()

                # Release the lock and wait for the topology description to
                # change, or for a timeout. We won't miss any changes that
                # came after our most recent _apply_selector call, since we've
                # held the lock until now.
                self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
                self._description.check_compatible()
                now = _time()
                server_descriptions = self._apply_selector(selector, address)

            return [self.get_server_by_address(sd.address)
                    for sd in server_descriptions] 
Example 52
Project: nyuki-legacy   Author: surycat   File: storage.py    Apache License 2.0 4 votes vote down vote up
def index(self):
        """
        Try to connect to mongo and index all the collections.
        """
        if self._db_name not in await self._client.list_database_names():
            # If the old mongo DB does not exist, use the new tenant format
            self._db_name = f"{os.environ['TENANT_ID']}#{self._db_name}"
        log.info("Selected database '%s'", self._db_name)

        self._db = self._client[self._db_name]
        # Collections
        self._workflow_templates = WorkflowTemplatesCollection(self._db)
        self._task_templates = TaskTemplatesCollection(self._db)
        self._workflow_metadata = MetadataCollection(self._db)
        self._workflow_instances = WorkflowInstancesCollection(self._db)
        self._task_instances = TaskInstancesCollection(self._db)
        self.regexes = DataProcessingCollection(self._db, 'regexes')
        self.lookups = DataProcessingCollection(self._db, 'lookups')
        self.triggers = TriggerCollection(self._db)

        log.info('Trying to connect to Mongo...')
        while True:
            try:
                await self._workflow_templates.index()
                await self._task_templates.index()
                await self._workflow_metadata.index()
                await self._workflow_instances.index()
                await self._task_instances.index()
                await self.regexes.index()
                await self.lookups.index()
                await self.triggers.index()
            except ServerSelectionTimeoutError as exc:
                log.error('Could not connect to Mongo - %s', exc)
            else:
                log.info('Successfully connected to Mongo')
                break

        if self._validate_on_start is True:
            collections = await self._db.list_collection_names()
            log.info('Validating %s collections', len(collections))
            for collection in collections:
                await self._db.validate_collection(collection)
                log.info('Validated collection %s', collection)

    # Templates 
Example 53
Project: satori   Author: leancloud   File: topology.py    Apache License 2.0 4 votes vote down vote up
def select_servers(self,
                       selector,
                       server_selection_timeout=None,
                       address=None):
        """Return a list of Servers matching selector, or time out.

        :Parameters:
          - `selector`: function that takes a list of Servers and returns
            a subset of them.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value common.SERVER_SELECTION_TIMEOUT
            is used.
          - `address`: optional server address to select.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        if server_selection_timeout is None:
            server_timeout = self._settings.server_selection_timeout
        else:
            server_timeout = server_selection_timeout

        with self._lock:
            self._description.check_compatible()

            now = _time()
            end_time = now + server_timeout
            server_descriptions = self._apply_selector(selector, address)

            while not server_descriptions:
                # No suitable servers.
                if server_timeout == 0 or now > end_time:
                    raise ServerSelectionTimeoutError(
                        self._error_message(selector))

                self._ensure_opened()
                self._request_check_all()

                # Release the lock and wait for the topology description to
                # change, or for a timeout. We won't miss any changes that
                # came after our most recent _apply_selector call, since we've
                # held the lock until now.
                self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
                self._description.check_compatible()
                now = _time()
                server_descriptions = self._apply_selector(selector, address)

            return [self.get_server_by_address(sd.address)
                    for sd in server_descriptions] 
Example 54
Project: setup   Author: mindbender-studio   File: topology.py    MIT License 4 votes vote down vote up
def select_servers(self,
                       selector,
                       server_selection_timeout=None,
                       address=None):
        """Return a list of Servers matching selector, or time out.

        :Parameters:
          - `selector`: function that takes a list of Servers and returns
            a subset of them.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value common.SERVER_SELECTION_TIMEOUT
            is used.
          - `address`: optional server address to select.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        if server_selection_timeout is None:
            server_timeout = self._settings.server_selection_timeout
        else:
            server_timeout = server_selection_timeout

        with self._lock:
            self._description.check_compatible()

            now = _time()
            end_time = now + server_timeout
            server_descriptions = self._description.apply_selector(
                selector, address)

            while not server_descriptions:
                # No suitable servers.
                if server_timeout == 0 or now > end_time:
                    raise ServerSelectionTimeoutError(
                        self._error_message(selector))

                self._ensure_opened()
                self._request_check_all()

                # Release the lock and wait for the topology description to
                # change, or for a timeout. We won't miss any changes that
                # came after our most recent apply_selector call, since we've
                # held the lock until now.
                self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
                self._description.check_compatible()
                now = _time()
                server_descriptions = self._description.apply_selector(
                    selector, address)

            return [self.get_server_by_address(sd.address)
                    for sd in server_descriptions] 
Example 55
Project: setup   Author: mindbender-studio   File: topology.py    MIT License 4 votes vote down vote up
def select_servers(self,
                       selector,
                       server_selection_timeout=None,
                       address=None):
        """Return a list of Servers matching selector, or time out.

        :Parameters:
          - `selector`: function that takes a list of Servers and returns
            a subset of them.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value common.SERVER_SELECTION_TIMEOUT
            is used.
          - `address`: optional server address to select.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        if server_selection_timeout is None:
            server_timeout = self._settings.server_selection_timeout
        else:
            server_timeout = server_selection_timeout

        with self._lock:
            self._description.check_compatible()

            now = _time()
            end_time = now + server_timeout
            server_descriptions = self._description.apply_selector(
                selector, address)

            while not server_descriptions:
                # No suitable servers.
                if server_timeout == 0 or now > end_time:
                    raise ServerSelectionTimeoutError(
                        self._error_message(selector))

                self._ensure_opened()
                self._request_check_all()

                # Release the lock and wait for the topology description to
                # change, or for a timeout. We won't miss any changes that
                # came after our most recent apply_selector call, since we've
                # held the lock until now.
                self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
                self._description.check_compatible()
                now = _time()
                server_descriptions = self._description.apply_selector(
                    selector, address)

            return [self.get_server_by_address(sd.address)
                    for sd in server_descriptions] 
Example 56
Project: research-software-directory   Author: research-software-directory   File: error_handlers.py    Apache License 2.0 4 votes vote down vote up
def init(app):
    @app.errorhandler(RouteException)
    @jsonify
    def handle_route_exception(exception):
        data = {
            "error": exception.message,
            "class": exception.__class__.__name__,
            "traceback": traceback.format_tb(exception.__traceback__),
            "data": exception.data
        }
        return data, exception.status_code

    @app.errorhandler(Exception)
    @jsonify
    def handle_exception(exception):
        data = {
            "error": str(exception),
            "class": exception.__class__.__name__,
            "traceback": traceback.format_tb(exception.__traceback__),
        }
        logger.warning('general exception: ' + str_format_exception(exception))
        return data, 500

    @app.errorhandler(404)
    @jsonify
    def _page_not_found(exception):
        data = {
            "error": "Resource not found",
            "class": "PageNotFoundException",
            "traceback": traceback.format_tb(exception.__traceback__),
        }
        return data, 404

    @app.errorhandler(ValidationError)
    @jsonify
    def _validation_error(exception):
        data = {
            "error": exception.message,
            "class": "ValidationError",
            "path": list(exception.absolute_path)
        }
        return data, 400

    @app.errorhandler(ServerSelectionTimeoutError)
    @jsonify
    def _mongo_connect_error(exception):
        data = {
            "error": "Couldn't connect to Mongo database: " + str(exception),
            "class": "ServerSelectionTimeoutError",
        }
        logger.error('Could not connect to Mongo')
        return data, 500 
Example 57
Project: opsbro   Author: naparuba   File: topology.py    MIT License 4 votes vote down vote up
def select_servers(self,
                       selector,
                       server_selection_timeout=None,
                       address=None):
        """Return a list of Servers matching selector, or time out.

        :Parameters:
          - `selector`: function that takes a list of Servers and returns
            a subset of them.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value common.SERVER_SELECTION_TIMEOUT
            is used.
          - `address`: optional server address to select.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        if server_selection_timeout is None:
            server_timeout = self._settings.server_selection_timeout
        else:
            server_timeout = server_selection_timeout

        with self._lock:
            self._description.check_compatible()

            now = _time()
            end_time = now + server_timeout
            server_descriptions = self._description.apply_selector(
                selector, address)

            while not server_descriptions:
                # No suitable servers.
                if server_timeout == 0 or now > end_time:
                    raise ServerSelectionTimeoutError(
                        self._error_message(selector))

                self._ensure_opened()
                self._request_check_all()

                # Release the lock and wait for the topology description to
                # change, or for a timeout. We won't miss any changes that
                # came after our most recent apply_selector call, since we've
                # held the lock until now.
                self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
                self._description.check_compatible()
                now = _time()
                server_descriptions = self._description.apply_selector(
                    selector, address)

            return [self.get_server_by_address(sd.address)
                    for sd in server_descriptions] 
Example 58
Project: docker   Author: getavalon   File: topology.py    MIT License 4 votes vote down vote up
def select_servers(self,
                       selector,
                       server_selection_timeout=None,
                       address=None):
        """Return a list of Servers matching selector, or time out.

        :Parameters:
          - `selector`: function that takes a list of Servers and returns
            a subset of them.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value common.SERVER_SELECTION_TIMEOUT
            is used.
          - `address`: optional server address to select.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        if server_selection_timeout is None:
            server_timeout = self._settings.server_selection_timeout
        else:
            server_timeout = server_selection_timeout

        with self._lock:
            self._description.check_compatible()

            now = _time()
            end_time = now + server_timeout
            server_descriptions = self._description.apply_selector(
                selector, address)

            while not server_descriptions:
                # No suitable servers.
                if server_timeout == 0 or now > end_time:
                    raise ServerSelectionTimeoutError(
                        self._error_message(selector))

                self._ensure_opened()
                self._request_check_all()

                # Release the lock and wait for the topology description to
                # change, or for a timeout. We won't miss any changes that
                # came after our most recent apply_selector call, since we've
                # held the lock until now.
                self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
                self._description.check_compatible()
                now = _time()
                server_descriptions = self._description.apply_selector(
                    selector, address)

            return [self.get_server_by_address(sd.address)
                    for sd in server_descriptions] 
Example 59
Project: mongodb-monitoring   Author: jruaux   File: topology.py    Apache License 2.0 4 votes vote down vote up
def select_servers(self,
                       selector,
                       server_selection_timeout=None,
                       address=None):
        """Return a list of Servers matching selector, or time out.

        :Parameters:
          - `selector`: function that takes a list of Servers and returns
            a subset of them.
          - `server_selection_timeout` (optional): maximum seconds to wait.
            If not provided, the default value common.SERVER_SELECTION_TIMEOUT
            is used.
          - `address`: optional server address to select.

        Calls self.open() if needed.

        Raises exc:`ServerSelectionTimeoutError` after
        `server_selection_timeout` if no matching servers are found.
        """
        if server_selection_timeout is None:
            server_timeout = self._settings.server_selection_timeout
        else:
            server_timeout = server_selection_timeout

        with self._lock:
            self._description.check_compatible()

            now = _time()
            end_time = now + server_timeout
            server_descriptions = self._apply_selector(selector, address)

            while not server_descriptions:
                # No suitable servers.
                if server_timeout == 0 or now > end_time:
                    raise ServerSelectionTimeoutError(
                        self._error_message(selector))

                self._ensure_opened()
                self._request_check_all()

                # Release the lock and wait for the topology description to
                # change, or for a timeout. We won't miss any changes that
                # came after our most recent _apply_selector call, since we've
                # held the lock until now.
                self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
                self._description.check_compatible()
                now = _time()
                server_descriptions = self._apply_selector(selector, address)

            return [self.get_server_by_address(sd.address)
                    for sd in server_descriptions]