From 389143879e024798ff8cfe5418066a50cc436cde Mon Sep 17 00:00:00 2001 From: Katia Date: Tue, 3 Dec 2024 14:30:17 +0900 Subject: [PATCH] add: error catchingi n decrement work counter --- src/async_resolver/state_block.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/async_resolver/state_block.rs b/src/async_resolver/state_block.rs index b57c8fe8..f6bd37f0 100644 --- a/src/async_resolver/state_block.rs +++ b/src/async_resolver/state_block.rs @@ -1,6 +1,6 @@ use std::sync::Arc; use tokio::time::Instant; -use super::{server_entry::ServerEntry, server_info::ServerInfo}; +use super::{resolver_error::ResolverError, server_entry::ServerEntry, server_info::ServerInfo}; /// This struct represent the state of information of a pending request. /// @@ -64,16 +64,12 @@ impl StateBlock { /// This function should be called each time the resolver performs work on behalf /// of the request. If the counter passes zero, the request is terminated with a /// temporary error. - /// - /// # Example - /// ``` - /// let mut state_block = StateBlock::new(Instant::now()); - /// state_block.decrement_work_counter(); - /// ``` - pub fn decrement_work_counter(&mut self) { + pub fn decrement_work_counter(&mut self) -> Result { + if self.work_counter == 0 { + return Err(ResolverError::RetriesLimitExceeded); + } self.work_counter -= 1; - - // TODO: Implement the logic to terminate the request if the counter reaches zero. + Ok(self.work_counter) } /// Increments the `current_server_index` of the request. @@ -150,13 +146,17 @@ mod state_block_tests { let mut state_block = StateBlock::new(5, 2, servers); assert_eq!(state_block.get_work_counter(), 5); - state_block.decrement_work_counter(); - assert_eq!(state_block.get_work_counter(), 4); + if let Ok(_) = state_block.decrement_work_counter() { + assert_eq!(state_block.get_work_counter(), 4); + } - state_block.decrement_work_counter(); - assert_eq!(state_block.get_work_counter(), 3); + if let Ok(_) = state_block.decrement_work_counter() { + assert_eq!(state_block.get_work_counter(), 3); + } } + + } \ No newline at end of file