o
    `j
                     @   s&   d dl Z d dlmZ G dd dZdS )    N)Futurec                   @   s2   e Zd ZdZdd Zdd ZdddZd	d
 ZdS )WakeupNotifiera  await wakeup(timeout_secs) when either ``timeout_secs`` elapses or
    notify() is called -- whichever first. The notifier is safe to call
    from any thread (it routes through call_soon_threadsafe).

    Level-triggered: notify() arriving while no one is awaiting is latched
    and consumed by the next ``__call__``. This closes a lost-wakeup race
    where a caller's state mutation (e.g. ``cluster._need_update = True``)
    and its ``notify()`` happen between another task's pre-await state
    check and its ``await self._wakeup(...)``. Without latching, the
    notification arrives at the IO thread before the task has registered
    a future to signal, and the task would sleep for the full timeout
    despite work being ready.

    Used by the metadata refresh loop to sleep on its TTL while remaining
    interruptible by external callers (e.g. KafkaProducer / KafkaConsumer
    invoking cluster.request_update() from another thread).
    c                 C   s   t || _d | _d| _d S )NF)weakrefproxy_net_fut_pending)selfnet r   W/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/kafka/net/wakeup_notifier.py__init__   s   
zWakeupNotifier.__init__c                 C   s,   | j d ur| j js| j d  d S d| _d S )NT)r   is_donesuccessr   r	   r   r   r   _wakeup    s   
zWakeupNotifier._wakeupNc              	      s   | j d ur
tdt | _ | jrd| _| j d  |d ur3z
| j|| j}W n ty2   Y d S w d }z| j I d H  W d | _ |d urL| j	| d S d S d | _ |d ur\| j	| w w )Nz$Concurrent access to WakeupNotifier!F)
r   RuntimeErrorr   r   r   r   
call_laterr   ReferenceErrorcancel)r	   timeout_secstimerr   r   r   __call__&   s0   
zWakeupNotifier.__call__c                 C   s*   z
| j | j W d S  ty   Y d S w N)r   call_soon_threadsafer   r   r   r   r   r   notify;   s
   zWakeupNotifier.notifyr   )__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r      s    
r   )r   kafka.futurer   r   r   r   r   r   <module>   s    