Avoid to acquire the lazy_connect lock if it is not required
This commit is contained in:
		@@ -15,7 +15,6 @@
 | 
			
		||||
from collections import namedtuple, defaultdict
 | 
			
		||||
import logging
 | 
			
		||||
import six
 | 
			
		||||
from warnings import warn
 | 
			
		||||
import threading
 | 
			
		||||
 | 
			
		||||
from cassandra.cluster import Cluster, _NOT_SET, NoHostAvailable, UserTypeDoesNotExist
 | 
			
		||||
@@ -105,14 +104,9 @@ def setup(
 | 
			
		||||
    :param list hosts: list of hosts, (``contact_points`` for :class:`cassandra.cluster.Cluster`)
 | 
			
		||||
    :param str default_keyspace: The default keyspace to use
 | 
			
		||||
    :param int consistency: The global default :class:`~.ConsistencyLevel` - default is the same as :attr:`.Session.default_consistency_level`
 | 
			
		||||
    :param bool lazy_connect: True if should not connect until first use (Deprecated)
 | 
			
		||||
    :param bool lazy_connect: True if should not connect until first use
 | 
			
		||||
    :param bool retry_connect: True if we should retry to connect even if there was a connection failure initially
 | 
			
		||||
    :param \*\*kwargs: Pass-through keyword arguments for :class:`cassandra.cluster.Cluster`
 | 
			
		||||
 | 
			
		||||
    .. warning ::
 | 
			
		||||
 | 
			
		||||
      lazy_connect is deprecated. It will be removed in 3.3.
 | 
			
		||||
 | 
			
		||||
    """
 | 
			
		||||
    global cluster, session, lazy_connect_args
 | 
			
		||||
 | 
			
		||||
@@ -123,9 +117,6 @@ def setup(
 | 
			
		||||
    models.DEFAULT_KEYSPACE = default_keyspace
 | 
			
		||||
 | 
			
		||||
    if lazy_connect:
 | 
			
		||||
        msg = "lazy_connect is deprecated. It will be removed in 3.3."
 | 
			
		||||
        warn(msg, DeprecationWarning)
 | 
			
		||||
        log.warning(msg)
 | 
			
		||||
        kwargs['default_keyspace'] = default_keyspace
 | 
			
		||||
        kwargs['consistency'] = consistency
 | 
			
		||||
        kwargs['lazy_connect'] = False
 | 
			
		||||
@@ -199,12 +190,20 @@ def get_cluster():
 | 
			
		||||
 | 
			
		||||
def handle_lazy_connect():
 | 
			
		||||
    global lazy_connect_args
 | 
			
		||||
 | 
			
		||||
    # if lazy_connect_args is None, it means the cluster is setup and ready
 | 
			
		||||
    # No need to acquire the lock
 | 
			
		||||
    if not lazy_connect_args:
 | 
			
		||||
        return
 | 
			
		||||
 | 
			
		||||
    with lazy_connect_lock:
 | 
			
		||||
        # lazy_connect_args might have been set to None by another thread while waiting the lock
 | 
			
		||||
        # In this case, do nothing.
 | 
			
		||||
        if lazy_connect_args:
 | 
			
		||||
            log.debug("lazy connect")
 | 
			
		||||
            hosts, kwargs = lazy_connect_args
 | 
			
		||||
            lazy_connect_args = None
 | 
			
		||||
            setup(hosts, **kwargs)
 | 
			
		||||
            lazy_connect_args = None
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def register_udt(keyspace, type_name, klass):
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user