
    nbi*                       U d dl mZ d dlZd dlZd dlZd dlmZ d dlmZm	Z	 d dl
mZ d dlmZmZmZ ej        r`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j        eeef         Zded<   ej        eef         Zded<   ddlmZ   ej!        de          Z"n ej!        d          Z" G d de#e          Z$ G d d          Z% G d de&          Z' G d de'ej(                  Z) G d de'ej*                  Z+ G d de'ej*                  Z,d*d#Z-e G d$ d%ej.        e"                               Z/e G d& d'ej.        e"                               Z0 G d( d)ej.        e"                   Z1dS )+    )annotationsN)deque)	dataclassfield)Enum)RLockEvent	get_ident   )ResponsePromise)HTTPConnection)ConnectionPool)PoolKey)HTTPResponseztyping.TypeAliasMappableTrafficManageableTraffic   )r   T)boundc                      e Zd ZdZdZdZdS )TrafficStater   r   r   N)__name__
__module____qualname__IDLEUSED	SATURATED     r/var/www/html/mdtn/previsions/meteo_cartes/venv/lib/python3.11/site-packages/urllib3_future/util/traffic_police.pyr   r      s        DDIIIr   r   c                  2    e Zd ZdZedd            Zd	dZdS )
ItemPlaceholderzOA dummy placeholder to be inserted for spot reservation into the TrafficPolice.returntyping.Literal[True]c                    dS )NTr   selfs    r    is_saturatedzItemPlaceholder.is_saturated*   s    tr   Nonec                    d S Nr   r&   s    r    closezItemPlaceholder.close.   s    r   N)r#   r$   r#   r)   )r   r   r   __doc__propertyr(   r,   r   r   r    r"   r"   '   sL        YY   X     r   r"   c                      e Zd ZdS )TrafficPoliceFineNr   r   r   r   r   r    r1   r1   2             r   r1   c                      e Zd ZdS )OverwhelmedTrafficNr2   r   r   r    r5   r5   5   r3   r   r5   c                      e Zd ZdS )UnavailableTrafficNr2   r   r   r    r7   r7   8   r3   r   r7   c                      e Zd ZdS )AtomicTrafficNr2   r   r   r    r9   r9   ;   r3   r   r9   manageable_traffic*ManageableTraffic | AsyncManageableTrafficr#   c                z    t          | dd          rt          j        S | j        rt          j        nt          j        S )Nr(   F)getattrr   r   is_idler   r   )r:   s    r    traffic_state_ofr?   >   s<     !>599 V%%$6$>U|  LDUUr   c                  6    e Zd ZU dZded<   ded<   dZded<   dS )	ActiveCursorz]Each Thread/Task is bound to a maximum of a single conn_or_pool. This represents the 'bound'.intobj_idr   conn_or_poolr   depthN)r   r   r   r.   __annotations__rE   r   r   r    rA   rA   G   s5         ggKKKOOOENNNNNNr   rA   c                      e Zd ZU dZ ee          Zded<    ee          Z	ded<   dZ
ded	<   dZded
<   ej        ej        ej        fZded<   dS )PendingSignalzRA thread may want to sleep on a specific signal to avoid consuming CPU needlessly.)default_factoryrB   owner_thread_idr	   eventNT | Nonetarget_conn_or_poolrD   ztuple[TrafficState, ...]states)r   r   r   r.   r   r
   rJ   rF   r	   rK   rM   rD   r   r   r   r   rN   r   r   r    rH   rH   P   s         \\ !5;;;O;;;;5///E//// %)(((( "L!!!! 	(F      r   rH   c                     e Zd ZdZdDdEdZ	 dFdGdZdHdZedId            ZedId            Z	dJdZ
edId            ZedId            ZdKdZdLdZdMdZdHd ZdNdOd"Zddd#dPd'ZdQd)Z	 dRdSd,Z	 	 	 	 dTdUd0Z	 dFdVd2ZdWd3Zej        	 	 	 dXdYd7            Z	 	 	 dZd[d9Zej        	 	 	 	 d\d]d;            ZdHd<ZdHd=ZdKd>ZdKd?Z d^dAZ!d_dCZ"dS )`TrafficPolicea  Thread-safe extended-Queue implementation.

    This class is made to enforce the 'I will have order!' psychopath philosophy.
    Rational: Recent HTTP protocols handle concurrent streams, therefore it is
    not as flat as before, we need to answer the following problems:

    1) we cannot just dispose of the oldest connection/pool, it may have a pending response in it.
    2) we need a map, a dumb and simple GPS, to avoid wasting CPU resources searching for a response (promise resolution).
       - that would permit doing read(x) on one response and then read(x) on another without compromising the concerned
         connection.
       - multiplexed protocols can permit temporary locking of a single connection even if response isn't consumed
         (instead of locking through entire lifecycle single request).

    This program is (very) complex and need, for patches, at least both unit tests and integration tests passing.
    NFmaxsize
