From 2355ced19affa8a7c7b2a390883d66f8f46ea183 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 11 Dec 2024 00:43:37 -0800 Subject: [PATCH] multithreading attempt (failure) --- validation/src/validator.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/validation/src/validator.rs b/validation/src/validator.rs index 23aebd9..7236ff3 100644 --- a/validation/src/validator.rs +++ b/validation/src/validator.rs @@ -38,6 +38,10 @@ pub struct Validator{ roblox_cookie:rbx_asset::cookie::CookieContext, api:api::Context, } +pub struct PartialValidator{ + roblox_cookie:rbx_asset::cookie::CookieContext, + api:api::Context, +} impl Validator{ pub async fn new( @@ -51,16 +55,27 @@ impl Validator{ api, }) } - pub async fn run(mut self){ - while let Some(message)=self.subscriber.next().await{ - self.validate_supress_error(message).await + pub async fn run(Self{mut subscriber,roblox_cookie,api}:Self){ + let sem=tokio::sync::Semaphore::new(16); + let partial_validator=PartialValidator{ + roblox_cookie, + api, + }; + loop{ + let permit=sem.acquire().await.unwrap(); + if let Some(message)=subscriber.next().await{ + tokio::spawn(partial_validator.validate_supress_error(message,permit)); + } } } - async fn validate_supress_error(&self,message:async_nats::Message){ +} +impl PartialValidator{ + async fn validate_supress_error(&self,message:async_nats::Message,permit:tokio::sync::SemaphorePermit<'_>){ match self.validate(message).await{ Ok(())=>println!("Validated, hooray!"), Err(e)=>println!("There was an error, oopsie! {e}"), } + std::mem::drop(permit); } async fn validate(&self,message:async_nats::Message)->Result<(),ValidateError>{ println!("validate {:?}",message);