U
    Hi                     @   s<   d Z ddlZddlmZ ddlmZ G dd dZe ZdS )zy
ElevenLabs Connection Limiter
Limits concurrent ElevenLabs WebSocket connections to prevent exceeding workspace limits.
    N)Optional)Logc                       sz   e Zd ZU dZdZed  ed< e Z	 fddZ
deeeddd	Zedd
ddZedddZedddZ  ZS )ElevenLabsConnectionLimitera8  
    Singleton connection limiter for ElevenLabs WebSocket connections.
    Limits concurrent connections to prevent exceeding workspace subscription limits.
    
    Workspace limit: 6 (base) or 14 (with bursting enabled)
    Agent limit: 14 concurrent calls
    Recommended limit: 14 (matches agent limit)
    N	_instancec                    sD   | j d kr>t | | _ d| j _td| j _d| j _i | j _| j S )N   r   )	r   super__new__max_connectionsasyncio	Semaphore	semaphoreactive_connectionsconnection_map)cls	__class__ K/var/www/html/live_calls/homebook/services/elevenlabs_connection_limiter.pyr      s    
z#ElevenLabsConnectionLimiter.__new__      @)
session_idtimeoutreturnc              
      s   z\t j| j |dI dH }|rX| j4 I dH  |  jd7  _d| j|< W 5 Q I dH R X W dS  t jk
r   t	d| j
 d|  Y dS  tk
r } z td| d	|  W Y dS d}~X Y nX dS )
a!  
        Acquire a connection slot for a session.
        
        Args:
            session_id: Unique session identifier
            timeout: Maximum time to wait for a slot (seconds)
        
        Returns:
            True if slot acquired, False if timeout or limit reached
        )r   N   Tu,   ⚠️ ElevenLabs connection limit reached (z(). Timeout waiting for slot for session Fu(   ❌ Error acquiring connection slot for z: )r
   wait_forr   acquire_lockr   r   TimeoutErrorr   warningr	   	Exceptionerror)selfr   r   Zacquireder   r   r   r   !   s$    z#ElevenLabsConnectionLimiter.acquire)r   r   c              
      sR   || j krN| j  | j4 I dH   td| jd | _| j |= W 5 Q I dH R X dS )z
        Release a connection slot for a session.
        
        Args:
            session_id: Unique session identifier
        Nr   r   )r   r   releaser   maxr   )r    r   r   r   r   r"   B   s
    

z#ElevenLabsConnectionLimiter.release)r   c                 C   s   | j S )z)Get current number of active connections.)r   r    r   r   r   get_active_countP   s    z,ElevenLabsConnectionLimiter.get_active_countc                 C   s   | j | j S )z)Get number of available connection slots.)r	   r   r$   r   r   r   get_available_slotsT   s    z/ElevenLabsConnectionLimiter.get_available_slots)r   )__name__
__module____qualname____doc__r   r   __annotations__r
   Lockr   r   strfloatboolr   r"   intr%   r&   __classcell__r   r   r   r   r      s   

!r   )r*   r
   typingr   services.log_utilsr   r   elevenlabs_limiterr   r   r   r   <module>   s
   O