From b4fe41ad0c782c05331c8544118663b11a2937b0 Mon Sep 17 00:00:00 2001 From: Bo Chen Date: Thu, 18 Aug 2022 12:03:04 -0700 Subject: [PATCH] virtio-devices: block: Refactor 'handle_event' for readability Signed-off-by: Bo Chen --- virtio-devices/src/block.rs | 102 +++++++++++++++++------------------- 1 file changed, 49 insertions(+), 53 deletions(-) diff --git a/virtio-devices/src/block.rs b/virtio-devices/src/block.rs index c3270b7e6..d24923bc5 100644 --- a/virtio-devices/src/block.rs +++ b/virtio-devices/src/block.rs @@ -295,24 +295,21 @@ impl EpollHelperHandler for BlockEpollHandler { // Process the queue only when the rate limit is not reached if !rate_limit_reached { - match self.process_queue_submit() { - Ok(needs_notification) => { - if needs_notification { - if let Err(e) = self.signal_used_queue() { - return Err(EpollHelperError::HandleEvent(anyhow!( - "Failed to signal used queue: {:?}", - e - ))); - } - } - } - Err(e) => { - return Err(EpollHelperError::HandleEvent(anyhow!( - "Failed to process queue (submit): {:?}", + let needs_notification = self.process_queue_submit().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to process queue (submit): {:?}", + e + )) + })?; + + if needs_notification { + self.signal_used_queue().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to signal used queue: {:?}", e - ))); - } - } + )) + })? + }; } } COMPLETION_EVENT => { @@ -320,49 +317,48 @@ impl EpollHelperHandler for BlockEpollHandler { EpollHelperError::HandleEvent(anyhow!("Failed to get queue event: {:?}", e)) })?; - match self.process_queue_complete() { - Ok(needs_notification) => { - if needs_notification { - if let Err(e) = self.signal_used_queue() { - return Err(EpollHelperError::HandleEvent(anyhow!( - "Failed to signal used queue: {:?}", - e - ))); - } - } - } - Err(e) => { - return Err(EpollHelperError::HandleEvent(anyhow!( - "Failed to process queue (complete): {:?}", + let needs_notification = self.process_queue_complete().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to process queue (complete): {:?}", + e + )) + })?; + + if needs_notification { + self.signal_used_queue().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to signal used queue: {:?}", e - ))); - } + )) + })?; } } RATE_LIMITER_EVENT => { if let Some(rate_limiter) = &mut self.rate_limiter { // Upon rate limiter event, call the rate limiter handler // and restart processing the queue. - if rate_limiter.event_handler().is_ok() { - match self.process_queue_submit() { - Ok(needs_notification) => { - if needs_notification { - if let Err(e) = self.signal_used_queue() { - return Err(EpollHelperError::HandleEvent(anyhow!( - "Failed to signal used queue: {:?}", - e - ))); - } - } - } - Err(e) => { - return Err(EpollHelperError::HandleEvent(anyhow!( - "Failed to process queue (submit): {:?}", - e - ))); - } - } - } + rate_limiter.event_handler().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to process rate limiter event: {:?}", + e + )) + })?; + + let needs_notification = self.process_queue_submit().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to process queue (submit): {:?}", + e + )) + })?; + + if needs_notification { + self.signal_used_queue().map_err(|e| { + EpollHelperError::HandleEvent(anyhow!( + "Failed to signal used queue: {:?}", + e + )) + })? + }; } else { return Err(EpollHelperError::HandleEvent(anyhow!( "Unexpected 'RATE_LIMITER_EVENT' when rate_limiter is not enabled."