From 095b73a1b1a63a9c571995585931d894a6553d6d Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sun, 17 Dec 2023 19:25:11 +0530 Subject: [PATCH] feat: pipelined read and write endpoints --- src/network/api.rs | 110 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 108 insertions(+), 2 deletions(-) diff --git a/src/network/api.rs b/src/network/api.rs index d4d0d2e..32c7ea3 100644 --- a/src/network/api.rs +++ b/src/network/api.rs @@ -20,7 +20,10 @@ use actix_web::post; use actix_web::web; use actix_web::web::Data; use actix_web::Responder; +use libmcaptcha::cache::messages::{CachedPoWConfig, RetrivePoW, VerifyCaptchaResult}; use libmcaptcha::master::messages::GetInternalData; +use serde::Deserialize; +use serde::Serialize; use web::Json; use crate::app::DcacheApp; @@ -38,7 +41,7 @@ pub async fn write( #[get("/state")] pub async fn state(app: Data) -> actix_web::Result { let sm = app.store.state_machine.read().await; - let data = sm + let resp = sm .data .master .send(GetInternalData) @@ -47,5 +50,108 @@ pub async fn state(app: Data) -> actix_web::Result { .await .unwrap() .unwrap(); - Ok(Json(data)) + Ok(Json(resp)) +} + +#[derive(Serialize, Deserialize, Clone, Debug)] +pub enum ReadRequest { + RetrivePoW(RetrivePoW), //Reader + VerifyCaptchaResult(VerifyCaptchaResult), //Reader +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub enum ReadResponse { + VerifyCaptchaResult(bool), + RetrivePoW(Option), +} + +#[post("/read")] +pub async fn read( + app: Data, + req: Json, +) -> actix_web::Result { + let sm = app.store.state_machine.read().await; + + let req = req.into_inner(); + let res = match req { + ReadRequest::RetrivePoW(msg) => { + let cache_res = sm + .data + .cache + .send(msg.clone()) + .await + .unwrap() + .await + .unwrap() + .unwrap(); + ReadResponse::RetrivePoW(cache_res) + } + ReadRequest::VerifyCaptchaResult(msg) => { + let cache_res = sm + .data + .cache + .send(msg.clone()) + .await + .unwrap() + .await + .unwrap() + .unwrap(); + ReadResponse::VerifyCaptchaResult(cache_res) + } + }; + Ok(Json(res)) +} + +#[post("/pipeline/read")] +pub async fn pipeline_read( + app: Data, + requests: Json>, +) -> actix_web::Result { + let requests = requests.into_inner(); + let mut responses = Vec::with_capacity(requests.len()); + let sm = app.store.state_machine.read().await; + for request in requests { + let res = match request { + ReadRequest::RetrivePoW(msg) => { + let cache_res = sm + .data + .cache + .send(msg.clone()) + .await + .unwrap() + .await + .unwrap() + .unwrap(); + ReadResponse::RetrivePoW(cache_res) + } + ReadRequest::VerifyCaptchaResult(msg) => { + let cache_res = sm + .data + .cache + .send(msg.clone()) + .await + .unwrap() + .await + .unwrap() + .unwrap(); + ReadResponse::VerifyCaptchaResult(cache_res) + } + }; + + responses.push(res); + } + Ok(Json(responses)) +} + +#[post("/pipeline/write")] +pub async fn pipeline_write( + app: Data, + requests: Json>, +) -> actix_web::Result { + let mut responses = Vec::with_capacity(requests.len()); + let mut requests = requests.into_inner(); + for req in requests.drain(0..) { + responses.push(app.raft.client_write(req).await); + } + Ok(Json(responses)) }