+
    ~j                         ^ RI t ^ RIt^ RIt^ RIHtHtHt ^ RIHt ^ RI	H
t
Ht ^RIHt ^RIHt ]! R4      t ! R R	]
],          4      tR# )
    N)AsyncGenerator	AwaitableCallable)asynccontextmanager)GenericTypeVar)logger)aioTc            
         a  ] tR t^t o RtRRRRRRRRRR	/V 3R
 lR lltV 3R lR ltV 3R lR lt]V 3R lR l4       t	V 3R lR lt
V 3R lR ltV 3R lR ltV 3R lR ltV 3R lR ltV 3R lR ltV 3R lR ltR tV tR# )!ConnectionPoolzHelper class to manage persistent connections like websockets.

Handles connection pooling and reconnection after max duration.
Can be used as an async context manager to automatically return connections to the pool.
max_session_durationNmark_refreshed_on_getF
connect_cbclose_cbconnect_timeoutg      $@c                   < V ^8  d   QhRS[ R,          RS[RS[S[ .S[S[,          3,          R,          RS[S[.S[R,          3,          R,          RS[ RR/# )   r   Nr   r   r   r   return)floatboolr   r   r   )format__classdict__s   "y/Users/mitch_tango/dev/rabbit-r1-livekit/agent/.venv/lib/python3.14/site-packages/livekit/agents/utils/connection_pool.py__annotate__ConnectionPool.__annotate__   sw     !2 !2 $dl!2  $	!2
 eWil23d:!2 A3	$/047!2 !2 
!2    c                   Wn         W n        W0n        W@n        / V n        \        4       V n        WPn        \        P                  ! 4       V n
        \        4       V n        RV n        RV n        RV n        R# )a  Initialize the connection wrapper.

Args:
    max_session_duration: Maximum duration in seconds before forcing reconnection
    mark_refreshed_on_get: If True, the session will be marked as fresh when get() is called. only used when max_session_duration is set.
    connect_cb: Optional async callback to create new connections
    close_cb: Optional async callback to close connections
N        F)_max_session_duration_mark_refreshed_on_get_connect_cb	_close_cb_connectionsset
_available_connect_timeoutasyncioLock_connect_lock	_to_close_prewarm_tasklast_acquire_timelast_connection_reused)selfr   r   r   r   r   s   &$$$$$r   __init__ConnectionPool.__init__   se    " &:"&;#%!,."%% /$\\^ "%EI ),,1#r   c                &   < V ^8  d   QhRS[ RS[/# r   timeoutr   r   r   )r   r   s   "r   r   r   8   s      e  r   c                   "   V P                   f   \        R4      hV P                  V4      G Rj  xL
 p\        P                  ! 4       V P                  V&   V#  L(5i)zCreate a new connection.

Returns:
    The new connection object

Raises:
    NotImplementedError: If no connect callback was provided
Nz.Must provide connect_cb or implement connect())r"   NotImplementedErrortimer$   )r/   r4   
connections   && r   _connectConnectionPool._connect8   sS      #%&VWW++G44
(,		*% 5s   .AA)Ac                   < V ^8  d   QhRR/# r   r   N )r   r   s   "r   r   r   G   s     P Pt Pr   c                
  "   V P                   '       d7   V P                   P                  4       p V P                  V4      G Rj  xL
  KH  R#  L  \         d'   p\        P
                  ! RT 2TR7        Rp?Kx  Rp?ii ; i5i)z7Drain and close all the connections queued for closing.Nzerror closing connection: )exc_info)r+   pop_maybe_close_connection	Exceptionr	   warning)r/   connes   &  r   _drain_to_closeConnectionPool._drain_to_closeG   sn     nnn>>%%'DP224888  9 P!;D6BQOOPsE   BBA AA 	BA B A;5B;B  Bc                :   < V ^8  d   QhRS[ RS[S[R3,          /# )r   r4   r   N)r   r   r   )r   r   s   "r   r   r   Q   s$      5 ^AtG5L r   c                 "   V P                  VR7      G Rj  xL
 p V5x  V P                  V4       R#  L  \         d    T P                  T4       h i ; i5i)zoGet a connection from the pool and automatically return it when done.

Yields:
    An active connection object
r4   N)getputBaseExceptionremove)r/   r4   rE   s   &$ r   r9   ConnectionPool.connectionP   sU      XXgX..	J
 HHTN /  	KK	s"   A6A8 AAAc                &   < V ^8  d   QhRS[ RS[/# r3   r5   )r   r   s   "r   r   r   `   s      E a r   c               2  "   V P                   ;_uu_4       GRj  xL
  V P                  4       G Rj  xL
  \        P                  ! 4       pV P                  '       d   V P                  P	                  4       pV P
                  e)   W P                  V,          ,
          V P
                  8:  dC   V P                  '       d   W P                  V&   RV n        RV n	        TuuRRR4      GRj  xL
  # V P                  V4       K  \        P                  ! 4       pV P                  V4      G Rj  xL
 p\        P                  ! 4       V,
          V n        RV n	        TuuRRR4      GRj  xL
  #  ELS EL> L LH L  + GRj  xL 
 '       g   i     R# ; i5i)zeGet an available connection or create a new one if needed.

Returns:
    An active connection object
Nr   TF)r*   rG   r8   r&   rA   r    r$   r!   r-   r.   rO   perf_counterr:   )r/   r4   nowrE   t0s   &$   r   rL   ConnectionPool.get`   s$     %%%%&&((())+C ///**,..6..t448R8RR22225))$/-0D*26D/ &%%" D!""$Bw//D%)%6%6%82%=D"*/D'/ &%%( &( 0) &%%%s   FE.FE:E1*E:A!E:E:F+E4,F1<E:-E6.-E:F(E8)F1E:4F6E:8F:F	 F
F	F		Fc                $   < V ^8  d   QhRS[ RR/# r   rE   r   Nr   )r   r   s   "r   r   r      s     	& 	& 	&d 	&r   c                `    WP                   9   d   V P                  P                  V4       R# R# )zMark a connection as available for reuse.

If connection has been reset, it will not be added to the pool.

Args:
    conn: The connection to make available
N)r$   r&   addr/   rE   s   &&r   rM   ConnectionPool.put   s'     $$$OO% %r   c                $   < V ^8  d   QhRS[ RR/# rX   rY   )r   r   s   "r   r   r      s     ' '! ' 'r   c                d   "   V P                   e   V P                  V4      G Rj  xL
  R# R#  L5i)zUClose a connection if close_cb is provided.

Args:
    conn: The connection to close
N)r#   r\   s   &&r   rB   &ConnectionPool._maybe_close_connection   s+      >>%..&&& &&s   #0.	0c                $   < V ^8  d   QhRS[ RR/# rX   rY   )r   r   s   "r   r   r      s     . .1 . .r   c                    V P                   P                  V4       WP                  9   d:   V P                  P	                  V4       V P                  P                  VR4       R# R# )zRemove a specific connection from the pool.

Marks the connection to be closed during the next drain cycle.

Args:
    conn: The connection to reset
N)r&   discardr$   r+   r[   rA   r\   s   &&r   rO   ConnectionPool.remove   sO     	%$$$NNt$!!$- %r   c                   < V ^8  d   QhRR/# r=   r>   )r   r   s   "r   r   r      s        D  r   c                    \        V P                  P                  4       4       F  pV P                  P	                  V4       K   	  V P                  P                  4        V P                  P                  4        R# )ziClear all existing connections.

Marks all current connections to be closed during the next drain cycle.
N)listr$   keysr+   r[   clearr&   r\   s   & r   
invalidateConnectionPool.invalidate   sV    
 **//12DNNt$ 3!r   c                   < V ^8  d   QhRR/# r=   r>   )r   r   s   "r   r   r      s     / / /r   c                   a  S P                   f   S P                  '       d   R# R V 3R llp\        P                  ! V! 4       4      p\        P
                  ! V4      S n         R# )zInitiate prewarming of the connection pool without blocking.

This method starts a background task that creates a new connection if none exist.
The task automatically cleans itself up when the connection pool is closed.
Nc                    V ^8  d   QhRR/# r=   r>   )r   s   "r   r   ,ConnectionPool.prewarm.<locals>.__annotate__   s     	. 	.T 	.r   c                  R  <"   SP                   ;_uu_4       GR j  xL
  SP                  '       g@   SP                  SP                  R7      G R j  xL
 p SP                  P                  V 4       R R R 4      GR j  xL
  R #  Lg L3 L
  + GR j  xL 
 '       g   i     R # ; i5i)NrK   )r*   r$   r:   r'   r&   r[   )rE   r/   s    r   _prewarm_impl-ConnectionPool.prewarm.<locals>._prewarm_impl   sh     ))))(((!%t7L7L!MMDOO''- *))M *)))s\   B'BB'2B
