ó á¬_Zc@sddlZddlZddlZddlZyddlmZmZWn'ek rsddlmZmZnXddl m Z ej dƒZ de fd„ƒYZdefd „ƒYZd efd „ƒYZd e fd „ƒYZdefd„ƒYZdS(iÿÿÿÿN(t PriorityQueuetEmptyi(tImproperlyConfiguredt elasticsearchtConnectionSelectorcBs eZdZd„Zd„ZRS(sŸ Simple class used to select a connection from a list of currently live connection instances. In init time it is passed a dictionary containing all the connections' options which it can then use during the selection process. When the `select` method is called it is given a list of *currently* live connections to choose from. The options dictionary is the one that has been passed to :class:`~elasticsearch.Transport` as `hosts` param and the same that is used to construct the Connection object itself. When the Connection was created from information retrieved from the cluster via the sniffing process it will be the dictionary returned by the `host_info_callback`. Example of where this would be useful is a zone-aware selector that would only select connections from it's own zones and only fall back to other connections where there would be none in it's zones. cCs ||_dS(sQ :arg opts: dictionary of connection instances and their options N(tconnection_opts(tselftopts((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pyt__init__!scCsdS(s} Select a connection from the given list. :arg connections: list of live connections to choose from N((Rt connections((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pytselect's(t__name__t __module__t__doc__RR (((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pyRs tRandomSelectorcBseZdZd„ZRS(s' Select a connection at random cCs tj|ƒS(N(trandomtchoice(RR ((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pyR 4s(R R R R (((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pyR0stRoundRobinSelectorcBs eZdZd„Zd„ZRS(s% Selector using round-robin. cCs)tt|ƒj|ƒtjƒ|_dS(N(tsuperRRt threadingtlocaltdata(RR((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pyR<scCsEt|jddƒd|j_|jjt|ƒ;_||jjS(Ntrriÿÿÿÿi(tgetattrRRtlen(RR ((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pyR @s(R R R RR (((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pyR8s tConnectionPoolcBsVeZdZddeed„Zd d„Zd„Ze d„Z d„Z d„Z RS( s\ Container holding the :class:`~elasticsearch.Connection` instances, managing the selection process (via a :class:`~elasticsearch.ConnectionSelector`) and dead connections. It's only interactions are with the :class:`~elasticsearch.Transport` class that drives all the actions within `ConnectionPool`. Initially connections are stored on the class as a list and, along with the connection options, get passed to the `ConnectionSelector` instance for future reference. Upon each request the `Transport` will ask for a `Connection` via the `get_connection` method. If the connection fails (it's `perform_request` raises a `ConnectionError`) it will be marked as dead (via `mark_dead`) and put on a timeout (if it fails N times in a row the timeout is exponentially longer - the formula is `default_timeout * 2 ** (fail_count - 1)`). When the timeout is over the connection will be resurrected and returned to the live pool. A connection that has been previously marked as dead and succeeds will be marked as live (its fail count will be deleted). i<ic Ks·|stdƒ‚n||_g|D]\}}|^q%|_t|jƒ|_tt|jƒƒ|_i|_|rŒt j |jƒn||_ ||_ |t |ƒƒ|_dS(s• :arg connections: list of tuples containing the :class:`~elasticsearch.Connection` instance and it's options :arg dead_timeout: number of seconds a connection should be retired for after a failure, increases on consecutive failures :arg timeout_cutoff: number of consecutive failures after which the timeout doesn't increase :arg selector_class: :class:`~elasticsearch.ConnectionSelector` subclass to use if more than one connection is live :arg randomize_hosts: shuffle the list of connections upon arrival to avoid dog piling effect across processes s>No defined connections, you need to specify at least one host.N(RRR ttupletorig_connectionsRRtdeadt dead_countRtshufflet dead_timeoutttimeout_cutofftdicttselector( RR RR tselector_classtrandomize_hoststkwargstcR((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pyR[s "   cCs¼|r |n tjƒ}y|jj|ƒWntk r@dSX|jj|dƒd}||j|<|jdt|d|jƒ}|j j |||fƒt j d|||ƒdS(s£ Mark the connection as dead (failed). Remove it from the live pool and put it on a timeout. :arg connection: the failed instance NiiisMConnection %r has failed for %i times in a row, putting on %i second timeout.( ttimeR tremovet ValueErrorRtgetRtminR Rtputtloggertwarning(Rt connectiontnowRttimeout((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pyt mark_deads  !cCs&y|j|=Wntk r!nXdS(s© Mark connection as healthy after a resurrection. Resets the fail counter for the connection. :arg connection: the connection to redeem N(RtKeyError(RR/((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pyt mark_live—s cCsÍ|jjƒr)|r%tj|jƒSdSy|jjdtƒ\}}Wn(tk rr|rntj|jƒSdSX| r¦|tjƒkr¦|jj ||fƒdS|j j |ƒt j d||ƒ|S(s¹ Attempt to resurrect a connection from the dead pool. It will try to locate one (not all) eligible (it's timeout is over) connection to return to the live pool. Any resurrected connection is also returned. :arg force: resurrect a connection even if there is none eligible (used when we have no live connections). If force is specified resurrect always returns a connection. Ntblocks&Resurrecting connection %r (force=%s).(RtemptyRRRR*tFalseRR'R,R tappendR-tinfo(RtforceR1R/((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pyt resurrect¤s  cCsQ|jƒ|j}|s'|jtƒSt|ƒdkrI|jj|ƒS|dS(sv Return a connection from the pool using the `ConnectionSelector` instance. It tries to resurrect eligible connections, forces a resurrection when no connections are availible and passes the list of live connections to the selector instance to choose from. Returns a connection instance and it's current fail count. ii(R;R tTrueRR"R (RR ((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pytget_connectionÎs   cCs"x|jD]}|jƒq WdS(s/ Explicitly closes connections N(Rtclose(Rtconn((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pyR>çsN( R R R RR<RtNoneR2R4R7R;R=R>(((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pyREs#  * tDummyConnectionPoolcBs:eZd„Zd„Zd„Zd„ZeZZZRS(cKsNt|ƒdkr!tdƒ‚n||_|dd|_|jf|_dS(Nis9DummyConnectionPool needs exactly one connection defined.i(RRRR/R (RR R%((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pyRïs  cCs|jS(N(R/(R((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pyR=øscCs|jjƒdS(s/ Explicitly closes connections N(R/R>(R((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pyR>ûscOsdS(N((RtargsR%((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pyt_noops( R R RR=R>RCR2R4R;(((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pyRAîs    (R'RtloggingRtQueueRRt ImportErrortqueuet exceptionsRt getLoggerR-tobjectRRRRRA(((sx/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-W9yz6j/elasticsearch/elasticsearch/connection_pool.pyts     ! ©