Your IP : 216.73.216.213


Current Path : /opt/alt/python35/lib/python3.5/site-packages/aioredis/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib/python3.5/site-packages/aioredis/__pycache__/pool.cpython-35.pyc



�D�Z�A�@sddlZddlZddlZddlmZmZddlmZddlm	Z	ddl
mZddlm
Z
ddlmZd	dd
dddddd
ddddddddddddddd�ZGdd�de
�ZGdd�d�ZGdd�d�ZdS)�N�)�create_connection�_PUBSUB_COMMANDS)�logger)�	parse_url)�PoolClosedError)�AbcPool)�Lock�db�password�ssl�encoding�minsize�maxsize�
�parser�loop�create_connection_timeout�pool_cls�connection_clsc�s�|
r*t|
t�s!td��|
}nt}t|t�r�t|�\}}
|
jd|�}|
jd|�}|
jd|�}|
jd|	�}	d|
kr�|
ds�|
dr�|s�td|
d|f��|p�|
d}|||||d|d	|d|d
|d|	d|d
|�}y|jdd�IdHWnAt	k
r�}z!|j
�|j�IdH�WYdd}~XnX|S)a{Creates Redis Pool.

    By default it creates pool of Redis instances, but it is
    also possible to create pool of plain connections by passing
    ``lambda conn: conn`` as commands_factory.

    *commands_factory* parameter is deprecated since v0.2.9

    All arguments are the same as for create_connection.

    Returns RedisPool instance or a pool_cls if it is given.
    z-pool_class does not meet the AbcPool contractr
rr
�timeoutrzConflicting ssl options are setrrrrrr�override_minFN)�
issubclassr�AssertionError�ConnectionsPool�
isinstance�strr�
setdefault�
_fill_free�	Exception�close�wait_closed)�addressr
rrr
rrrrrrr�cls�options�pool�ex�r'�>/opt/alt/python35/lib/python3.5/site-packages/aioredis/pool.py�create_pool
s:			
r)c@s;eZdZdZddddddddddddddd	�Zd
d�Zedd
��Zedd��Zedd��Z	edd��Z
edd��Zdd�Zdd�Z
dd�Zdd�Zedd��Zd d!�Zed"d#��Zed$d%��Zd&d'�Zd(d)�Zfd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zed6d7��Zed8d9��Zed:d;��Zdfd<d=�Zd>d?�Z d@dA�Z!dBdC�Z"dDdE�Z#ddFdG�Z$dHdI�Z%dJdK�Z&dLdM�Z'dNdO�Z(dS)PrzRedis connections pool.NrrrrrcCs�t|t�r|dks6td|t|�f��|dk	sNtd��t|t�ri|dks�td|t|�f��||ks�td||f��|dkr�tj�}||_||_||_||_	||_
||_||_|	|_
||_tjd|�|_t�|_d|_tjdtd|�d|�|_tjd|�|_d|_d|_|
|_dS)	Nrzminsize must be int >= 0z"Arbitrary pool size is disallowed.zmaxsize must be int > 0zInvalid pool min/max sizes�maxlen�lockr)r�intr�type�asyncioZget_event_loop�_address�_db�	_password�_ssl�	_encoding�
_parser_class�_minsize�_create_connection_timeout�_loop�collections�deque�_pool�set�_used�
_acquiring�	Conditionr	�_cond�Event�_close_state�
_close_waiter�_pubsub_conn�_connection_cls)�selfr"r
rr
rrrrrrrr'r'r(�__init__Cs4										$		zConnectionsPool.__init__cCs+dj|jj|j|j|j|j�S)Nz#<{} [db:{}, size:[{}:{}], free:{}]>)�format�	__class__�__name__r
rr�freesize)rEr'r'r(�__repr__cszConnectionsPool.__repr__cCs|jS)zMinimum pool size.)r5)rEr'r'r(rhszConnectionsPool.minsizecCs
|jjS)zMaximum pool size.)r:r*)rEr'r'r(rmszConnectionsPool.maxsizecCs|jt|j�|jS)zCurrent pool size.)rJ�lenr<r=)rEr'r'r(�sizerszConnectionsPool.sizecCs
t|j�S)z#Current number of free connections.)rLr:)rEr'r'r(rJwszConnectionsPool.freesizecCs|jS)N)r/)rEr'r'r(r"|szConnectionsPool.addressc	�s)|jIdH�|j�IdHWdQRXdS)zPClear pool connections.

        Close and remove all free connections.
        N)r?�	_do_clear)rEr'r'r(�clear�szConnectionsPool.clearc�sag}x9|jrA|jj�}|j�|j|j��q	Wtj|d|j�IdHdS)Nr)r:�popleftr �appendr!r.�gatherr7)rE�waiters�connr'r'r(rN�s
zConnectionsPool._do_clearc�s�|jj�IdH|jIdH��|js:t|j��g}x9|jr{|jj�}|j�|j|j	��qCWx.|j
D]#}|j�|j|j	��q�Wtj|d|j
�IdHtjdt|��WdQRXdS)NrzClosed %d connection(s))rA�waitr?r=rr:rPr rQr!r<r.rRr7r�debugrL)rErSrTr'r'r(�	_do_close�s

zConnectionsPool._do_closecCsA|jj�s=tj|j�d|j�|_|jj�dS)zLClose all free and in-progress connections and mark pool as closed.
        rN)rA�is_setr.�
