o
    Df7                     @  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   G/home/ubuntu/webapp/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 Zd(ddZd)ddZedd  Zejd!d  Zejd"rfe
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_resourcec                 C  s   | j rdS d| _ | 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)zClose and remove all resources in the pool (also those in use).

        Used to close resources from parent processes after fork
        (e.g. sockets/connections).
        NT)	r   r#   r!   popKeyErrorrB   AttributeErrorr   
IndexError)r   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}|| _ |r7z|   W n	 ty6   Y nw |   ||k rI| j|dkd d S d S )Nr   z,Can't shrink pool when in use: was={} now={}T)collect)r   r#   r3   formatr   r   r$   _shrink_down)r   r%   forceignore_errorsreset
prev_limitr   r   r   resize   s(   $zResource.resizeTc                 C  s   G dd d}| j }t|d| - t|j| jkr6|j }|r&| | t|j| jksW d    d S W d    d S 1 sAw   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   rS   rU   rV   r   r   r   __exit__   s   z,Resource._shrink_down.<locals>.Noop.__exit__N)rS   rT   rU   r   rV   r   rW   rX   )r   r   r   rR   rY   r   r   r   r   Noop   s    rZ   mutex)r!   getattrr)   r   r%   popleftrB   )r   rJ   rZ   r   r2   r   r   r   rL      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   )rQ   )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hasattrra   appendformat_stack)r   argskwargsrb   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 r`   r   )rg   rd   re   r   _orig_releaserc   )r   r   rm   rn   r   r   r   r0      s   
)NNN)FN)FFF)T)r   r   r   r   r   r*   r    r&   r$   r-   r9   r5   r=   r?   rA   r0   rB   r   rQ   rL   propertyr%   setterosenvironr4   rf   ro   rc   r   r   r   r   r   !   s8    

4	
!


r   )r   
__future__r   rr   collectionsr   r   r   r   
_LifoQueuetypingr    r   utils.compatr	   utils.functionalr
   typesr   r   r   r   r   r   r   <module>   s    