o
    FDi9$                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d d	lmZ eeZG d
d dZdS )    N)deque)AsyncIterator)asynccontextmanagersuppress)Optional)
Connection)AsynchPoolError)	constants)
PoolStatusc                   @   s  e Zd ZejejfdedefddZd6ddZd7d	d
Z	de
fddZedefddZedefddZede
fddZedefddZedefddZedefddZedefddZedefddZd7ddZdefdd Zdee fd!d"Zdefd#d$Zd%eddfd&d'Zd(d)d*ed+eddfd,d-Zd(d)d+eddfd.d/Zede e fd0d1Z!d6d2d3Z"d7d4d5Z#dS )8Poolminsizemaxsizec                 K   s   |dk rt d|dk rt d||krt d|| _|| _|| _t|| _t | _t	|d| _
t	|d| _d| _d| _d S )N   z+maxsize is expected to be greater than zeror   z2minsize is expected to be greater or equal to zerozminsize is greater than maxsize)maxlenF)
ValueError_maxsize_minsize_connection_kwargsasyncio	Semaphore_semLock_lockr   _acquired_connections_free_connections_opened_closed)selfr   r   kwargs r   D/var/www/Datamplify/venv/lib/python3.10/site-packages/asynch/pool.py__init__   s   

zPool.__init__returnc                    s   |   I d H  | S N)startupr   r   r   r    
__aenter__'   s   zPool.__aenter__Nc                    s   |   I d H  d S r#   )shutdown)r   exc_typeexc_valexc_tbr   r   r    	__aexit__+   s   zPool.__aexit__c                 C   s>   t | j}| j}d| d| j d| j dt| dd| dS )N<z	(minsize=z
, maxsize=z) object at 0xxz
; status: >)type__name__statusr   r   id)r   cls_namer1   r   r   r    __repr__.   s   
zPool.__repr__c                 C      | j S )ziReturns True if the pool is opened.

        :returns: the pool open status
        :rtype: bool
        )r   r%   r   r   r    opened6      zPool.openedc                 C   r5   )ziReturn True if the pool is closed.

        :returns: the pool close status
        :rtype: bool
        )r   r%   r   r   r    closed@   r7   zPool.closedc                 C   sD   | j s	| js	tjS | j r| jstjS | jr| j stjS t|  d)zReturn the status of the pool.

        :raise AsynchPoolError: an unresolved pool state.
        :return: the Pool object status
        :rtype: str (PoolStatus StrEnum)
        z is in an unknown state)r   r   r
   createdr6   r8   r   r%   r   r   r    r1   J   s   	zPool.statusc                 C   
   t | jS )zReturn the number of connections acquired from the pool.

        A connection is acquired when the `pool.connection()` is invoked.

        :return: the number of connections requested from the pool
        :rtype: int
        )lenr   r%   r   r   r    acquired_connections[   s   

zPool.acquired_connectionsc                 C   r:   )zReturn the number of free connections in the pool.

        :return: the number of free connections in the pool
        :rtype: int
        )r;   r   r%   r   r   r    free_connectionsg   s   
zPool.free_connectionsc                 C   s   | j | j S )am  Return the number of connections associated with the pool.

        This number is the sum of the acquired and free connections.
        So this sum may be interpreted as the current size of the pool
        or the number of connections associated with the pool and so on.

        :return: the number of connections related to the pool
        :rtype: int
        )r<   r=   r%   r   r   r    
_pool_sizeq   s   zPool._pool_sizec                 C   r5   r#   )r   r%   r   r   r    r         zPool.maxsizec                 C   r5   r#   )r   r%   r   r   r    r      r?   zPool.minsizec              
      s   | j | jkrt|  d| j | jkrt|  dtdi | j}| I d H  z| I d H  | j| W d S  t	yS } zd| d|  }t||d }~ww )Nz is already fullz is overburdenzfailed to create a z for r   )
