
    >i                    v    d Z ddlm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 dd	lmZ d
 Z G d d      Zy)z%Generic resource pool implementation.    )annotationsN)nullcontext)Empty	LifoQueue   )
exceptions)register_after_fork)lazyc                D    	 | j                          y # t        $ r Y y w xY wN)force_close_all	Exception)resources    j/var/www/html/wine-match-dev/backend/winematch-backend/venv/lib/python3.12/site-packages/kombu/resource.py_after_fork_cleanup_resourcer      s%      " s    	c                     e Zd ZdZej
                  ZdZddZd Zd Z	ddZ
d Zd	 Zd
 Zd Zd Zd ZddZddZddZed        Zej,                  d        Zej0                  j3                  d      re
ZeZdZd Z
d Zyy)ResourcezPool of resources.FNc                    || _         |xs d| _        d| _        ||n| j                  | _        t	               | _        t               | _        | j                  rt        t        | t               | j                          y )Nr   F)_limitpreload_closedclose_after_forkr   	_resourceset_dirtyr	   r   setup)selflimitr   r   s       r   __init__zResource.__init__   sp    |!  + 151F1F 	
 #e  %8%D&BC

    c                    t        d      )Nzsubclass responsibility)NotImplementedErrorr   s    r   r   zResource.setup+   s    !";<<r    c                    | j                   r=t        | j                        | j                   k\  r| j                  | j                         | j                  j                  | j                                y r   )r   lenr   LimitExceededr   
put_nowaitnewr#   s    r   _add_when_emptyzResource._add_when_empty.   sL    ::#dkk*djj8$$TZZ00 	!!$((*-r    c                     j                   rt        d       j                  rM	 	  j                  j	                  ||      	  j                         j                  j                         n j                   j                                fd}|_
        S # t        $ r? t        t              r j                  j                           j                          w xY w# t        $ r  j                          Y nw xY w)a  Acquire resource.

        Arguments:
        ---------
            block (bool): If the limit is exceeded,
                then block until there is an available item.
            timeout (float): Timeout to wait
                if ``block`` is true.  Default is :const:`None` (forever).

        Raises
        ------
            LimitExceeded: if block is false and the limit has been exceeded.
        zAcquire on closed pool)blocktimeoutc                 (    j                          y)a'  Release resource so it can be used by another thread.

            Warnings:
            --------
                The caller is responsible for discarding the object,
                and to never use the resource again.  A new resource must
                be acquired if so needed.
            N)release)Rr   s   r   r.   z!Resource.acquire.<locals>.release\   s     LLOr    )r   RuntimeErrorr   r   getprepareBaseException
isinstancer
   r'   r.   r   addr   r)   r(   )r   r+   r,   r.   r/   s   `   @r   acquirezResource.acquire6   s     <<788::***HA	 LLO KKOOA&TXXZ(A		 	3 ) %a. NN55a8  !LLO  +((*+ s   C, B! !AC),DDc                    |S r    r   r   s     r   r2   zResource.preparej   s    r    c                $    |j                          y r   )closer9   s     r   close_resourcezResource.close_resourcem   s    r    c                     y r   r8   r9   s     r   release_resourcezResource.release_resourcep       r    c                t    | j                   r| j                  j                  |       | j                  |       y)zqReplace existing resource with a new instance.

        This can be used in case of defective resources.
        N)r   r   discardr<   r9   s     r   replacezResource.replaces   s+    
 ::KK)H%r    c                    | j                   rH| j                  j                  |       | j                  j	                  |       | j                  |       y | j                  |       y r   )r   r   rA   r   r'   r>   r<   r9   s     r   r.   zResource.release|   sI    ::KK)NN%%h/!!(+)r    c                     y r   r8   r9   s     r   collect_resourcezResource.collect_resource   r?   r    c                ~   | j                   ry|| _         | j                  }| j                  }	 	 |j                         }	 | j                  |       $# t        $ r Y nw xY w# t        $ r Y w xY w	 	 |j                  j                         }n# t        $ r Y yw xY w	 | j                  |       n# t        $ r Y nw xY wN)aa  Close and remove all resources in the pool (also those in use).

        Used to close resources from parent processes after fork
        (e.g. sockets/connections).

        Arguments:
        ---------
            close_pool (bool): If True (default) then the pool is marked
                as closed. In case of False the pool can be reused.
        N)	r   r   r   popKeyErrorrE   AttributeErrorqueue
IndexError)r   
close_pooldirtyr   dresress         r   r   zResource.force_close_all   s     <<!>>yy{%%d+    " nn((* %%c*!  sG   A  A! 	AA!	A-,A-2B 	BBB/ /	B;:B;c                f   | j                   }| j                  rCd|cxk  r| j                   k  r.n n+|s)|s%t        dj                  | j                   |            d}|| _         |r	 | j	                  d       | j                          ||k  r| j                  |dkD         y y # t
        $ r Y 7w xY w)Nr   z,Can't shrink pool when in use: was={} now={}TF)rL   )collect)r   r   r0   formatr   r   r   _shrink_down)r   r   forceignore_errorsreset
prev_limits         r   resizezResource.resize   s    [[
KKA33]"BIIU,- - E$$$6 	

:eai0   s   &B$ $	B0/B0c                    | j                   }t        |dt                     5  t        |j                        rt        |j                        t        | j
                        z   | j                  kD  r{|j                  j                         }|r| j                  |       t        |j                        r9t        |j                        t        | j
                        z   | j                  kD  r{d d d        y # 1 sw Y   y xY w)Nmutex)	r   getattrr   r%   rJ   r   r   rG   rE   )r   rQ   r   r/   s       r   rS   zResource._shrink_down   s    >> Xw6 	-hnn%3x~~+>T[[AQ+QUYU_U_*_NN&&())!, hnn%3x~~+>T[[AQ+QUYU_U_*_	- 	- 	-s   CC44C=c                    | j                   S r   )r   r#   s    r   r   zResource.limit   s    {{r    c                &    | j                  |       y r   )rX   )r   r   s     r   r   zResource.limit   s    Er    KOMBU_DEBUG_POOLr   c                x   dd l }| j                  dz   x}| _        t        d| d| j                  j                           | j
                  |i |}||_        t        d| d| j                  j                          t        |d      sg |_        |j                  j                  |j                                |S )Nr   r   +z	 ACQUIRE -acquired_by)	traceback_next_resource_idprint	__class____name___orig_acquire_resource_idhasattrrb   appendformat_stack)r   argskwargsrc   idrs         r   r6   zResource.acquire   s    *.*@*@1*DDB'AbT4>>#:#:";<="""D3F3AANAbT4>>#:#:";<=1m, "MM  !7!7!9:Hr    c                    |j                   }t        d| d| j                  j                          | j	                  |      }t        d| d| j                  j                          | xj
                  dz  c_        |S )Nr`   z	 RELEASE ra   r   )ri   re   rf   rg   _orig_releaserd   )r   r   ro   rp   s       r   r.   zResource.release   ss    &&BAbT4>>#:#:";<=""8,AAbT4>>#:#:";<=""a'"Hr    )NNN)FN)T)FFF)rg   
__module____qualname____doc__r   r&   r   r   r   r)   r6   r2   r<   r>   rB   r.   rE   r   rX   rS   propertyr   setterosenvironr1   rh   rr   rd   r8   r    r   r   r      s    ,,M=.2h&*!F1$	-   \\  
zz~~()
		% *r    r   )ru   
__future__r   rx   
contextlibr   rJ   r   r    r   utils.compatr	   utils.functionalr
   r   r   r8   r    r   <module>r      s.    + " 	 " "  - "R Rr    