
    %`i(                        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Z	 	 	 	 ddee         ded	eeeeef                           d
efdZdS )    N)futures)AnyOptionalSequenceTuple)aio)authentication_utils)load_certs_from_envFaddressoptionsasynchronouscredentialsc                 N   |rt           nt          }|pg }t          |          }|                    dt          j                                                  |d<   |                    dt          j                                                  |d<   |                                }g }t          j
                    rEddlm}m} |r|                     |                       n|                     |                       ||j        }	| |f}
nst"          j                            dd                                          dv r5t)                      \  }}}t          j        |||	          }|j        }	| |f}
n
|j        }	| f}
|r
 |	|
||d
}n |	|
d|i}|rt          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channels                   k/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/_private/grpc_utils.pyinit_grpc_channelr7      s%   0 *3''tKmG==L-9-=-= #+"K"K"M"M. .L)* 1=0@0@#S[%Q%Q%S%S1 1L,-   ""G L133 	O	
 	
 	
 	
 	
 	
 	
 	

  	O ; ; = =>>>> K K M MNNN %4k*			s	+	+	1	1	3	3}	D	D2E2G2G/;6/#%
 
 

 &4o.		 &6J	  E!/l
 
 
 "/9>g>> 	E,WD|DDDGN    grpc_servermax_workersthread_name_prefixc                    |rt           nt          }g }t          j                    rI|r$ddlm} |                     |                       n#ddlm} |                     |                       |r|                    |r|nd|          }nC| t          d          t          j        | |          }	|                    |	|r|n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)r:   r;   )r   r   r	   r#   Bray._private.authentication.grpc_authentication_server_interceptorr=   r&   r>   server
ValueErrorr   ThreadPoolExecutor)
r:   r;   r   r   r.   r   r=   r>   r@   executors
             r6   $create_grpc_server_with_interceptorsrD   _   sQ   , *3''tK L133 G 	G       D D F FGGGG       C C E EFFF  
##)5?4 $ 
 

 STTT-#1
 
 
 ##)5?4 $ 
 
 Mr8   )NFN)Nr9   NF)r(   
concurrentr   typingr   r   r   r   r   r   r   r   ray._private.authenticationr	   ray._private.tls_utilsr
   strboolChannelCredentialsr7   intrD    r8   r6   <module>rN      sc   				       1 1 1 1 1 1 1 1 1 1 1 1        



 < < < < < < 6 6 6 6 6 6
 4859	O OOhuS#X/0O O $12	O O O Of "&+37	< <#<< huS#X/0< 	< < < < < <r8   