int | Noneconcurrencyboolc                    || _         || _        i | _        i | _        i | _        t                      | _        i | _        i | _        t                      | _
        d| _        d| _        dS )z
        :param maxsize: Maximum number of items that can be contained.
        :param concurrency: Whether to allow a single item to be used across multiple threads.
            Delegating thread safety to another level.
        FN)rQ   rS   	_registry
_container_cursorr   _signals_map
_map_typesr   _lock	_shutdownparent)r'   rQ   rS   s      r    __init__zTrafficPolice.__init__x   sf     & (*(*3516 -/	57 WW
  %,0r   expect_conn_or_poolrL   rN   r   r#   PendingSignal[T]c                    t          |          }|r||_        || j                            |           n| j                            |           |S )z3A thread declare waiting upon a specific condition.)rM   )rH   rN   rY   append
appendleft)r'   r`   rN   signals       r    _register_signalzTrafficPolice._register_signal   sj      3
 
 
  	#"FM *M  ((((M$$V,,, r   r)   c                   t                      }| j                            |          }t          |j                  }d}| j        D ]A}|j        ||j        v r|} n,t          |j                  |j	        k    r||j        v r|} nB||j        | j
        |j	        <   dS | j                            |           |j        |_        t          |j	        |j                  | j        |j        <   |j                                         dS )zpWe either transfer our cursor (active conn_or_pool) to some sleeping thread, or we reinsert it in the container.N)rC   rD   )r
   rX   popr?   rD   rY   rM   rN   idrC   rW   removerA   rJ   rK   set)r'   current_keyactive_cursorcursor_statenext_signalpending_signals         r     _next_signal_or_container_insertz.TrafficPolice._next_signal_or_container_insert   s%   kk((55'(BCC/3"m 	 	N19>#888"0KE 9
 ~9::m>RRR$(==="0KE4A4NDOM01F[)))#0#= 4@ '&35
 5
 5
[01
 	r   c                n    | j         5  t                      | j        v cddd           S # 1 swxY w Y   dS zODetermine if the current thread hold a conn_or_pool that must be released asap.N)r\   r
   rX   r&   s    r    busyzTrafficPolice.busy   s~     Z 	/ 	/;;$,.	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/s   *..c                    t                      }| j        5  || j        v o$t          | j        |         j        t
                    cddd           S # 1 swxY w Y   dS rs   )r
   r\   rX   
isinstancerD   r"   )r'   
cursor_keys     r    busy_with_placeholderz#TrafficPolice.busy_with_placeholder   s     [[
Z 	 	- *Z(53 3	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   .AAArD   r   c                    | j         5  t                      }|| j        vr	 d d d            dS | j        |         }|j        t	          |          k    cd d d            S # 1 swxY w Y   d S NF)r\   r
   rX   rC   ri   )r'   rD   rw   rm   s       r    is_heldzTrafficPolice.is_held   s    Z 	< 	<"J--		< 	< 	< 	< 	< 	< 	< 	< !L4M '2l+;+;;	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<s   A$AA#&A#c                    | j         5  t          d | j                                        D                       cddd           S # 1 swxY w Y   dS )z>All manageable traffic is idle. No activities for all entries.c              3  L   K   | ]}t          |          t          j        u V   d S r+   )r?   r   r   .0_s     r    	<genexpr>z.TrafficPolice.bag_only_idle.<locals>.<genexpr>   sF         !##|'88     r   N)r\   allrV   valuesr&   s    r    bag_only_idlezTrafficPolice.bag_only_idle   s     Z 	 	  ..00    	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   0AA	A	c                    | j         5  | j        s	 ddd           dS t          d | j                                        D                       cddd           S # 1 swxY w Y   dS )z@All manageable traffic is saturated. No more capacity available.NFc              3  L   K   | ]}t          |          t          j        u V   d S r+   )r?   r   r   r~   s     r    r   z3TrafficPolice.bag_only_saturated.<locals>.<genexpr>   sF         !##|'==     r   )r\   rV   r   r   r&   s    r    bag_only_saturatedz TrafficPolice.bag_only_saturated   s     Z 	 	> 	 	 	 	 	 	 	 	   ..00    	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   	A/AA!ArB   c                l    | j         5  t          | j                  cd d d            S # 1 swxY w Y   d S r+   r\   lenrV   r&   s    r    __len__zTrafficPolice.__len__   {    Z 	' 	't~&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	'   )--valuec                    t          |          }|| j        vrd S g }| j                                        D ]-\  }}t          |          |k    r|                    |           .|D ]}| j        |= | j        |= d S r+   )ri   rV   rZ   itemsrc   r[   )r'   r   rC   outdated_keyskeyvals         r    
