rustfs_lock/
remote_client.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
15use crate::{Locker, lock_args::LockArgs};
16use async_trait::async_trait;
17use rustfs_protos::{node_service_time_out_client, proto_gen::node_service::GenerallyLockRequest};
18use std::io::{Error, Result};
19use tonic::Request;
20use tracing::info;
21
22#[derive(Debug, Clone)]
23pub struct RemoteClient {
24    addr: String,
25}
26
27impl RemoteClient {
28    pub fn new(url: url::Url) -> Self {
29        let addr = format!("{}://{}:{}", url.scheme(), url.host_str().unwrap(), url.port().unwrap());
30        Self { addr }
31    }
32}
33
34#[async_trait]
35impl Locker for RemoteClient {
36    async fn lock(&mut self, args: &LockArgs) -> Result<bool> {
37        info!("remote lock");
38        let args = serde_json::to_string(args)?;
39        let mut client = node_service_time_out_client(&self.addr)
40            .await
41            .map_err(|err| Error::other(format!("can not get client, err: {err}")))?;
42        let request = Request::new(GenerallyLockRequest { args });
43
44        let response = client.lock(request).await.map_err(Error::other)?.into_inner();
45
46        if let Some(error_info) = response.error_info {
47            return Err(Error::other(error_info));
48        }
49
50        Ok(response.success)
51    }
52
53    async fn unlock(&mut self, args: &LockArgs) -> Result<bool> {
54        info!("remote unlock");
55        let args = serde_json::to_string(args)?;
56        let mut client = node_service_time_out_client(&self.addr)
57            .await
58            .map_err(|err| Error::other(format!("can not get client, err: {err}")))?;
59        let request = Request::new(GenerallyLockRequest { args });
60
61        let response = client.un_lock(request).await.map_err(Error::other)?.into_inner();
62
63        if let Some(error_info) = response.error_info {
64            return Err(Error::other(error_info));
65        }
66
67        Ok(response.success)
68    }
69
70    async fn rlock(&mut self, args: &LockArgs) -> Result<bool> {
71        info!("remote rlock");
72        let args = serde_json::to_string(args)?;
73        let mut client = node_service_time_out_client(&self.addr)
74            .await
75            .map_err(|err| Error::other(format!("can not get client, err: {err}")))?;
76        let request = Request::new(GenerallyLockRequest { args });
77
78        let response = client.r_lock(request).await.map_err(Error::other)?.into_inner();
79
80        if let Some(error_info) = response.error_info {
81            return Err(Error::other(error_info));
82        }
83
84        Ok(response.success)
85    }
86
87    async fn runlock(&mut self, args: &LockArgs) -> Result<bool> {
88        info!("remote runlock");
89        let args = serde_json::to_string(args)?;
90        let mut client = node_service_time_out_client(&self.addr)
91            .await
92            .map_err(|err| Error::other(format!("can not get client, err: {err}")))?;
93        let request = Request::new(GenerallyLockRequest { args });
94
95        let response = client.r_un_lock(request).await.map_err(Error::other)?.into_inner();
96
97        if let Some(error_info) = response.error_info {
98            return Err(Error::other(error_info));
99        }
100
101        Ok(response.success)
102    }
103
104    async fn refresh(&mut self, args: &LockArgs) -> Result<bool> {
105        info!("remote refresh");
106        let args = serde_json::to_string(args)?;
107        let mut client = node_service_time_out_client(&self.addr)
108            .await
109            .map_err(|err| Error::other(format!("can not get client, err: {err}")))?;
110        let request = Request::new(GenerallyLockRequest { args });
111
112        let response = client.refresh(request).await.map_err(Error::other)?.into_inner();
113
114        if let Some(error_info) = response.error_info {
115            return Err(Error::other(error_info));
116        }
117
118        Ok(response.success)
119    }
120
121    async fn force_unlock(&mut self, args: &LockArgs) -> Result<bool> {
122        info!("remote force_unlock");
123        let args = serde_json::to_string(args)?;
124        let mut client = node_service_time_out_client(&self.addr)
125            .await
126            .map_err(|err| Error::other(format!("can not get client, err: {err}")))?;
127        let request = Request::new(GenerallyLockRequest { args });
128
129        let response = client.force_un_lock(request).await.map_err(Error::other)?.into_inner();
130
131        if let Some(error_info) = response.error_info {
132            return Err(Error::other(error_info));
133        }
134
135        Ok(response.success)
136    }
137
138    async fn close(&self) {}
139
140    async fn is_online(&self) -> bool {
141        true
142    }
143
144    async fn is_local(&self) -> bool {
145        false
146    }
147}