ensure_futurerWr7rBr;)rEr'r'r(r �szConnectionsPool.closecCs
|jj�S)zTrue if pool is closed.)rArX)rEr'r'r(�closed�szConnectionsPool.closedc�sI|jj�IdH|jdk	s't�tj|jd|j�IdHdS)zWait until pool gets closed.Nr)rArUrBrr.Zshieldr7)rEr'r'r(r!�szConnectionsPool.wait_closedcCs
|jpdS)zCurrently selected db index.r)r0)rEr'r'r(r
�szConnectionsPool.dbcCs|jS)zCurrent set codec or None.)r3)rEr'r'r(r
�szConnectionsPool.encodingcOs�|j||�\}}|dk	rO|j|||�}|j||||�S|j||||�}|j||||�SdS)a+Executes redis command in a free connection and returns
        future waiting for result.

        Picks connection from free pool and send command through
        that connection.
        If no connection is found, returns coroutine waiting for
        free connection to execute command.
        N)�get_connection�execute�
_check_result�
_wait_execute)rE�command�args�kwrTr"�fut�coror'r'r(r\�s	zConnectionsPool.executecGsK|j|�\}}|dk	r1|j||�S|j|||i�SdS)a�Executes Redis (p)subscribe/(p)unsubscribe commands.

        ConnectionsPool picks separate connection for pub/sub
        and uses it until explicitly closed or disconnected
        (unsubscribing from all channels/patterns will leave connection
         locked for pub/sub use).

        There is no auto-reconnect for this PUB/SUB connection.

        Returns asyncio.gather coroutine waiting for all channels/patterns
        to receive answers.
        N)r[�execute_pubsub�_wait_execute_pubsub)rEr_ZchannelsrTr"r'r'r(rd�s
zConnectionsPool.execute_pubsubcCs�|j�j�}|tk}|rU|jrU|jjsL|j|jjfSd|_x�t|j�D]t}|jd}|jj	d�|jr�qe|j
r�qe|r�||_|jj|�|jj
|�||jfSWd|jfS)zDGet free connection from pool.

        Returns connection.
        Nrr)�upper�striprrCrZr"�rangerJr:�rotate�	in_pubsub�remover<�addr/)rEr_r`Z	is_pubsub�irTr'r'r(r[�s&	
			zConnectionsPool.get_connectioncGs|S)zjHook to check result or catch exception (like MovedError).

        This method can be coroutine.
        r')rErb�datar'r'r(r]�szConnectionsPool._check_resultc�sH|j||�IdH}z|j|||�IdHSWd|j|�XdS)z'Acquire connection and execute command.N)�acquirer\�release)rEr"r_r`rarTr'r'r(r^szConnectionsPool._wait_executec�s�|jrtd��|jdksE|jjsEtd|jf��|jIdH�s|jritd��|jdks�|jjr�|j|�IdH}||_|j}|j|||�IdHSWdQRXdS)NzPool is closedz Expected no or closed connection)rZrrCrr?�_create_new_connectionrd)rEr"r_r`rarTr'r'r(res				z$ConnectionsPool._wait_execute_pubsubc
�sdd}|jIdH�IxAt|j�D]'}|oI|j|j|�IdH}q%W||_WdQRX|S)z~Changes db index for all free connections.

        All previously acquired connections will be closed when released.
        TN)r?rhrJr:�selectr0)rEr
�resrmr'r'r(rrs%zConnectionsPool.selectc
�sV||_|jIdH�8x0t|j�D]}|j|j|�IdHq(WWdQRXdS)N)r1r?rhrJr:�auth)rErrmr'r'r(rt&s	zConnectionsPool.authcCs$|jr |jjr |jjSdS)Nr)rCrZrj)rEr'r'r(rj,s
zConnectionsPool.in_pubsubcCs-|jr |jjr |jjStji�S)N)rCrZ�pubsub_channels�types�MappingProxyType)rEr'r'r(ru2s
zConnectionsPool.pubsub_channelscCs-|jr |jjr |jjStji�S)N)rCrZ�pubsub_patternsrvrw)rEr'r'r(rx8s
zConnectionsPool.pubsub_patternsc
�s�|jrtd��|jIdH��|jr9td��x�|jdd�IdH|jr�|jj�}|jst|��||jks�t||jf��|jj	|�|S|jj
�IdHq<WWdQRXdS)zYAcquires a connection from free pool.

        Creates new connection if needed.
        zPool is closedNrT)rZrr?rrJr:rPrr<rlrU)rEr_r`rTr'r'r(ro>s			$zConnectionsPool.acquirecCs#||jks!td|f��|jj|�|js|jr`tjd|�|j�n�|jr�tjd|�|j�n}|j	r�tjd|�|j�nW|j
|j
kr�|jr�|j|jkr�|j
j|�q|j�n
|j�tj|j�d|j�dS)z�Returns used connection back into pool.

        When returned connection has db index that differs from one in pool
        the connection will be closed and dropped.
        When queue of free connections is full the connection will be dropped.
        z)Invalid connection, maybe from other poolz,Connection %r is in transaction, closing it.z/Connection %r is in subscribe mode, closing it.z/Connection %r has pending commands, closing it.rN)r<rrkrZZin_transactionrZwarningr rj�_waitersr
