// Copyright 2024 RustFS Team // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #[allow(unsafe_code)] mod generated; use std::{error::Error, time::Duration}; use common::globals::GLOBAL_Conn_Map; pub use generated::*; use proto_gen::node_service::node_service_client::NodeServiceClient; use tonic::{ Request, Status, metadata::MetadataValue, service::interceptor::InterceptedService, transport::{Channel, Endpoint}, }; // Default 100 MB pub const DEFAULT_GRPC_SERVER_MESSAGE_LEN: usize = 100 * 1024 * 1024; pub async fn node_service_time_out_client( addr: &String, ) -> Result< NodeServiceClient< InterceptedService) -> Result, Status> + Send + Sync + 'static>>, >, Box, > { let token: MetadataValue<_> = "rustfs rpc".parse()?; let channel = match GLOBAL_Conn_Map.read().await.get(addr) { Some(channel) => channel.clone(), None => { let connector = Endpoint::from_shared(addr.to_string())?.connect_timeout(Duration::from_secs(60)); connector.connect().await? } }; GLOBAL_Conn_Map.write().await.insert(addr.to_string(), channel.clone()); // let timeout_channel = Timeout::new(channel, Duration::from_secs(60)); Ok(NodeServiceClient::with_interceptor( channel, Box::new(move |mut req: Request<()>| { req.metadata_mut().insert("authorization", token.clone()); Ok(req) }), )) }