_map_clearzTrafficPolice._map_clear  s    E''F	)) 	* 	*HC#ww&  $$S)))  	% 	%C	#$$	% 	%r   traffic_typetypec                   | j                                         D ]f\  }}||u r]| j        |         }|j        r| j        5  t          |          }|| j        v r|cddd           c S 	 ddd           n# 1 swxY w Y   gdS )zRFind the first available conn or pool that is linked to at least one traffic type.N)r[   r   rZ   r>   r\   ri   rW   )r'   r   kvrD   rC   s         r    _find_byzTrafficPolice._find_by  s    O))++ 		, 		,DAqL  #y|' Z , ,--F00+, , , , , , , , , ,0, , , , , , , , , , , , , , , ts   A55A9	<A9	c                   | j         5  | j        s	 ddd           dS t                      }| j        |         }|                     |j                   |j        | j        vrt          d          | j        |j        = 	 |j        	                                 n# t          $ r Y nw xY w| j        |= g }t          | j                   }| j        D ]S}|r|                    |           |j        2t          |j                  |j        k    r|                    |           T|D ]}| j                            |           	 ddd           n# 1 swxY w Y   |D ]}|j                                         dS )zIn case there is no other way, a conn or pool may be unusable and should be destroyed.
        This make the scheduler forget about it.NzOur internal thread safety mechanism seems out of sync. This is likely a bug in urllib3-future. You may open a ticket at https://github.com/jawah/urllib3.future for support.)r\   rt   r
   rX   r   rD   rC   rV   r7   r,   	ExceptionrT   rY   rc   rM   ri   rj   rK   rk   )r'   rw   rm   signals_to_wakeedge_case_all_wakerp   s         r    kill_cursorzTrafficPolice.kill_cursor!  s'    Z *	5 *	59 *	5 *	5 *	5 *	5 *	5 *	5 *	5 *	5 #J L4MOOM6777#4>99(d  
 }34*002222    Z(
 !O%)$.%9%9!9"&- 	; 	;% #**>::: #6B>=>>-BVVV#**>:::"1 5 5$$^44445S*	5 *	5 *	5 *	5 *	5 *	5 *	5 *	5 *	5 *	5 *	5 *	5 *	5 *	5 *	5X . 	' 	'N $$&&&&	' 	's<   	EAE>BE
