o
    $i(                     @   s   d dl Z d dlmZ d dlmZmZmZmZ d dlZd dlm	Z
 d dlZd dlmZ d dlmZ 			dded	eeeeef   d
edeej fddZ				ddee ded	eeeeef   d
efddZdS )    N)futures)AnyOptionalSequenceTuple)aio)authentication_utils)load_certs_from_envFaddressoptionsasynchronouscredentialsc                 C   s0  |rt nt}|p	g }t|}|dtj |d< |dtj |d< | }g }t	
 rEddlm}m} |r?||  n||  |durQ|j}	| |f}
n'tjdd dv rrt \}}}tj|||d	}|j}	| |f}
n|j}	| f}
|r|	|
||d
}|S |	|
d|i}|rtj|g|R  }|S )a#  Create a gRPC channel with authentication interceptors if token auth is enabled.

    This function handles:
    - TLS configuration via RAY_USE_TLS environment variable or custom credentials
    - Authentication interceptors when token auth is enabled
    - Keepalive settings from Ray config
    - Both synchronous and asynchronous channels

    Args:
        address: The gRPC server address (host:port)
        options: Optional gRPC channel options as sequence of (key, value) tuples
        asynchronous: If True, create async channel; otherwise sync
        credentials: Optional custom gRPC credentials for TLS. If provided, takes
            precedence over RAY_USE_TLS environment variable.

    Returns:
        grpc.Channel or grpc.aio.Channel: Configured gRPC channel with interceptors
    zgrpc.keepalive_time_mszgrpc.keepalive_timeout_msr   )+SyncAuthenticationMetadataClientInterceptorget_async_auth_interceptorsNRAY_USE_TLS0)1true)certificate_chainprivate_keyroot_certificates)r   interceptorsr   )aiogrpcgrpcdictgetray_configgrpc_client_keepalive_time_ms grpc_client_keepalive_timeout_msitemsr   is_token_auth_enabledBray._private.authentication.grpc_authentication_client_interceptorr   r   extendappendsecure_channelosenvironlowerr	   ssl_channel_credentialsinsecure_channelintercept_channel)r
   r   r   r   grpc_moduleoptions_dictr   r   r   channel_creator	base_argsserver_cert_chainr   ca_certtls_credentialschannel r4   T/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/_private/grpc_utils.pyinit_grpc_channel   sN   



r6   grpc_servermax_workersthread_name_prefixc           
      C   s   |rt nt}g }t r'|rddlm} ||  nddlm} ||  |r6|j|r/|nd|d}|S | du r>t	dt
j| |d}	|j|	|rL|nd|d}|S )a  Create a gRPC server with authentication interceptors if token auth is enabled.

    This function handles:
    - Authentication interceptors when token auth is enabled
    - Both synchronous and asynchronous servers
    - Thread pool configuration for sync servers

    Args:
        max_workers: Max thread pool workers (required for sync, ignored for async)
        thread_name_prefix: Thread name prefix for sync thread pool
        options: Optional gRPC server options as sequence of (key, value) tuples
        asynchronous: If True, create async server; otherwise sync

    Returns:
        grpc.Server or grpc.aio.Server: Configured gRPC server with interceptors
    r   )$AsyncAuthenticationServerInterceptor)#SyncAuthenticationServerInterceptorN)r   r   z4max_workers is required for synchronous gRPC servers)r8   r9   )r   r   r   r!   Bray._private.authentication.grpc_authentication_server_interceptorr:   r$   r;   server
ValueErrorr   ThreadPoolExecutor)
r8   r9   r   r   r,   r   r:   r;   r=   executorr4   r4   r5   $create_grpc_server_with_interceptors_   s4   

rA   )NFN)Nr7   NF)r&   
concurrentr   typingr   r   r   r   r   r   r   r   ray._private.authenticationr   ray._private.tls_utilsr	   strboolChannelCredentialsr6   intrA   r4   r4   r4   r5   <module>   sB    
S