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.opt-1.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|
}nt}t|t�r�t|�\}}
|
jd|�}|
jd|�}|
jd|�}|
jd|	�}	d|
kr�|p�|
d}|||||d|d|d|d|d	|	d
|d|�}y|jdd
�IdHWnAtk
r5}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.
    r
rr
�timeoutrrrrrrr�override_minFN)	�ConnectionsPool�
isinstance�strr�
setdefault�
_fill_free�	Exception�close�wait_closed)�addressr
rrr
rrrrrrr�cls�options�poolZex�r$�>/opt/alt/python35/lib/python3.5/site-packages/aioredis/pool.py�create_pool
s2		
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�|dkrtj�}||_||_||_||_||_||_||_|	|_	||_
tjd|�|_
t�|_d|_tjdtd|�d|�|_tjd|�|_d|_d|_|
|_dS)N�maxlenr�lockr)�asyncioZget_event_loop�_address�_db�	_password�_ssl�	_encoding�
_parser_class�_minsize�_create_connection_timeout�_loop�collections�deque�_pool�set�_used�
_acquiringZ	Conditionr	�_condZEvent�_close_state�
_close_waiter�_pubsub_conn�_connection_cls)�selfr r
rr
rrrrrrrr$r$r%�__init__Cs&										$		zConnectionsPool.__init__cCs+dj|jj|j|j|j|j�S)Nz#<{} [db:{}, size:[{}:{}], free:{}]>)�format�	__class__�__name__r
rr�freesize)r>r$r$r%�__repr__cszConnectionsPool.__repr__cCs|jS)zMinimum pool size.)r0)r>r$r$r%rhszConnectionsPool.minsizecCs
|jjS)zMaximum pool size.)r5r')r>r$r$r%rmszConnectionsPool.maxsizecCs|jt|j�|jS)zCurrent pool size.)rC�lenr7r8)r>r$r$r%�sizerszConnectionsPool.sizecCs
t|j�S)z#Current number of free connections.)rEr5)r>r$r$r%rCwszConnectionsPool.freesizecCs|jS)N)r*)r>r$r$r%r |szConnectionsPool.addressc	�s)|jIdH�|j�IdHWdQRXdS)zPClear pool connections.

        Close and remove all free connections.
        N)r9�	_do_clear)r>r$r$r%�clear�szConnectionsPool.clearc�sag}x9|jrA|jj�}|j�|j|j��q	Wtj|d|j�IdHdS)Nr)r5�popleftr�appendrr)�gatherr2)r>�waiters�connr$r$r%rG�s
zConnectionsPool._do_clearc�s�|jj�IdH|jIdH��g}x9|jrb|jj�}|j�|j|j��q*Wx.|jD]#}|j�|j|j��qmWt	j
|d|j�IdHtj
dt|��WdQRXdS)NrzClosed %d connection(s))r:�waitr9r5rIrrJrr7r)rKr2r�debugrE)r>rLrMr$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)r:�is_setr)�
ensure_futurerPr2r;r6)r>r$r$r%r�szConnectionsPool.closecCs
|jj�S)zTrue if pool is closed.)r:rQ)r>r$r$r%�closed�szConnectionsPool.closedc�s4|jj�IdHtj|jd|j�IdHdS)zWait until pool gets closed.Nr)r:rNr)Zshieldr;r2)r>r$r$r%r�szConnectionsPool.wait_closedcCs
|jpdS)zCurrently selected db index.r)r+)r>r$r$r%r
�szConnectionsPool.dbcCs|jS)zCurrent set codec or None.)r.)r>r$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)r>�command�args�kwrMr �fut�coror$r$r%rU�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)rT�execute_pubsub�_wait_execute_pubsub)r>rXZchannelsrMr r$r$r%r]�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�striprr<rSr �rangerCr5�rotate�	in_pubsub�remover7�addr*)r>rXrYZ	is_pubsub�irMr$r$r%rT�s&	
			zConnectionsPool.get_connectioncGs|S)zjHook to check result or catch exception (like MovedError).

        This method can be coroutine.
        r$)r>r[�datar$r$r%rV�szConnectionsPool._check_resultc�sH|j||�IdH}z|j|||�IdHSWd|j|�XdS)z'Acquire connection and execute command.N)�acquirerU�release)r>r rXrYrZrMr$r$r%rWszConnectionsPool._wait_executec�s�|jrtd��|jIdH�s|jr9td��|jdksT|jjrq|j|�IdH}||_|j}|j|||�IdHSWdQRXdS)NzPool is closed)rSrr9r<�_create_new_connectionr])r>r rXrYrZrMr$r$r%r^s				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)r9rarCr5�selectr+)r>r