B%"E$B%%BEEEblockc                   d\  }}d}	 | j         5  | j        rt                      }| j        |         }t	          |j                  t          j        u rh|                     |j                   | j	        |j
        = 	 |j                                         n# t          $ r Y nw xY w| j        |= 	 ddd           dS | j	        s	 ddd           dS | j	                                        D ]/\  }}|| j        v r!t	          |          t          j        u r||}} n0|[|Y|                     |           | j	        |= | j        |= 	 |                                 n# t          $ r Y nw xY w	 ddd           dS |                     dt          j                  }ddd           n# 1 swxY w Y   |rn|j                                         t%          d| j         d          )zWhen trying to fill the bag, arriving at the maxsize, we may want to remove an item.
        This method try its best to find the most appropriate idle item and removes it.
        NNNTzCannot select a disposable connection to ease the charge. This usually means that your pool sizing is insufficient, please increase your pool maxsize appropriately. Currently set at maxsize=zG. Usually you want as much as the number of active threads/connections.)r\   rt   r
   rX   r?   rD   r   r   r   rV   rC   r,   r   r   rW   rf   rK   waitr5   rQ   )	r'   r   eligible_obj_ideligible_conn_or_poolre   rw   rm   rC   rD   s	            r    _sacrifice_first_idlez#TrafficPolice._sacrifice_first_idleS  s    2<..3	  -H -H9 !*J$(L$<M ))CDD',- - (BCCC N=+?@!)6<<>>>>( ! ! ! D! !L4'-H -H -H -H -H -H -H -H* ~ --H -H -H -H -H -H -H -H0 -1N,@,@,B,B  (FL$/11,\::l>OOOAG)>".3H3TOO$9:::78-335555$    W-H -H -H -H -H -H -H -HZ ..t\5FGG[-H -H -H -H -H -H -H -H -H -H -H -H -H -H -H^  Lg3	 j !H )-H H H
 
 	
sm   A*F";BF"
B"F"!B""F";F"A2F"EF"
E%"F"$E%%F"6 F""F&)F&)r   immediately_unavailabletraffic_indicatorsr   r   c               *   t                      }| j                                         | j        r=|                                  | j        sd| _        | j                                         dS | j        ct          | j                  | j        k    rFt          |          | j        vr0| j                                         | 
                    |           n| j                                         | j        5  t          |          }|| j        v }|r|| j        v r	 ddd           dS n
|| j        |<   |sO| j        s|| j        |<   n| j        |         }|xj        dz  c_        |j        dk    r|                                  nY|| j        vrt!          ||          | j        |<   nt#          d          | j        du rt'          |t(                    s
|| j        |<   ddd           n# 1 swxY w Y   |r|D ]}	|                     |	|           dS dS )zHRegister and/or store the conn_or_pool into the TrafficPolice container.FN)r   r   r   zGYou must release the previous connection in order to acquire a new one.T)r
   r\   acquirer]   r   rV   releaserQ   r   ri   r   rW   rt   rX   rE   rq   rA   r9   rS   rv   r"   memorize)
