o
    h                     @   s   d dl Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
mZmZ ddlmZmZmZ ddlmZ dd	lmZ dd
lmZ e jee je f ZeeZG dd dZG dd deZdS )    N   )AsyncioBackend)ConcurrencyBackend)DEFAULT_POOL_LIMITSDEFAULT_TIMEOUT_CONFIG	CertTypesHTTPVersionTypes
PoolLimitsTimeoutTypesVerifyTypes)AsyncRequestAsyncResponseOrigin)
get_logger   )AsyncDispatcher)HTTPConnectionc                   @   s   e Zd ZdZdddZ	ddededeje	 fd	d
Z
de	ddfddZde	ddfddZdddZdeje	 fddZdefddZdS )ConnectionStorez
    We need to maintain collections of connections in a way that allows us to:

    * Lookup connections by origin.
    * Iterate over connections by insertion time.
    * Return the total number of connections.
    returnNc                 C   s   i | _ i | _d S N)all	by_originself r   f/var/www/html/magazine_api/magazine_env/lib/python3.10/site-packages/httpx/dispatch/connection_pool.py__init__"   s   
zConnectionStore.__init__Forigin
http2_onlyc                 C   sd   z| j | }W n
 ty   Y d S w ttt| }|r#|js#d S ||= |s,| j |= | j|= |S r   )r   KeyErrornextreversedlistkeysZis_http2r   )r   r   r   connections
connectionr   r   r   pop_by_origin&   s   
zConnectionStore.pop_by_originr%   c                 C   sF   d| j |< zd| j|j |< W d S  ty"   |di| j|j< Y d S w )Ng        )r   r   r   r   r   r%   r   r   r   add9   s   
zConnectionStore.addc                 C   s4   | j |= | j|j |= | j|j s| j|j= d S d S r   )r   r   r   r'   r   r   r   remove@   s
   zConnectionStore.removec                 C   s   | j   | j  d S r   )r   clearr   r   r   r   r   r*   F   s   
zConnectionStore.clearc                 C   s   t | j S r   )iterr   r#   r   r   r   r   __iter__J   s   zConnectionStore.__iter__c                 C   s
   t | jS r   )lenr   r   r   r   r   __len__M   s   
zConnectionStore.__len__r   N)F)__name__
__module____qualname____doc__r   r   booltypingOptionalr   r&   r(   r)   r*   Iteratorr,   intr.   r   r   r   r   r      s     


r   c                   @   s   e Zd Zdddeeddddedededede	d	e
d
efddZedefddZ			ddededededef
ddZdedefddZdeddfddZdddZdedeje fddZdS )ConnectionPoolTN)verifycert	trust_envtimeoutpool_limitshttp_versionsbackendr:   r;   r<   r=   r>   r?   r@   c                C   s`   || _ || _|| _|| _|| _d| _|| _t | _t | _	|d u r$t
 n|| _| j|| _d S )NF)r:   r;   r=   r>   r?   	is_closedr<   r   keepalive_connectionsactive_connectionsr   r@   Zget_semaphoremax_connections)r   r:   r;   r<   r=   r>   r?   r@   r   r   r   r   R   s   zConnectionPool.__init__r   c                 C   s   t | jt | j S r   )r-   rB   rC   r   r   r   r   num_connectionsk   s   zConnectionPool.num_connectionsrequestc              
      sj   | j |jjdI d H }z|j||||dI d H }W |S  ty4 } z| j| | j  |d }~ww )N)r   )r:   r;   r=   )	acquire_connectionurlr   sendBaseExceptionrC   r)   rD   release)r   rF   r:   r;   r=   r%   responseexcr   r   r   rI   o   s   
zConnectionPool.sendr   c              
      s   t d| | |}|d u r6| j I d H  t|| j| j| j| j	| j
| j| jd}t d| nt d| | j| |S )Nzacquire_connection origin=)r:   r;   r=   r?   r@   Zrelease_funcr<   znew_connection connection=zreuse_connection connection=)loggerdebugpop_connectionrD   acquirer   r:   r;   r=   r?   r@   release_connectionr<   rC   r(   r   r   r%   r   r   r   rG      s&   

z!ConnectionPool.acquire_connectionr%   c                    s   t d| |jr| j| | j  d S | jjd ur:| j	| jjkr:| j| | j  |
 I d H  d S | j| | j| d S )Nzrelease_connection connection=)rN   rO   rA   rC   r)   rD   rK   r>   Z
soft_limitrE   closerB   r(   r'   r   r   r   rR      s   
z!ConnectionPool.release_connectionc                    s8   d| _ t| j}| j  |D ]	}| I d H  qd S )NT)rA   r"   rB   r*   rT   )r   r$   r%   r   r   r   rT      s   

zConnectionPool.closec                 C   sF   | j j|dd}|d u r| j|}|d ur!| r!| j  d }|S )NT)r   )rC   r&   rB   is_connection_droppedrD   rK   rS   r   r   r   rP      s   
zConnectionPool.pop_connection)NNNr/   )r0   r1   r2   r   r   r   r   r4   r
   r	   r   r   r   propertyr8   rE   r   r   rI   r   r   rG   rR   rT   r5   r6   rP   r   r   r   r   r9   Q   sV    	


r9   )r5   Zconcurrency.asyncior   Zconcurrency.baser   configr   r   r   r   r	   r
   r   modelsr   r   r   utilsr   baser   r%   r   DictListZCONNECTIONS_DICTr0   rN   r   r9   r   r   r   r   <module>   s    $	8