<s> """<STR_LIT>""" <EOL> import contextlib <EOL> import threading <EOL> import six <EOL> from gcloud . bigtable . happybase . connection import Connection <EOL> from gcloud . bigtable . happybase . connection import _get_cluster <EOL> _MIN_POOL_SIZE = <NUM_LIT:1> <EOL> """<STR_LIT>""" <EOL> class NoConnectionsAvailable ( RuntimeError ) : <EOL> """<STR_LIT>""" <EOL> class ConnectionPool ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , size , ** kwargs ) : <EOL> if not isinstance ( size , six . integer_types ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if size < _MIN_POOL_SIZE : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . _lock = threading . Lock ( ) <EOL> self . _queue = six . moves . queue . LifoQueue ( maxsize = size ) <EOL> self . _thread_connections = threading . local ( ) <EOL> connection_kwargs = kwargs <EOL> connection_kwargs [ '<STR_LIT>' ] = False <EOL> if '<STR_LIT>' not in connection_kwargs : <EOL> connection_kwargs [ '<STR_LIT>' ] = _get_cluster ( <EOL> timeout = kwargs . get ( '<STR_LIT>' ) ) <EOL> for _ in six . moves . range ( size ) : <EOL> connection = Connection ( ** connection_kwargs ) <EOL> self . _queue . put ( connection ) <EOL> def _acquire_connection ( self , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _queue . get ( block = True , timeout = timeout ) <EOL> except six . moves . queue . Empty : <EOL> raise NoConnectionsAvailable ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> @ contextlib . contextmanager <EOL> def connection ( self , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> connection = getattr ( self . _thread_connections , '<STR_LIT>' , None ) <EOL> retrieved_new_cnxn = False <EOL> if connection is None : <EOL> retrieved_new_cnxn = True <EOL> connection = self . _acquire_connection ( timeout ) <EOL> with self . _lock : <EOL> self . _thread_connections . current = connection <EOL> connection . open ( ) <EOL> yield connection <EOL> if retrieved_new_cnxn : <EOL> del self . _thread_connections . current <EOL> self . _queue . put ( connection ) </s>