r'   rD   r   r   r   rw   rC   registered_conn_or_poolrm   	indicators
             r    putzTrafficPolice.put  s    [[

 > 	> '!&J   F L$DN##t|33<  66J   &&U&3333J   Z %	; %	;%%F&,&># ' 6T_,,%	; %	; %	; %	; %	; %	; %	; %	; - *6v&* ;y @ /;DOF++$(L$<M!''1,''$*a//==??? T\11/;FL/Q/QDL,,'a   #t++J /5 5+ /;DOF+K%	; %	; %	; %	; %	; %	; %	; %	; %	; %	; %	; %	; %	; %	; %	;N  	7/ 7 7	i6666	7 	77 7s   6%G)(B5G))G-0G-typing.Generator[T, None, None]c              #  h  K   | j         5  | j        rt          d          | j        rg }| j                                        D ]8\  }}t          |          t          j        ur!|                    ||f           9t                      }|D ]\  }}| j        |= || j
        vrt          ||          | j
        |<   n| j
        |         xj        dz  c_        | j        r
|| j        |<   	 |V  |                                  u# |                                  w xY wd d d            d S # 1 swxY w Y   d S )NaOne connection/pool active per thread at a given time. Call release prior to calling this method.r   )r\   rt   r9   rW   r   r?   r   r   rc   r
   rX   rA   rE   rS   r   )r'   idle_targets
cur_obj_idcur_conn_or_poolrw   rC   rD   s          r    	iter_idlezTrafficPolice.iter_idle  s     Z !	' !	'y #A  
  '4648O4I4I4K4K H H0J 0'(899ARRR  ''5E(FGGGG&[[
,8 ' '(FL/!553?3U3UZ00 Z066!;66' ?2>/'****C!	' !	' !	' !	' !	' !	' !	' !	' !	' !	' !	' !	' !	' !	' !	' !	' !	' !	's*   CD'(D,D'DD''D+.D+non_saturated_onlynot_idle_onlyc                2    |                      d||          S )NF)r   r   r   )get)r'   r   r   s      r    
get_nowaitzTrafficPolice.get_nowait  s(     xx1'  
 
 	
r   Ttimeoutfloat | Nonec                   | j         rt          d          d }d }| j        5  | j        r| j        rV| j        O| j        t          | j                  k    r2|                     t                      d|           	 d d d            d S | j        r,|rOd\  }}| j        
                                D ]%\  }}	t          |	          t          j        u r!||	}} || j        |= nm|s| j                                        \  }}nNd\  }}| j        
                                D ]%\  }}	t          |	          t          j        u r!||	}} || j        |= |l|jt!                      }
|
| j        vrt%          ||          | j        |
<   n| j        |
         xj        dz  c_        | j        r
