o
    f                     @  s   d Z ddlmZ ddlZddlmZ ddlmZ ddlmZ	 ddl
mZ dd	lmZ dd
lmZ ddlmZ er>ddlmZ dd ZG dd de	ZG dd dZdS )z%Generic resource pool implementation.    )annotationsN)deque)Empty)	LifoQueue)TYPE_CHECKING   )
exceptions)register_after_fork)lazy)TracebackTypec                 C  s$   z|    W d S  ty   Y d S w N)force_close_all	Exception)resource r   A/var/www/html/venv/lib/python3.10/site-packages/kombu/resource.py_after_fork_cleanup_resource   s
   r   c                   @  s   e Zd ZdZdd ZdS )r   z#Last in first out version of Queue.c                 C  s   t  | _d S r   )r   queue)selfmaxsizer   r   r   _init      zLifoQueue._initN)__name__
__module____qualname____doc__r   r   r   r   r   r      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)r   limitr   r    r   r   r   __init__(   s   

zResource.__init__c                 C  s   t d)Nzsubclass responsibility)NotImplementedErrorr   r   r   r   r$   7   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   r0   h   s   	z!Resource.acquire.<locals>.release)r   RuntimeErrorr%   r!   getr   r-   prepareBaseException
isinstancer
   r+   r0   r#   addr,   )r   r.   r/   r0   r   r1   r   acquireB   s4   


zResource.acquirec                 C  s   |S r   r   r   r   r   r   r   r5   v      zResource.preparec                 C  s   |   d S r   )closer:   r   r   r   close_resourcey   r   zResource.close_resourcec                 C     d S r   r   r:   r   r   r   release_resource|   r;   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#   discardr=   r:   r   r   r   replace   s   zResource.replacec                 C  s:   | j r| j| | j| | | d S | | d S r   )r%   r#   r@   r!   r+   r?   r=   r:   r   r   r   r0      s
   zResource.releasec                 C  r>   r   r   r:   r   r   r   collect_resource   r;   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KeyErrorrB   AttributeErrorr   
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)rG   )collect)r   r#   r3   formatr   r   r$   _shrink_down)r   r%   forceignore_errorsreset
prev_limitr   r   r   resize   s(   $zResource.resizec                 C  s   G dd d}| j }t|d| Q t|jrJt|jt| j | jkrR|j }|r0| | t|jrZt|jt| j | jks$W d    d S W d    d S W d    d S W d    d S 1 sew   Y  d S )Nc                   @  s   e Zd Zdd ZdddZdS )z#Resource._shrink_down.<locals>.Noopc                 S  r>   r   r   r(   r   r   r   	__enter__   r;   z-Resource._shrink_down.<locals>.Noop.__enter__exc_typetypeexc_valr   exc_tbr   returnNonec                 S  r>   r   r   )r   rT   rV   rW   r   r   r   __exit__   s   z,Resource._shrink_down.<locals>.Noop.__exit__N)rT   rU   rV   r   rW   r   rX   rY   )r   r   r   rS   rZ   r   r   r   r   Noop   s    r[   mutex)r!   getattrr)   r   r#   r%   popleftrB   )r   rK   r[   r   r2   r   r   r   rM      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   )rR   )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__r   _orig_acquire_resource_idhasattrrb   appendformat_stack)r   argskwargsrc   idrr   r   r   r9      s   
c                 C  sR   |j }td| d| jj  | |}td| d| jj  |  jd8  _|S )Nr`   z	 RELEASE ra   r   )rh   re   rf   r   _orig_releaserd   )r   r   rn   ro   r   r   r   r0      s   
)NNN)FN)T)FFF)r   r   r   r   r   r*   r    r&   r$   r-   r9   r5   r=   r?   rA   r0   rB   r   rR   rM   propertyr%   setterosenvironr4   rg   rp   rd   r   r   r   r   r   !   s8    

4	

&


r   )r   
__future__r   rs   collectionsr   r   r   r   
_LifoQueuetypingr    r   utils.compatr	   utils.functionalr
   typesr   r   r   r   r   r   r   <module>   s    