r>   r   r   r   r   connectpingr   appendConnectionErrorr   connemsgr   r   r    _create_connection   s   
zPool._create_connectionc                 C   s   | j s
td|  | j  S )Nzno free connection in )r   r   popleftr%   r   r   r    _pop_connection   s   
zPool._pop_connectionc                    sT   | j r(|  }tt | I d H  |W  d    S 1 s w   Y  | j sd S r#   )r   rJ   r   rC   _refreshr   rE   r   r   r    _get_fresh_connection   s   
 zPool._get_fresh_connectionc                    sJ   |   I d H  }r| j| |S |  I d H  |  }| j| |S r#   )rM   r   rB   rH   rJ   rL   r   r   r    _acquire_connection   s   zPool._acquire_connectionrE   c              
      s~   || j vrtd| d|  | j | z	| I d H  W n ty6 } zd| d}t||d }~ww | j| d S )Nzthe connection z does not belong to zthe z is invalidated)r   r   removerK   rC   r   rB   rD   r   r   r    _release_connection   s   

zPool._release_connectionFstrictnrR   c                   s   |dk rd| d  }t | j|  jkr-  d j d| d j d}t||s1d S  fdd	t|D }|r\td
d tj|ddiI d H D r^d| d  }t|d S d S )Nr   z!cannot create a negative number (z) of connections for z	 has the z connections, adding z will exceed its maxsize ()c                    s   g | ]	}t   qS r   )r   create_taskrH   ).0_r%   r   r    
<listcomp>   s    z*Pool._init_connections.<locals>.<listcomp>c                 s   s    | ]
}t |tr|V  qd S r#   )
isinstance	Exception)rV   ir   r   r    	<genexpr>   s    
z)Pool._init_connections.<locals>.<genexpr>return_exceptionsTzfailed to create the z connection(s) for the )r   r>   r   r   rangeanyr   gather)r   rS   rR   rG   tasksr   r%   r    _init_connections   s.   
zPool._init_connectionsc                   s2   | j | j  }dkr| j||dI d H  d S d S )Nr   rQ   )r   r>   rb   )r   rR   gapr   r   r    _ensure_minsize_connections   s   z Pool._ensure_minsize_connectionsc                 C  s  | j 4 I dH  | j4 I dH  |  I dH }W d  I dH  n1 I dH s(w   Y  zM|V  W | j4 I dH 6 z
| |I dH  W n ty[ } zt| W Y d}~nd}~ww | jddI dH  W d  I dH  n1 I dH suw   Y  nI| j4 I dH 6 z
| |I dH  W n ty } zt| W Y d}~nd}~ww | jddI dH  W d  I dH  w 1 I dH sw   Y  w W d  I dH  dS 1 I dH sw   Y  dS )aE  Get a connection from the pool.

        If requested more connections than the pool can provide,
        the pool gets blocked until a connection comes back.

        :raises AsynchPoolError: if a connection cannot be acquired or released

        :return: a free connection from the pool
        :rtype: Connection
        NTrQ   )r   r   rN   rP   r   loggerwarningrd   )r   rE   rF   r   r   r    
connection   s6   (( ,.zPool.connectionc              	      s   | j 4 I dH / | jr| W  d  I dH  S | j| jddI dH  d| _| jr,d| _W d  I dH  | S 1 I dH s=w   Y  | S )zInitialise the pool.

        When entering the context,
        the pool get filled with connections
        up to the pool `minsize` value.

        :return: a pool object with `minsize` opened connections
        :rtype: Pool
        NTrQ   F)r   r   rb   r   r   r%   r   r   r    r$      s   		zPool.startupc              	      s   | j 4 I dH 6 | jr| j }| I dH  | js| jr-| j }| I dH  | jsd| _d| _W d  I dH  dS 1 I dH sDw   Y  dS )zClose the pool.

        This method closes consequently free connections first.
        Then it does the same for the acquired connections.
        Then the pool is marked closed.
        NFT)r   r   rI   closer   r   r   rL   r   r   r    r'   	  s   

.zPool.shutdown)r"   r   )r"   N)$r0   
__module____qualname__r	   POOL_MIN_SIZEPOOL_MAX_SIZEintr!   r&   r+   strr4   propertyboolr6   r8   r1   r<   r=   r>   r   r   rH   r   rJ   r   rM   rN   rP   rb   rd   r   r   rg   r$   r'   r   r   r   r    r      sL    


			


r   )r   loggingcollectionsr   collections.abcr   
contextlibr   r   typingr   asynch.connectionr   asynch.errorsr   asynch.protor	   asynch.proto.models.enumsr
   	getLoggerr0   re   r   r   r   r   r    <module>   s    
