rustfs_protos/
lib.rs

1// Copyright 2024 RustFS Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#[allow(unsafe_code)]
16mod generated;
17
18use std::{error::Error, time::Duration};
19
20pub use generated::*;
21use proto_gen::node_service::node_service_client::NodeServiceClient;
22use rustfs_common::globals::GLOBAL_Conn_Map;
23use tonic::{
24    Request, Status,
25    metadata::MetadataValue,
26    service::interceptor::InterceptedService,
27    transport::{Channel, Endpoint},
28};
29
30// Default 100 MB
31pub const DEFAULT_GRPC_SERVER_MESSAGE_LEN: usize = 100 * 1024 * 1024;
32
33pub async fn node_service_time_out_client(
34    addr: &String,
35) -> Result<
36    NodeServiceClient<
37        InterceptedService<Channel, Box<dyn Fn(Request<()>) -> Result<Request<()>, Status> + Send + Sync + 'static>>,
38    >,
39    Box<dyn Error>,
40> {
41    let token: MetadataValue<_> = "rustfs rpc".parse()?;
42    let channel = match GLOBAL_Conn_Map.read().await.get(addr) {
43        Some(channel) => channel.clone(),
44        None => {
45            let connector = Endpoint::from_shared(addr.to_string())?.connect_timeout(Duration::from_secs(60));
46            connector.connect().await?
47        }
48    };
49    GLOBAL_Conn_Map.write().await.insert(addr.to_string(), channel.clone());
50
51    // let timeout_channel = Timeout::new(channel, Duration::from_secs(60));
52    Ok(NodeServiceClient::with_interceptor(
53        channel,
54        Box::new(move |mut req: Request<()>| {
55            req.metadata_mut().insert("authorization", token.clone());
56            Ok(req)
57        }),
58    ))
59}