|| j        |<   |cd d d            S |s+|                     d t          j        t          j                  }d d d            n# 1 swxY w Y   |q| j        dk    rt/          d          |U|j                            |          st/          d	| d
          |j        |                     ||||          S |j        S t/          d          )Nr   Tr   r   r   r   r   zNo connection availabler   zNo connection available within z
 second(s))r   r   r   r   )rt   r9   r\   rW   r   rQ   r   rV   r   r"   r   r?   r   r   popitemr   r
   rX   rA   rE   rS   rf   r   r7   rK   r   rD   r   )r'   r   r   r   r   rD   re   rC   r   r   rw   s              r    r   zTrafficPolice.get  s    9 	=  
 Z ;	 ;	
 O	 '+'>	 ,*<#dn"5"555HH'))04#    
  ;	 ;	 ;	 ;	 ;	 ;	 ;	 ;	  %(% 8+5(FL8<8M8M8O8O  4
$4+,<==AWWW$/9;K) OF3 ) 
8/3/F/F/H/H,/9,<@O<Q<Q<S<S " "8J(8/0@AA\EVVV (3=?OLF!!- $ 7%,*B!*J!553?3U3UZ00Z066!;66' ?2>/'i;	 ;	 ;	 ;	 ;	 ;	 ;	 ;	l  .. % % o;	 ;	 ;	 ;	 ;	 ;	 ;	 ;	 ;	 ;	 ;	 ;	 ;	 ;	 ;	z 
 |q  ()BCCC!|(((99 ,M'MMM   &.88# '+=&3	 $    ** !:;;;s   AG4D'G4;-G44G8;G8traffic_indicatorc                   | j         5  || j        du rt          d          |*t                      }| j        |         }|j        |j        }}n(t          |          |}}|| j        vr	 d d d            d S t          |t                    r"|| j        |<   t          |          | j        |<   n0t          |          }|| j        |<   t          |          | j        |<   d d d            d S # 1 swxY w Y   d S )NFz#No connection active on this thread)r\   rt   r9   r
   rX   rC   rD   ri   rV   rv   tuplerZ   r   r[   )r'   r   rD   rw   rm   rC   traffic_indicator_ids          r    r   zTrafficPolice.memorize~  s    Z 	P 	P#	U(:(:#$IJJJ#&[[
 $Z 8'4';]=W'),'7'7// #	P 	P 	P 	P 	P 	P 	P 	P& +U33 P/;	+,59:K5L5L 122')*;'<'<$2>	./8<=N8O8O 453	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	Ps   A"C+7A'C++C/2C/c                H   t          |t                    r|nt          |          }| j        5  || j        vr	 ddd           dS | j        |= | j        |= ddd           n# 1 swxY w Y   | j        .	 | j                            |           dS # t          $ r Y dS w xY wdS )zRRemove any referenced holder to map a traffic_indicator to his owner conn_or_pool.N)	rv   r   ri   r\   rZ   r[   r^   forgetr7   r'   r   r   s      r    r   zTrafficPolice.forget  s0    +U33'%&& 	 Z 	% 	%$)##	% 	% 	% 	% 	% 	% 	% 	% 	#$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% ;"""#455555%    #"s)   A"A""A&)A&5B 
BBMappableTraffic | Noneplaceholder_set0typing.Generator[typing.Callable[[T], None] | T]c              #  H   K   |                       ||d          }||V  dS g |                    |           |s  j        t                      gR d|d | j                                         dd fd
}|V  s                                  dS dS )aW  Reserve a spot into the TrafficPolice instance while you construct your conn_or_pool.

        Creating a conn_or_pool may or may not take significant time, in order
        to avoid having many thread racing for TrafficPolice insert, we must
        have a way to instantly reserve a spot meanwhile we built what
        is required.
        NF)r   r   release_on_missingTr   swappable_conn_or_poolr   r#   r)   c                    dj         5                                    j        | gR ddd d d d            d S # 1 swxY w Y   d S )NTFr   )r\   r   r   )r   r'   	swap_mader   s    r    
inner_swapz0TrafficPolice.locate_or_hold.<locals>.inner_swap  s     I    """*'  -1	                    s   '?AA)r   r   r#   r)   )locaterc   r   r"   r\   r   r   )r'   r   r   r   rD   r   r   r   s   `     @@r    locate_or_holdzTrafficPolice.locate_or_hold  s3      (;;"3#( '  L '""""(%%&7888 	DH!!#  )-	    (J   		 	 	 	 	 	 	 	  		 	r   r   c                d   d}	 | j                                          t          |t                    sRt          |t                    r|nt          |          }|| j        vrd\  }}nE| j        |         }t          |          }n(| j                                          t          d          ||~t          |t                    si| j	        
                                D ]O\  }	}
t          |
d          r:t          |
j        t                    r |
j                            |          r|
|	}} nP|||r| j                                          dS t          |t                    s| j                                          ne|                     |t"          j                  }| j                                          |j                            |          st+          d          t-                      }| j         5  | j        rJ| j        |         }|j        |k    r#|xj        dz  c_        |j        cddd           S t9          d	          || j        vr|st=          d
          nZ| j        s| j        |= || j        vrtA          ||          | j        |<   n| j        |         xj        dz  c_        |cddd           S |                     |          }ddd           n# 1 swxY w Y   |j                            |          st+          d          |j        t=          d          |j        S )zFWe want to know what conn_or_pool hold ownership of traffic_indicator.NTr   zunsupported traffic_indicatorpoolr   z<Timed out while waiting for conn_or_pool to become availabler   ^Seeking to locate a connection when having another one used, did you forget a call to release?zUnavailable connectionz}The signal was awaken without conn_or_pool assignment. This means that a connection was broken, presumably in another thread.)!r\   r   rv   r   r   ri   rZ   r   
