U
    ]                     @   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dZdeeeje	 ddd	Z
e	dd
ddZe	dd
ddZddddZeje	 dddZed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.
    Nreturnc                 C   s   i | _ i | _d S N)all	by_originself r   ~/var/www/html/staging.mfahmagazine.net/magazine_api/magazine_env/lib/python3.8/site-packages/httpx/dispatch/connection_pool.py__init__"   s    zConnectionStore.__init__F)origin
http2_onlyr   c                 C   sf   z| j | }W n tk
r$   Y d S X ttt| }|rH|jsHd S ||= |sZ| 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_originr&   r   c                 C   sH   d| j |< zd| j|j |< W n$ tk
rB   |di| j|j< Y nX d S )Ng        )r   r   r   r    r   r&   r   r   r   add9   s
    
zConnectionStore.addc                 C   s0   | j |= | j|j |= | j|j s,| j|j= 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__)F)__name__
__module____qualname____doc__r   r   booltypingOptionalr   r'   r*   r+   r,   Iteratorr.   intr0   r   r   r   r   r      s     r   c                	   @   s   e Zd Zdddeedddeeeee	e
edddZeedddZdeeeeed	d
dZeedddZeddddZddddZeeje dddZdS )ConnectionPoolTN)verifycert	trust_envtimeoutpool_limitshttp_versionsbackendc                C   s`   || _ || _|| _|| _|| _d| _|| _t | _t | _	|d krHt
 n|| _| j|| _d S )NF)r;   r<   r>   r?   r@   	is_closedr=   r   keepalive_connectionsactive_connectionsr   rA   Zget_semaphoremax_connections)r   r;   r<   r=   r>   r?   r@   rA   r   r   r   r   R   s    zConnectionPool.__init__r   c                 C   s   t | jt | j S r   )r/   rC   rD   r   r   r   r   num_connectionsk   s    zConnectionPool.num_connections)requestr;   r<   r>   r   c              
      st   | j |jjdI d H }z|j||||dI d H }W n< tk
rn } z| j| | j  |W 5 d }~X Y nX |S )N)r   )r;   r<   r>   )	acquire_connectionurlr   sendBaseExceptionrD   r+   rE   release)r   rG   r;   r<   r>   r&   responseexcr   r   r   rJ   o   s       
zConnectionPool.send)r   r   c              
      s   t d| | |}|d krj| 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@   rA   Zrelease_funcr=   znew_connection connection=zreuse_connection connection=)loggerdebugpop_connectionrE   acquirer   r;   r<   r>   r@   rA   release_connectionr=   rD   r*   r   r   r&   r   r   r   rH      s$    

z!ConnectionPool.acquire_connectionr(   c                    s   t d| |jr.| j| | j  nX| jjd k	rn| j	| jjkrn| j| | j  |
 I d H  n| j| | j| d S )Nzrelease_connection connection=)rO   rP   rB   rD   r+   rE   rL   r?   Z
soft_limitrF   closerC   r*   r)   r   r   r   rS      s    

z!ConnectionPool.release_connectionc                    s6   d| _ t| j}| j  |D ]}| I d H  qd S )NT)rB   r#   rC   r,   rU   )r   r%   r&   r   r   r   rU      s
    

zConnectionPool.closec                 C   sF   | j j|dd}|d kr$| j|}|d k	rB| rB| j  d }|S )NT)r   )rD   r'   rC   Zis_connection_droppedrE   rL   rT   r   r   r   rQ      s    
zConnectionPool.pop_connection)NNN)r1   r2   r3   r   r   r   r   r5   r
   r	   r   r   r   propertyr9   rF   r   r   rJ   r   r   rH   rS   rU   r6   r7   rQ   r   r   r   r   r:   Q   s@      r:   )r6   Zconcurrency.asyncior   Zconcurrency.baser   configr   r   r   r   r	   r
   r   Zmodelsr   r   r   utilsr   baser   r&   r   DictZListZCONNECTIONS_DICTr1   rO   r   r:   r   r   r   r   <module>   s   $	8