BB
2B'=B>B'B
B'
B$	B
B$	B$		B')r,   r$   r(   create_taskweakrefref)r/   rq   tasks   f  r   prewarmConnectionPool.prewarm   sN     )T->->->	. 	. ""=?3$[[.r   c                   < V ^8  d   QhRR/# r=   r>   )r   r   s   "r   r   r      s     % %d %r   c                   "   V P                   e7   V P                  4       pV'       d   \        P                  ! V4      G Rj  xL
  V P                  4        V P	                  4       G Rj  xL
  R#  L. L5i)z@Close all connections, draining any pending connection closures.N)r,   r
   gracefully_cancelrj   rG   )r/   rv   s   & r   acloseConnectionPool.aclose   s\     )%%'D++D111""$$$ 2 	%s(   &A5A5A1'A5*A3+A53A5)r&   r#   r"   r*   r'   r$   r!   r    r,   r+   r-   r.   )__name__
__module____qualname____firstlineno____doc__r0   r:   rG   r   r9   rL   rM   rB   rO   rj   rw   r|   __static_attributes____classdictcell__)r   s   @r   r   r      s     !2 .2!2 ',	!2
 >B!2 ;?!2 "&!2 !2F P P   >	& 	&' '. .   / /$% %r   r   )r(   r8   rt   collections.abcr   r   r   
contextlibr   typingr   r   logr	    r
   r   r   r>   r   r   <module>r      s;       ? ? * #  CLv%WQZ v%r   