Repository: firecracker-microvm/firecracker
File: src/vmm/src/devices/virtio/net/device.rs (Rust)

Code before:
        if self.rx_rate_limiter.is_blocked() {
            self.metrics.rx_rate_limiter_throttled.inc();
            return;
        }

        self.resume_rx()
            .unwrap_or_else(|err| report_net_event_fail(&self.metrics, err));
    }

    /// Process a single TX queue event.
    ///
    /// This is called by the event manager responding to the guest adding a new
    /// buffer in the TX queue.
    pub fn process_tx_queue_event(&mut self) {
        self.metrics.tx_queue_event_count.inc();
        if let Err(err) = self.queue_evts[TX_INDEX].read() {
            error!("Failed to get tx queue event: {:?}", err);
            self.metrics.event_fails.inc();
        } else if !self.tx_rate_limiter.is_blocked()
        // If the limiter is not blocked, continue transmitting bytes.
        {
            self.process_tx()
                .inspect_err(|e| {
                    if let DeviceError::InvalidAvailIdx(err) = e {
                        panic!("{}", err)
                    }
                })
                .unwrap_or_else(|err| report_net_event_fail(&self.metrics, err));
        } else {
            self.metrics.tx_rate_limiter_throttled.inc();
        }
    }

    pub fn process_rx_rate_limiter_event(&mut self) {
        self.metrics.rx_event_rate_limiter_count.inc();
        // Upon rate limiter event, call the rate limiter handler
        // and restart processing the queue.

        match self.rx_rate_limiter.event_handler() {
            Ok(_) => {
                // There might be enough budget now to receive the frame.
                self.resume_rx()
                    .inspect_err(|e| {
                        if let DeviceError::InvalidAvailIdx(err) = e {
                            panic!("{}", err)
                        }
                    })
                    .unwrap_or_else(|err| report_net_event_fail(&self.metrics, err));
            }
            Err(err) => {
                error!("Failed to get rx rate-limiter event: {:?}", err);

Review comment (bug):
`report_net_event_fail` already has a panic inside for `InvalidAvailIdx` error

Code after:
        if self.rx_rate_limiter.is_blocked() {
            self.metrics.rx_rate_limiter_throttled.inc();
            return;
        }

        self.resume_rx()
            .unwrap_or_else(|err| report_net_event_fail(&self.metrics, err));
    }

    /// Process a single TX queue event.
    ///
    /// This is called by the event manager responding to the guest adding a new
    /// buffer in the TX queue.
    pub fn process_tx_queue_event(&mut self) {
        self.metrics.tx_queue_event_count.inc();
        if let Err(err) = self.queue_evts[TX_INDEX].read() {
            error!("Failed to get tx queue event: {:?}", err);
            self.metrics.event_fails.inc();
        } else if !self.tx_rate_limiter.is_blocked()
        // If the limiter is not blocked, continue transmitting bytes.
        {
            self.process_tx()
                .unwrap_or_else(|err| report_net_event_fail(&self.metrics, err));
        } else {
            self.metrics.tx_rate_limiter_throttled.inc();
        }
    }

    pub fn process_rx_rate_limiter_event(&mut self) {
        self.metrics.rx_event_rate_limiter_count.inc();
        // Upon rate limiter event, call the rate limiter handler
        // and restart processing the queue.

        match self.rx_rate_limiter.event_handler() {
            Ok(_) => {
                // There might be enough budget now to receive the frame.
                self.resume_rx()
                    .unwrap_or_else(|err| report_net_event_fail(&self.metrics, err));
            }
            Err(err) => {
                error!("Failed to get rx rate-limiter event: {:?}", err);
                self.metrics.event_fails.inc();
            }
        }
    }

    pub fn process_tx_rate_limiter_event(&mut self) {
        self.metrics.tx_rate_limiter_event_count.inc();
        // Upon rate limiter event, call the rate limiter handler
        // and restart processing the queue.
        match self.tx_rate_limiter.event_handler() {