rrJr:rQr.rY�_wakeupr7)rErTr'r'r(rpSs,		

	

	



zConnectionsPool.releasecCsTxMt|j�D]<}|jd}|jr<|jj�q|jjd�qWdS)Nrr)rhrJr:rZrPri)rErmrTr'r'r(�_drop_closedus

	zConnectionsPool._drop_closedc�s|j�x|j|jkr�|jd7_z=|j|j�IdH}|jd�IdH|jj|�Wd|jd8_|j�Xq
W|j	r�dS|rxw|jr|j|j
kr|jd7_z+|j|j�IdH}|jj|�Wd|jd8_|j�Xq�WdS)NrZping)r{rMrr=rqr/r\r:rQrJr)rErrTr'r'r(r}s&
	zConnectionsPool._fill_freecCsRt|d|jd|jd|jd|jd|jd|jd|jd|j�S)	Nr
rrr
rrrr)	rr0r1r2r3r4r6rDr7)rEr"r'r'r(rq�s								z&ConnectionsPool._create_new_connectionc	�sB|jIdH�|jj�WdQRX|dk	r>|j�IdHdS)N)r?�notifyr!)rEZclosing_connr'r'r(rz�szConnectionsPool._wakeupcCstd��dS)Nz6'await' should be used as a context manager expression)�RuntimeError)rEr'r'r(�	__enter__�szConnectionsPool.__enter__cGsdS)Nr')rEr`r'r'r(�__exit__�szConnectionsPool.__exit__ccs$|j�j�EdH}t||�S)N)ro�	__await__�_ConnectionContextManager)rErTr'r'r(r��szConnectionsPool.__await__cCs
t|�S)z�Return async context manager for working with connection.

        async with pool.get() as conn:
            await conn.execute('get', 'my-key')
        )�_AsyncConnectionContextManager)rEr'r'r(�get�szConnectionsPool.get))rI�
__module__�__qualname__�__doc__rFrK�propertyrrrMrJr"rOrNrWr rZr!r
r
r\rdr[r]r^rerrrtrjrurxrorpr{rrqrzr~rr�r�r'r'r'r(r@sR
"rc@s:eZdZd
Zdd�Zdd�Zdd�Zd	S)r�r:�_conncCs||_||_dS)N)r:r�)rEr%rTr'r'r(rF�s	z"_ConnectionContextManager.__init__cCs|jS)N)r�)rEr'r'r(r~�sz#_ConnectionContextManager.__enter__cCs1z|jj|j�Wdd|_d|_XdS)N)r:rpr�)rE�exc_type�	exc_value�tbr'r'r(r�s	z"_ConnectionContextManager.__exit__N)r:r�)rIr�r��	__slots__rFr~rr'r'r'r(r��sr�c@s:eZdZd
Zdd�Zdd�Zdd�Zd	S)r�r:r�cCs||_d|_dS)N)r:r�)rEr%r'r'r(rF�s	z'_AsyncConnectionContextManager.__init__c�s$|jj�IdH}||_|jS)N)r:ror�)rErTr'r'r(�
__aenter__�s	z)_AsyncConnectionContextManager.__aenter__c�s1z|jj|j�Wdd|_d|_XdS)N)r:rpr�)rEr�r�r�r'r'r(�	__aexit__�s	z(_AsyncConnectionContextManager.__aexit__N)r:r�)rIr�r�r�rFr�r�r'r'r'r(r��sr�)r.r8rvZ
connectionrr�logr�utilr�errorsr�abcrZlocksr	r)rr�r�r'r'r'r(�<module>s 0��