�resrfr$r$r%rks%zConnectionsPool.selectc
�sV||_|jIdH�8x0t|j�D]}|j|j|�IdHq(WWdQRXdS)N)r,r9rarCr5�auth)r>rrfr$r$r%rm&s	zConnectionsPool.authcCs$|jr |jjr |jjSdS)Nr)r<rSrc)r>r$r$r%rc,s
zConnectionsPool.in_pubsubcCs-|jr |jjr |jjStji�S)N)r<rS�pubsub_channels�types�MappingProxyType)r>r$r$r%rn2s
zConnectionsPool.pubsub_channelscCs-|jr |jjr |jjStji�S)N)r<rS�pubsub_patternsrorp)r>r$r$r%rq8s
zConnectionsPool.pubsub_patternsc
�s�|jrtd��|jIdH�t|jr9td��xW|jdd�IdH|jr}|jj�}|jj|�|S|jj	�IdHq<WWdQRXdS)zYAcquires a connection from free pool.

        Creates new connection if needed.
        zPool is closedNrT)
rSrr9rrCr5rIr7rerN)r>rXrYrMr$r$r%rh>s			zConnectionsPool.acquirecCs|jj|�|js�|jr?tjd|�|j�n�|jretjd|�|j�n}|jr�tjd|�|j�nW|j	|j	kr�|j
r�|j|j
kr�|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,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)r7rdrSZin_transactionrZwarningrrcZ_waitersr
rrCr5rJr)rR�_wakeupr2)r>rMr$r$r%riSs(			

	

	



zConnectionsPool.releasecCsTxMt|j�D]<}|jd}|jr<|jj�q|jjd�qWdS)Nrr)rarCr5rSrIrb)r>rfrMr$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)rsrFrr8rjr*rUr5rJrCr)r>rrMr$r$r%r}s&
	zConnectionsPool._fill_freecCsRt|d|jd|jd|jd|jd|jd|jd|jd|j�S)	Nr
rrr
rrrr)	rr+r,r-r.r/r1r=r2)r>r r$r$r%rj�s								z&ConnectionsPool._create_new_connectionc	�sB|jIdH�|jj�WdQRX|dk	r>|j�IdHdS)N)r9Znotifyr)r>Zclosing_connr$r$r%rr�szConnectionsPool._wakeupcCstd��dS)Nz6'await' should be used as a context manager expression)�RuntimeError)r>r$r$r%�	__enter__�szConnectionsPool.__enter__cGsdS)Nr$)r>rYr$r$r%�__exit__�szConnectionsPool.__exit__ccs$|j�j�EdH}t||�S)N)rh�	__await__�_ConnectionContextManager)r>rMr$r$r%rw�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)r>r$r$r%�get�szConnectionsPool.get))rB�
__module__�__qualname__�__doc__r?rD�propertyrrrFrCr rHrGrPrrSrr
r
rUr]rTrVrWr^rkrmrcrnrqrhrirsrrjrrrurvrwrzr$r$r$r%r@sR
"rc@s:eZdZd
Zdd�Zdd�Zdd�Zd	S)rxr5�_conncCs||_||_dS)N)r5r)r>r#rMr$r$r%r?�s	z"_ConnectionContextManager.__init__cCs|jS)N)r)r>r$r$r%ru�sz#_ConnectionContextManager.__enter__cCs1z|jj|j�Wdd|_d|_XdS)N)r5rir)r>�exc_type�	exc_value�tbr$r$r%rv�s	z"_ConnectionContextManager.__exit__N)r5r)rBr{r|�	__slots__r?rurvr$r$r$r%rx�srxc@s:eZdZd
Zdd�Zdd�Zdd�Zd	S)ryr5rcCs||_d|_dS)N)r5r)r>r#r$r$r%r?�s	z'_AsyncConnectionContextManager.__init__c�s$|jj�IdH}||_|jS)N)r5rhr)r>rMr$r$r%�
__aenter__�s	z)_AsyncConnectionContextManager.__aenter__c�s1z|jj|j�Wdd|_d|_XdS)N)r5rir)r>r�r�r�r$r$r%�	__aexit__�s	z(_AsyncConnectionContextManager.__aexit__N)r5r)rBr{r|r�r?r�r�r$r$r$r%ry�sry)r)r3roZ
connectionrr�logr�utilr�errorsr�abcrZlocksr	r&rrxryr$r$r$r%�<module>s 0��