ValueErrorrV   r   hasattrr   rP   beaconr"   rf   r   r   rK   r   TimeoutErrorr
   rt   rX   rC   rE   rD   r9   rW   r7   rS   rA   )r'   r   r   r   r   re   r   rD   rC   r_obj_idr_conn_or_poolsignal_pre_locaterw   rm   s                 r    r   zTrafficPolice.locate  s     4	J   /66 B ""3U;;/%%-..  di''+5(L&&#'9S>L--FF
""$$$ !@AAA $N"#4e<< # 150D0D0F0F " ",Hn~v66 ":&+]< < " *.556GHH "3A8&L!E#v~% )J&&(((t lO<< 
""$$$ $ 5 5l4! ! J   $*///@@ "R  e4	l [[
Z 	9 	9y 	 $Z 8 '611!''1,''(5	9 	9 	9 	9 	9 	9 	9 	9 $t   T_,, G,-EFFFG ' 0/T\11/;FL/Q/QDL,,L,22a722#1	9 	9 	9 	9 	9 	9 	9 	94 **<88F5	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	98 |   11 	N   &$Y  
 ""s   86K ;A7K ?K  K$'K$MappableTraffic | type | Nonec              #  D  K   	 t                      }|rt          |t                    r| j        5  |                     |          }|rt          |          }| j        r'| j        |         }|j        |k    rt          d          | j
        s| j        |= || j        vrt          ||          | j        |<   n| j        |         xj        dz  c_        d d d            n# 1 swxY w Y   nd|                     |||          }nK| j        r,| j        |         }|j        |j        }}|xj        dz  c_        n|                     |||          }| |t#          d          t#          d          |V  |                                  d S # |                                  w xY w)Nr   r   )r   r   )r   r   r   zDNo connection matches the traffic indicator (promise, response, ...)zNo connection are available)r
   rv   r   r\   r   ri   rt   rX   rC   r9   rS   rW   rA   rE   r   rD   r   r7   r   )	r'   r   r   r   r   rw   rD   rC   rm   s	            r    borrowzTrafficPolice.borrow[  sK     8	"J  ,/66  D D'+}}5F'G'G' D%'%5%5F#y &04Z0H#0#76#A#A*7 )I+& +& %& $(#3 <$(OF$;)==;G$*$0<" <"Z 8 8
 !%Z 8 > >! C > >/D D D D D D D D D D D D D D D4 $(;;) $/ $ $LL 9 
