-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
172 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,46 @@ | ||
//! Zingo-Proxy client server. | ||
use nym_sphinx_anonymous_replies::requests::AnonymousSenderTag; | ||
use std::sync::{atomic::AtomicBool, Arc}; | ||
use tokio::sync::mpsc; | ||
|
||
use self::{ | ||
dispatcher::NymDispatcher, | ||
ingestor::{NymIngestor, TcpIngestor}, | ||
request::ZingoProxyRequest, | ||
worker::WorkerPool, | ||
}; | ||
|
||
pub mod dispatcher; | ||
pub mod error; | ||
pub mod ingestor; | ||
pub mod request; | ||
pub mod worker; | ||
|
||
/// | ||
pub struct Queue<T> { | ||
/// Maximum length of the queue. | ||
max_size: usize, | ||
/// Queue sender. | ||
queue_tx: mpsc::Sender<T>, | ||
/// Queue receiver. | ||
queue_rx: mpsc::Receiver<T>, | ||
} | ||
|
||
/// LightWallet server capable of servicing clients over both http and nym. | ||
pub struct Server { | ||
/// Listens for incoming gRPC requests over HTTP. | ||
tcp_ingestor: TcpIngestor, | ||
/// Listens for incoming gRPC requests over Nym Mixnet. | ||
nym_ingestor: NymIngestor, | ||
/// Sends gRPC responses over Nym Mixnet. | ||
nym_dispatcher: NymDispatcher, | ||
/// Dynamically sized pool of workers. | ||
worker_pool: WorkerPool, | ||
/// Request queue. | ||
request_queue: Queue<ZingoProxyRequest>, | ||
/// Nym response queue. | ||
nym_response_queue: Queue<(Vec<u8>, AnonymousSenderTag)>, | ||
/// Represents the Online status of the Server. | ||
pub online: Arc<AtomicBool>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
//! Holds the server dispatcher (replyer) implementations. | ||
use nym_sdk::mixnet::MixnetMessageSender; | ||
use nym_sphinx_anonymous_replies::requests::AnonymousSenderTag; | ||
use std::sync::{ | ||
atomic::{AtomicBool, Ordering}, | ||
Arc, | ||
}; | ||
use tokio::sync::mpsc; | ||
|
||
use crate::{ | ||
nym::{client::NymClient, error::NymError}, | ||
server::error::DispatcherError, | ||
}; | ||
|
||
/// Status of the worker. | ||
#[derive(Debug, Clone)] | ||
pub enum DispatcherStatus { | ||
/// On hold, due to blockcache / node error. | ||
Inactive, | ||
/// Listening for new requests. | ||
Listening, | ||
} | ||
|
||
/// Sends gRPC responses over Nym Mixnet. | ||
pub struct NymDispatcher { | ||
/// Nym Client | ||
dispatcher: NymClient, | ||
/// Used to send requests to the queue. | ||
response_queue: mpsc::Receiver<(Vec<u8>, AnonymousSenderTag)>, | ||
/// Used to send requests to the queue. | ||
response_requeue: mpsc::Sender<(Vec<u8>, AnonymousSenderTag)>, | ||
/// Represents the Online status of the gRPC server. | ||
online: Arc<AtomicBool>, | ||
/// Current status of the ingestor. | ||
status: DispatcherStatus, | ||
} | ||
|
||
impl NymDispatcher { | ||
/// Creates a Nym Ingestor | ||
pub async fn spawn( | ||
nym_conf_path: &str, | ||
response_queue: mpsc::Receiver<(Vec<u8>, AnonymousSenderTag)>, | ||
response_requeue: mpsc::Sender<(Vec<u8>, AnonymousSenderTag)>, | ||
online: Arc<AtomicBool>, | ||
) -> Result<Self, DispatcherError> { | ||
let client = NymClient::spawn(&format!("{}/dispatcher", nym_conf_path)).await?; | ||
Ok(NymDispatcher { | ||
dispatcher: client, | ||
response_queue, | ||
response_requeue, | ||
online, | ||
status: DispatcherStatus::Inactive, | ||
}) | ||
} | ||
|
||
/// Starts Nym service. | ||
pub async fn serve(mut self) -> tokio::task::JoinHandle<Result<(), DispatcherError>> { | ||
tokio::task::spawn(async move { | ||
// NOTE: This interval may need to be reduced or removed / moved once scale testing begins. | ||
let mut interval = tokio::time::interval(tokio::time::Duration::from_millis(50)); | ||
// TODO Check self.status and wait on server / node if on hold. | ||
self.status = DispatcherStatus::Listening; | ||
loop { | ||
tokio::select! { | ||
_ = interval.tick() => { | ||
if !self.check_online() { | ||
println!("Nym dispatcher shutting down."); | ||
return Ok(()); | ||
} | ||
} | ||
incoming = self.response_queue.recv() => { | ||
match incoming { | ||
Some(response) => { | ||
if !self.check_online() { | ||
println!("Nym dispatcher shutting down."); | ||
return Ok(()); | ||
} | ||
if let Err(nym_e) = self.dispatcher | ||
.client | ||
.send_reply(response.1, response.0.clone()) | ||
.await.map_err(NymError::from) { | ||
// TODO: Convert to use try_send(). | ||
if let Err(e) = self.response_requeue.send(response).await { | ||
eprintln!("Failed to send response over nym: {}\nAnd failed to requeue response: {}\nFatal error! Restarting nym dispatcher.", nym_e, e); | ||
// TODO: Handle error. Restart nym dispatcher. | ||
} | ||
eprintln!("Failed to send response over nym: {}\nResponse requeued, restarting nym dispatcher.", nym_e); | ||
// TODO: Handle error. Restart nym dispatcher. | ||
} | ||
} | ||
None => { | ||
println!("Response queue closed, nym dispatcher shutting down."); | ||
return Ok(()); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
|
||
fn check_online(&self) -> bool { | ||
self.online.load(Ordering::SeqCst) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters