From 22f5985f1b22ee91fab49cc0df2ce998c8cea81c Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Thu, 29 Mar 2018 13:18:08 -0600 Subject: [PATCH] Do request verification in parallel, and then process the verified requests --- src/accountant.rs | 2 +- src/accountant_skel.rs | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/accountant.rs b/src/accountant.rs index 622b96a1c..8d4d90995 100644 --- a/src/accountant.rs +++ b/src/accountant.rs @@ -116,7 +116,7 @@ impl Accountant { } /// Process a Transaction that has already been verified. - fn process_verified_transaction( + pub fn process_verified_transaction( self: &mut Self, tr: &Transaction, allow_deposits: bool, diff --git a/src/accountant_skel.rs b/src/accountant_skel.rs index 4b8c61185..390fab91d 100644 --- a/src/accountant_skel.rs +++ b/src/accountant_skel.rs @@ -19,6 +19,7 @@ use std::thread::{spawn, JoinHandle}; use std::time::Duration; use streamer; use transaction::Transaction; +use rayon::prelude::*; pub struct AccountantSkel { pub acc: Accountant, @@ -34,8 +35,19 @@ pub enum Request { GetId { is_last: bool }, } +impl Request { + /// Verify the request is valid. + pub fn verify(&self) -> bool { + match *self { + Request::Transaction(ref tr) => tr.verify(), + _ => true, + } + } +} + +/// Parallel verfication of a batch of requests. fn filter_valid_requests(reqs: Vec<(Request, SocketAddr)>) -> Vec<(Request, SocketAddr)> { - reqs + reqs.into_par_iter().filter({ |x| x.0.verify() }).collect() } #[derive(Serialize, Deserialize, Debug)] @@ -66,10 +78,10 @@ impl AccountantSkel { } /// Process Request items sent by clients. - pub fn process_request(self: &mut Self, msg: Request) -> Option { + pub fn process_verified_request(self: &mut Self, msg: Request) -> Option { match msg { Request::Transaction(tr) => { - if let Err(err) = self.acc.process_transaction(tr) { + if let Err(err) = self.acc.process_verified_transaction(&tr, false) { eprintln!("Transaction error: {:?}", err); } None @@ -114,7 +126,7 @@ impl AccountantSkel { let mut num = 0; let mut ursps = rsps.write().unwrap(); for (req, rsp_addr) in reqs { - if let Some(resp) = obj.lock().unwrap().process_request(req) { + if let Some(resp) = obj.lock().unwrap().process_verified_request(req) { if ursps.responses.len() <= num { ursps .responses