$(L$<M%,%2 )F "''1,'''#'88#WM $, $ $L #$0,^   ))FGGGLLNNNNNDLLNNNNs0   ,F	 B!CF	 C!!F	 $C!%BF	 	Fc                $   | j         5  | j        s	 d d d            d S t                      }| j        |         }|xj        dz  c_        |j        dk    r$| j        s|                                  n| j        |= d d d            d S # 1 swxY w Y   d S )Nr   r   )r\   rt   r
   rX   rE   rS   rq   )r'   rw   rm   s      r    r   zTrafficPolice.release  s   Z 	1 	19  		1 	1 	1 	1 	1 	1 	1 	1 #J L4M1$"a''' 199;;;;Z0	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1s   	BABB	B	c                (   g }| j         5  d| _        | j        D ]=}t          | j        |                   t          j        u r|                    |           >|D ]_}| j                            |          }	 |                                 n# t          $ r Y nw xY w| 
                    |           | j        |= `| j        r,t                      }| j        |         }|j        |v r| j        |= | j        sd| _        ddd           dS # 1 swxY w Y   dS )zShutdown traffic pool.TFN)r\   r]   rW   r?   r   r   rc   rh   r,   r   r   rV   rt   r
   rX   rC   )r'   planned_removalrC   rD   rw   rm   s         r    clearzTrafficPolice.clear  s   Z 	' 	'!DN/ 3 3#DOF$;<<@QQQ#**6222) 
+ 
+#226:: &&((((    D ---N6**y 1&[[
 $Z 8 '?::Z0 > '!&;	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	's7   A+D6B
D
BDBA"DDDc                l    | j         5  t          | j                  cd d d            S # 1 swxY w Y   d S r+   )r\   r   rW   r&   s    r    qsizezTrafficPolice.qsize  s{    Z 	( 	(t''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(r   c                l    | j         5  t          | j                  cd d d            S # 1 swxY w Y   d S r+   r   r&   s    r    rsizezTrafficPolice.rsize  r   r   MappableTraffic | typec                    t          |t                    sNt          |t                    r|nt          |          }| j        5  || j        v cddd           S # 1 swxY w Y   |                     |          duS )zVAnswer the following question: Do this PoliceTraffic know about the traffic_indicator?N)rv   r   r   ri   r\   rZ   r   r   s      r    r   zTrafficPolice.beacon  s    +T22 	( /77+!!)**   ( (di'( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( }}.//t;;s   	AA Astrc                    | j         5  | j        }| o| j        }|rd}n|rd}nd}d|                                  d| j         d| dcd d d            S # 1 swxY w Y   d S )N	SaturatedIdleUsedz<TrafficPolice /z (z)>)r\   r   r   r   rQ   )r'   r(   r>   statuss       r    __repr__zTrafficPolice.__repr__  s    Z 	O 	O2L&&=4+=G   $  NTZZ\\NNDLNNFNNN	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	Os   A AAArz   )rQ   rR   rS   rT   r+   )r`   rL   rN   r   r#   ra   r-   )r#   rT   )rD   r   r#   rT   )r#   rB   )r   r   r#   r)   )r   r   r#   rL   )F)r   rT   r#   r)   )
rD   r   r   r   r   rT   r   rT   r#   r)   )r#   r   )FF)r   rT   r   rT   r#   rL   )TNFF)
r   rT   r   r   r   rT   r   rT   r#   rL   )r   r   rD   rL   r#   r)   )r   r   r#   r)   )NFF)r   r   r   rT   r   rT   r#   r   )TNT)
r   r   r   rT   r   r   r   rT   r#   rL   )NTNF)
r   r   r   rT   r   r   r   rT   r#   r   )r   r   r#   rT   )r#   r   )#r   r   r   r.   r_   rf   rq   r/   rt   rx   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   
contextlibcontextmanagerr   r   r   r   r   r   r   r   r   r   r   r    rP   rP   g   sN         1 1 1 1 1B )-    ,"  "  "  " H / / / X/
    X	< 	< 	< 	<    X    X' ' ' '% % % %    0' 0' 0' 0'dB
 B
 B
 B
 B
P (-J7 J7 J7 J7 J7 J7X"' "' "' "'J GL
 
 
 
 
  $#(#l< l< l< l< l<^ LPP P P P P<   *  59 %	; ; ; ; ;@  $#'j# j# j# j# j#X  <@ $#? ? ? ? ?B1 1 1 1$!' !' !' !'F( ( ( (' ' ' '< < < <O O O O O Or   rP   )r:   r;   r#   r   )2
__future__r   r   queuetypingcollectionsr   dataclassesr   r   enumr   	threadingr   r	   r
   TYPE_CHECKINGbackend._baser   
connectionr   connectionpoolr   poolmanagerr   responser   Unionr   rF   r   _async.traffic_policeAsyncManageableTrafficTypeVarr   rB   r   r"   r   r1   Fullr5   Emptyr7   r9   r?   GenericrA   rH   rP   r   r   r    <module>r	     s1   " " " " " " "             ( ( ( ( ( ( ( (       - - - - - - - - - -	 //////++++++//////%%%%%%''''''(.ow.)O     +1,~~7U*VVVVVRRRRRRs"3444AAsA    3           ( ' ' ' '	 ' ' ' = < < < <*EJ < < < > = = = =*EK = = = 9 8 8 8 8%u{ 8 8 8V V V V     6>!$        FN1%   ,OO OO OO OO OOFN1% OO OO OO OO OOr   