o
    i                     @   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                       s   e Zd ZU dZdZed  ed< e Z	 fddZ
ddeded	efd
dZded	dfddZd	efddZd	ef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 u 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 }|r<| j4 I dH  |  jd7  _d| j|< W d  I dH  W dS 1 I dH s7w   Y  W dS  t jyU   t	d| j
 d|  Y dS  tyr } ztd| d	|  W Y d}~dS d}~ww )
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   acquireder   r   r   r   !   s4   
z#ElevenLabsConnectionLimiter.acquirec              	      st   || j v r8| j  | j4 I dH  td| jd | _| j |= W d  I dH  dS 1 I dH s1w   Y  dS 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.releasec                 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