Ë
    tPêj;  ã                   óX   — d Z ddlZddlmZmZ ddlmZmZmZm	Z	  G d„ d«      Z
defd„Zy)	zÊ
Tracks the number of HTTP requests currently in-flight on this uvicorn worker.

Used by /health/backlog to expose per-pod queue depth, and emitted as the
Prometheus gauge `litellm_in_flight_requests`.
é    N)ÚAnyÚFinal)ÚASGIAppÚReceiveÚScopeÚSendc                   óœ   — e Zd ZU dZdZeed<   dZedz  ed<   dZ	e
ed<   ded	dfd
„Zdededed	dfd„Zed	efd„«       Zed	edz  fd„«       Zy)ÚInFlightRequestsMiddlewareu‰  
    ASGI middleware that increments a counter when a request arrives and
    decrements it when the response is sent (or an error occurs).

    The counter is class-level and therefore scoped to a single uvicorn worker
    process â€” exactly the per-pod granularity we want.

    Also updates the `litellm_in_flight_requests` Prometheus gauge if
    prometheus_client is installed. The gauge is lazily initialised on the
    first request so that PROMETHEUS_MULTIPROC_DIR is already set by the time
    we register the metric. Initialisation is attempted only once â€” if
    prometheus_client is absent the class remembers and never retries.
    r   Ú
_in_flightNÚ_gaugeFÚ_gauge_init_attemptedÚappÚreturnc                 ó   — || _         y )N)r   )Úselfr   s     ú{/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/proxy/middleware/in_flight_requests_middleware.pyÚ__init__z#InFlightRequestsMiddleware.__init__!   s	   € Øˆ�ó    ÚscopeÚreceiveÚsendc              ƒ   óÌ  K  — |d   dk7  r| j                  |||«      ƒ d {  –—†  y t        xj                  dz  c_        t        j                  «       }|�|j	                  «        	 | j                  |||«      ƒ d {  –—†  t        xj                  dz  c_        |�|j                  «        y y 7 Œ�7 Œ3# t        xj                  dz  c_        |�|j                  «        w w xY w­w)NÚtypeÚhttpé   )r   r
   r   Ú
_get_gaugeÚincÚdec)r   r   r   r   Úgauges        r   Ú__call__z#InFlightRequestsMiddleware.__call__$   sÊ   è ø€ Ø�‰=˜FÒ"Ø—(‘(˜5 '¨4Ó0×0Ð0Øä"×-Ò-°Ñ2Õ-Ü1×<Ñ<Ó>ˆØÐØ�I‰IŒKð	Ø—(‘(˜5 '¨4Ó0×0Ð0ä&×1Ò1°QÑ6Õ1ØÐ Ø—	‘	•ð !ð 1øð 1ùä&×1Ò1°QÑ6Õ1ØÐ Ø—	‘	•ð !üs:   ‚C$¡B/¢AC$Á'B3 Á=B1Á>B3 Â.C$Â1B3 Â3.C!Ã!C$c                  ó"   — t         j                  S )z7Return the number of HTTP requests currently in-flight.)r
   r   © r   r   Ú	get_countz$InFlightRequestsMiddleware.get_count4   s   € ô *×4Ñ4Ð4r   c                  ój  — t         j                  rt         j                  S dt         _        	 ddlm}  dt
        j                  v r$ | ddd¬«      t         _        t         j                  S  | dd«      t         _        	 t         j                  S # t        $ r d t         _        Y t         j                  S w xY w)	NTr   ©ÚGaugeÚPROMETHEUS_MULTIPROC_DIRÚlitellm_in_flight_requestszBNumber of HTTP requests currently in-flight on this uvicorn workerÚlivesum)Úmultiprocess_mode)r
   r   r   Úprometheus_clientr&   ÚosÚenvironÚ	Exceptionr%   s    r   r   z%InFlightRequestsMiddleware._get_gauge9   s£   € ä%×;Ò;Ü-×4Ñ4Ð4Ø;?Ô"Ô8ð	5Ý/à)¬R¯Z©ZÑ7á49Ø0ØXØ&/ô5Ô*Ô1ô *×0Ñ0Ð0ñ 5:Ø0ØXó5Ô*Õ1ô *×0Ñ0Ð0øô ò 	5Ø04Ô&Õ-Ü)×0Ñ0Ð0ð	5ús   ­,B Á)B ÂB2Â1B2)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚintÚ__annotations__r   r   r   Úboolr   r   r   r   r   r    Ústaticmethodr#   r   r"   r   r   r
   r
      sš   … ñð €J�ÓØ€FˆC�$‰JÓØ"'Ð˜4Ó'ð˜Gð ¨ó ð Eð °Gð À4ð ÈDó ð  ð5�sò 5ó ð5ð ð1˜˜d™
ò 1ó ñ1r   r
   r   c                  ó*   — t         j                  «       S )zFModule-level convenience wrapper used by the /health/backlog endpoint.)r
   r#   r"   r   r   Úget_in_flight_requestsr8   R   s   € ä%×/Ñ/Ó1Ð1r   )r2   r,   Útypingr   r   Ústarlette.typesr   r   r   r   r
   r3   r8   r"   r   r   ú<module>r;      s1   ðñó 
ß ç 9Ó 9÷A1ñ A1ðH2 ô 2r   