Do request verification in parallel, and then process the verified requests
This commit is contained in:
		| @@ -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, | ||||
|   | ||||
| @@ -19,6 +19,7 @@ use std::thread::{spawn, JoinHandle}; | ||||
| use std::time::Duration; | ||||
| use streamer; | ||||
| use transaction::Transaction; | ||||
| use rayon::prelude::*; | ||||
|  | ||||
| pub struct AccountantSkel<W: Write + Send + 'static> { | ||||
|     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<W: Write + Send + 'static> AccountantSkel<W> { | ||||
|     } | ||||
|  | ||||
|     /// Process Request items sent by clients. | ||||
|     pub fn process_request(self: &mut Self, msg: Request) -> Option<Response> { | ||||
|     pub fn process_verified_request(self: &mut Self, msg: Request) -> Option<Response> { | ||||
|         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<W: Write + Send + 'static> AccountantSkel<W> { | ||||
|             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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user