dcache/src/network/raft_network_impl.rs

141 lines
4.2 KiB
Rust
Raw Normal View History

2023-05-26 00:42:35 +05:30
/*
* mCaptcha - A proof of work based DoS protection system
* Copyright © 2021 Aravinth Manivannan <realravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2023-05-24 21:22:14 +05:30
use async_trait::async_trait;
use openraft::error::InstallSnapshotError;
use openraft::error::NetworkError;
use openraft::error::RPCError;
use openraft::error::RaftError;
use openraft::error::RemoteError;
use openraft::raft::AppendEntriesRequest;
use openraft::raft::AppendEntriesResponse;
use openraft::raft::InstallSnapshotRequest;
use openraft::raft::InstallSnapshotResponse;
use openraft::raft::VoteRequest;
use openraft::raft::VoteResponse;
use openraft::BasicNode;
use openraft::RaftNetwork;
use openraft::RaftNetworkFactory;
use serde::de::DeserializeOwned;
use serde::Serialize;
2023-05-27 10:28:52 +05:30
use crate::DcacheNodeId;
use crate::DcacheTypeConfig;
2023-05-24 21:22:14 +05:30
2023-05-27 10:28:52 +05:30
pub struct DcacheNetwork {}
2023-05-24 21:22:14 +05:30
2023-05-27 10:28:52 +05:30
impl DcacheNetwork {
2023-05-24 21:22:14 +05:30
pub async fn send_rpc<Req, Resp, Err>(
&self,
2023-05-27 10:28:52 +05:30
target: DcacheNodeId,
2023-05-24 21:22:14 +05:30
target_node: &BasicNode,
uri: &str,
req: Req,
2023-05-27 10:28:52 +05:30
) -> Result<Resp, RPCError<DcacheNodeId, BasicNode, Err>>
2023-05-24 21:22:14 +05:30
where
Req: Serialize,
Err: std::error::Error + DeserializeOwned,
Resp: DeserializeOwned,
{
let addr = &target_node.addr;
let url = format!("http://{}/{}", addr, uri);
tracing::debug!("send_rpc to url: {}", url);
let client = reqwest::Client::new();
tracing::debug!("client is created for: {}", url);
let resp = client
.post(url)
.json(&req)
.send()
.await
.map_err(|e| RPCError::Network(NetworkError::new(&e)))?;
tracing::debug!("client.post() is sent");
let res: Result<Resp, Err> = resp
.json()
.await
.map_err(|e| RPCError::Network(NetworkError::new(&e)))?;
res.map_err(|e| RPCError::RemoteError(RemoteError::new(target, e)))
}
}
2023-05-27 10:28:52 +05:30
// NOTE: This could be implemented also on `Arc<DcacheNetwork>`, but since it's empty, implemented
2023-05-24 21:22:14 +05:30
// directly.
#[async_trait]
2023-05-27 10:28:52 +05:30
impl RaftNetworkFactory<DcacheTypeConfig> for DcacheNetwork {
type Network = DcacheNetworkConnection;
2023-05-24 21:22:14 +05:30
2023-05-27 10:28:52 +05:30
async fn new_client(&mut self, target: DcacheNodeId, node: &BasicNode) -> Self::Network {
DcacheNetworkConnection {
owner: DcacheNetwork {},
2023-05-24 21:22:14 +05:30
target,
target_node: node.clone(),
}
}
}
2023-05-27 10:28:52 +05:30
pub struct DcacheNetworkConnection {
owner: DcacheNetwork,
target: DcacheNodeId,
2023-05-24 21:22:14 +05:30
target_node: BasicNode,
}
#[async_trait]
2023-05-27 10:28:52 +05:30
impl RaftNetwork<DcacheTypeConfig> for DcacheNetworkConnection {
2023-05-24 21:22:14 +05:30
async fn send_append_entries(
&mut self,
2023-05-27 10:28:52 +05:30
req: AppendEntriesRequest<DcacheTypeConfig>,
2023-05-24 21:22:14 +05:30
) -> Result<
2023-05-27 10:28:52 +05:30
AppendEntriesResponse<DcacheNodeId>,
RPCError<DcacheNodeId, BasicNode, RaftError<DcacheNodeId>>,
2023-05-24 21:22:14 +05:30
> {
self.owner
.send_rpc(self.target, &self.target_node, "raft-append", req)
.await
}
async fn send_install_snapshot(
&mut self,
2023-05-27 10:28:52 +05:30
req: InstallSnapshotRequest<DcacheTypeConfig>,
2023-05-24 21:22:14 +05:30
) -> Result<
2023-05-27 10:28:52 +05:30
InstallSnapshotResponse<DcacheNodeId>,
RPCError<DcacheNodeId, BasicNode, RaftError<DcacheNodeId, InstallSnapshotError>>,
2023-05-24 21:22:14 +05:30
> {
self.owner
.send_rpc(self.target, &self.target_node, "raft-snapshot", req)
.await
}
async fn send_vote(
&mut self,
2023-05-27 10:28:52 +05:30
req: VoteRequest<DcacheNodeId>,
2023-05-24 21:22:14 +05:30
) -> Result<
2023-05-27 10:28:52 +05:30
VoteResponse<DcacheNodeId>,
RPCError<DcacheNodeId, BasicNode, RaftError<DcacheNodeId>>,
2023-05-24 21:22:14 +05:30
> {
self.owner
.send_rpc(self.target, &self.target_node, "raft-vote", req)
.await
}
}