o
    wl~i                     @  sr   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
d ZG dd dZdS )z%Generic resource pool implementation.    )annotationsN)nullcontext)Empty	LifoQueue   )
exceptions)register_after_fork)lazyc                 C  s$   z|    W d S  ty   Y d S w N)force_close_all	Exception)resource r   U/var/www/newdalilibackend/backend/venv/lib/python3.10/site-packages/kombu/resource.py_after_fork_cleanup_resource   s
   r   c                   @  s   e Zd ZdZejZdZd&ddZdd Zdd	 Z	d'd
dZ
dd Zdd Zdd Zdd Zdd Zdd Zd(ddZd)ddZd(ddZedd  Zejd!d  Zejd"rge
ZeZd#Zd$d Z
d%d ZdS dS )*ResourcezPool of resources.FNc                 C  s^   || _ |pd| _d| _|d ur|n| j| _t | _t | _| jr)td ur)t| t	 | 
  d S )Nr   F)_limitpreload_closedclose_after_forkr   	_resourceset_dirtyr   r   setup)selflimitr   r   r   r   r   __init__   s   

zResource.__init__c                 C  s   t d)Nzsubclass responsibility)NotImplementedErrorr   r   r   r   r   +   s   zResource.setupc                 C  s6   | j rt| j| j kr| | j | j|   d S r
   )r   lenr   LimitExceededr   
put_nowaitnewr   r   r   r   _add_when_empty.   s   zResource._add_when_emptyc                   s   j rtdjrM	 z
jj||d W n ty"     Y n)w z  W n tyC   t	 t
r=j      w j  nqn   fdd}| _ S )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 poolr   )blocktimeoutc                     s      dS )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)releaser   Rr   r   r   r&   \   s   	z!Resource.acquire.<locals>.release)r   RuntimeErrorr   r   getr   r#   prepareBaseException
isinstancer	   r!   r&   r   addr"   )r   r$   r%   r&   r   r'   r   acquire6   s4   


zResource.acquirec                 C  s   |S r
   r   r   r   r   r   r   r+   j      zResource.preparec                 C  s   |   d S r
   )closer0   r   r   r   close_resourcem   s   zResource.close_resourcec                 C     d S r
   r   r0   r   r   r   release_resourcep   r1   zResource.release_resourcec                 C  s    | j r	| j| | | dS )zqReplace existing resource with a new instance.

        This can be used in case of defective resources.
        N)r   r   discardr3   r0   r   r   r   replaces   s   zResource.replacec                 C  s:   | j r| j| | j| | | d S | | d S r
   )r   r   r6   r   r!   r5   r3   r0   r   r   r   r&   |   s
   zResource.releasec                 C  r4   r
   r   r0   r   r   r   collect_resource   r1   zResource.collect_resourceTc                 C  s   | j rdS || _ | j}| j}	 z| }W n	 ty   Y nw z| | W n	 ty/   Y nw q	 z|j }W n
 tyC   Y dS w z| | W n	 tyT   Y nw q2)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KeyErrorr8   AttributeErrorqueue
IndexError)r   
close_pooldirtyr   dresresr   r   r   r      s:   	zResource.force_close_allc                 C  s   | j }| jr"d|  k r| j k r"n n|s"|s td| j |d}|| _ |r9z| jdd W n	 ty8   Y nw |   ||k rK| j|dkd d S d S )Nr   z,Can't shrink pool when in use: was={} now={}TF)r>   )collect)r   r   r)   formatr   r   r   _shrink_down)r   r   forceignore_errorsreset
prev_limitr   r   r   resize   s(   $zResource.resizec                 C  s   | j }t|dt Q t|jrCt|jt| j | jkrK|j }|r)| | t|jrSt|jt| j | jksW d    d S W d    d S W d    d S W d    d S 1 s^w   Y  d S )Nmutex)	r   getattrr   r   r<   r   r   r9   r8   )r   rB   r   r(   r   r   r   rD      s   $

&"zResource._shrink_downc                 C  s   | j S r
   )r   r   r   r   r   r      s   zResource.limitc                 C  s   |  | d S r
   )rI   )r   r   r   r   r   r      s   KOMBU_DEBUG_POOLr   c                 O  s   dd l }| jd  }| _td| d| jj  | j|i |}||_td| d| jj  t|ds7g |_|j	|
  |S )Nr   r   +z	 ACQUIRE -acquired_by)	traceback_next_resource_idprint	__class____name___orig_acquire_resource_idhasattrrO   appendformat_stack)r   argskwargsrP   idrr   r   r   r/      s   
c                 C  sR   |j }td| d| jj  | |}td| d| jj  |  jd8  _|S )NrM   z	 RELEASE rN   r   )rV   rR   rS   rT   _orig_releaserQ   )r   r   r\   r]   r   r   r   r&      s   
)NNN)FN)T)FFF)rT   
__module____qualname____doc__r   r    r   r   r   r#   r/   r+   r3   r5   r7   r&   r8   r   rI   rD   propertyr   setterosenvironr*   rU   r^   rQ   r   r   r   r   r      s8    

4	

#


r   )ra   
__future__r   rd   
contextlibr   r<   r   r    r   utils.compatr   utils.functionalr	   r   r   r   r   r   r   <module>   s    