Aravinth Manivannan
77d4720e7d
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
81 lines
2.5 KiB
Rust
81 lines
2.5 KiB
Rust
/*
|
|
* mCaptcha - A proof of work based DoS protection system
|
|
* Copyright © 2023 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/>.
|
|
*/
|
|
use actix_web::post;
|
|
use actix_web::web;
|
|
use actix_web::web::Data;
|
|
use actix_web::Responder;
|
|
use openraft::error::*;
|
|
use openraft::raft::AppendEntriesRequest;
|
|
use openraft::raft::InstallSnapshotRequest;
|
|
use openraft::raft::VoteRequest;
|
|
use openraft::raft::*;
|
|
use serde::*;
|
|
use web::Json;
|
|
|
|
use crate::app::DcacheApp;
|
|
use crate::DcacheNodeId;
|
|
use crate::DcacheTypeConfig;
|
|
|
|
// --- Raft communication
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
pub enum RaftMessage {
|
|
VoteRequest(VoteRequest<DcacheNodeId>),
|
|
Append(AppendEntriesRequest<DcacheTypeConfig>),
|
|
Snapshot(InstallSnapshotRequest<DcacheTypeConfig>),
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub enum RaftRes {
|
|
VoteRes(Result<VoteResponse<DcacheNodeId>, RaftError<DcacheNodeId>>),
|
|
SnapshotRes(
|
|
Result<
|
|
InstallSnapshotResponse<DcacheNodeId>,
|
|
RaftError<DcacheNodeId, InstallSnapshotError>,
|
|
>,
|
|
),
|
|
AppendRes(Result<AppendEntriesResponse<DcacheNodeId>, RaftError<DcacheNodeId>>),
|
|
}
|
|
|
|
#[post("/raft-vote")]
|
|
pub async fn vote(
|
|
app: Data<DcacheApp>,
|
|
req: Json<VoteRequest<DcacheNodeId>>,
|
|
) -> actix_web::Result<impl Responder> {
|
|
let res = app.raft.vote(req.0).await;
|
|
Ok(Json(res))
|
|
}
|
|
|
|
#[post("/raft-append")]
|
|
pub async fn append(
|
|
app: Data<DcacheApp>,
|
|
req: Json<AppendEntriesRequest<DcacheTypeConfig>>,
|
|
) -> actix_web::Result<impl Responder> {
|
|
let res = app.raft.append_entries(req.0).await;
|
|
Ok(Json(res))
|
|
}
|
|
|
|
#[post("/raft-snapshot")]
|
|
pub async fn snapshot(
|
|
app: Data<DcacheApp>,
|
|
req: Json<InstallSnapshotRequest<DcacheTypeConfig>>,
|
|
) -> actix_web::Result<impl Responder> {
|
|
let res = app.raft.install_snapshot(req.0).await;
|
|
Ok(Json(res))
|
|
}
|