<s> import collections <EOL> import sys <EOL> import textwrap <EOL> from logging import WARNING <EOL> from gremlinclient . graph import GraphDatabase <EOL> from gremlinclient . log import pool_logger <EOL> class Pool ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , graph , maxsize = <NUM_LIT> , loop = None , force_release = False , <EOL> future_class = None ) : <EOL> self . _graph = graph <EOL> self . _maxsize = maxsize <EOL> self . _pool = collections . deque ( ) <EOL> self . _waiters = collections . deque ( ) <EOL> self . _acquired = set ( ) <EOL> self . _acquiring = <NUM_LIT:0> <EOL> self . _closed = False <EOL> self . _loop = loop <EOL> self . _force_release = force_release <EOL> self . _future_class = self . _graph . future_class <EOL> @ property <EOL> def freesize ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . _pool ) <EOL> @ property <EOL> def size ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . _acquired ) + self . _acquiring + self . freesize <EOL> @ property <EOL> def maxsize ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _maxsize <EOL> @ property <EOL> def graph ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _graph <EOL> @ property <EOL> def pool ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _pool <EOL> @ property <EOL> def closed ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _closed or self . _graph is None <EOL> def acquire ( self ) : <EOL> """<STR_LIT>""" <EOL> future = self . _future_class ( ) <EOL> if self . _pool : <EOL> while self . _pool : <EOL> conn = self . _pool . popleft ( ) <EOL> if not conn . closed : <EOL> pool_logger . debug ( "<STR_LIT>" . format ( conn ) ) <EOL> future . set_result ( conn ) <EOL> self . _acquired . add ( conn ) <EOL> break <EOL> else : <EOL> pool_logger . debug ( <EOL> "<STR_LIT>" . format ( conn ) ) <EOL> elif self . size < self . maxsize : <EOL> self . _acquiring += <NUM_LIT:1> <EOL> conn_future = self . graph . connect ( <EOL> force_release = self . _force_release , pool = self ) <EOL> def cb ( f ) : <EOL> try : <EOL> conn = f . result ( ) <EOL> except Exception as e : <EOL> future . set_exception ( e ) <EOL> else : <EOL> pool_logger . debug ( "<STR_LIT>" . format ( conn ) ) <EOL> self . _acquired . add ( conn ) <EOL> future . set_result ( conn ) <EOL> finally : <EOL> self . _acquiring -= <NUM_LIT:1> <EOL> conn_future . add_done_callback ( cb ) <EOL> else : <EOL> pool_logger . debug ( <EOL> "<STR_LIT>" . format ( future ) ) <EOL> self . _waiters . append ( future ) <EOL> return future <EOL> def release ( self , conn ) : <EOL> """<STR_LIT>""" <EOL> future = self . _future_class ( ) <EOL> if self . size <= self . maxsize : <EOL> if conn . closed : <EOL> pool_logger . info ( <EOL> "<STR_LIT>" . format ( conn ) ) <EOL> self . _acquired . remove ( conn ) <EOL> conn = None <EOL> elif self . _waiters : <EOL> waiter = self . _waiters . popleft ( ) <EOL> waiter . set_result ( conn ) <EOL> pool_logger . debug ( <EOL> "<STR_LIT>" . format ( conn ) ) <EOL> else : <EOL> self . _pool . append ( conn ) <EOL> self . _acquired . remove ( conn ) <EOL> future . set_result ( None ) <EOL> else : <EOL> future_conn = conn . close ( ) <EOL> future_conn . add_done_callback ( <EOL> lambda f : future . set_result ( f . result ( ) ) ) <EOL> return future <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> while self . pool : <EOL> conn = self . pool . popleft ( ) <EOL> conn . close ( ) <EOL> while self . _waiters : <EOL> f = self . _waiters . popleft ( ) <EOL> f . cancel ( ) <EOL> self . _graph = None <EOL> self . _closed = True <EOL> pool_logger . info ( <EOL> "<STR_LIT>" . format ( self ) ) <EOL> def __enter__ ( self ) : <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" ) <EOL> def __exit__ ( self , * args ) : <EOL> pass </s>
