
    j                     \    d dl Z d dlZd dlZd dlmZmZ ej                  ded   fd       Zy)    N)IteratorCallablereturnc               #      K   	 t         j                  j                  dt        ffd}  |        t         j                  _        d t         j                  _        y# t         j                  _        w xY ww)aB  
    This patching addresses the issue of httpx.AsyncClient not working
    correctly when it's used by multiple event loops.

    The connection from connection pool created with one event loop can be tried to be used
    by the request processed via another event loop. Asyncio doesn't support
    that and the RuntimeError is raised.

    So, this context manager patches AsyncHTTPConnection class in a way that all of the
    async connections expire immediately and the runtime error is not possible.

    Related issues:
    https://github.com/comet-ml/opik/issues/1132
    https://github.com/encode/httpx/discussions/2959

    TODO: this function might probably require extra logic for handling the cases
    when there is already existing async connection pool with opened connections, but it is
    out of scope for now.
    r   c                  B    t        j                        fd       } | S )Nc                      d|d<    | i |S )Nr   keepalive_expiry )argskwargsoriginals     x/Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/evaluation/asyncio_support.pywrappedzfasync_http_connections_expire_immediately.<locals>.AsyncHTTPConnection__init__wrapper.<locals>.wrapped!   s    -.)*000    )	functoolswraps)r   r   s    r   "AsyncHTTPConnection__init__wrapperzUasync_http_connections_expire_immediately.<locals>.AsyncHTTPConnection__init__wrapper    s%    __X&1 '1 Nr   N)httpcoreAsyncHTTPConnection__init__r   )r   r   s    @r   )async_http_connections_expire_immediatelyr      s\     *9//88	H 	 1S0T$$-08$$-$$-s   A9AA 	A9A66A9)r   r   
contextlibtypingr   r   contextmanagerr   r
   r   r   <module>r      s6       % "98D> "9 "9r   