pub(crate) async fn update_request_cadence(&self, thread_number: usize) {
        if let Some(co_mitigation) = self.config.co_mitigation.as_ref() {
            // Return immediately if coordinated omission mitigation is disabled.
            if co_mitigation == &SwanlingCoordinatedOmissionMitigation::Disabled {
                return;
            }

            // Grab a read/write-copy of the `requests_cadence` object to update the
            // current request cadence.
            let mut request_cadence = self.request_cadence.write().await;

            // Grab the current timestamp to calculate the difference since the last
            // time through the loop.
            let now = std::time::Instant::now();

            // Swap out the `slept` counter, which is the total time the SwanlingUser slept
            // between tasks, a potentially randomly changing value. Reset to 0 for the
            // next loop through all SwanlingTasks.
            request_cadence.delays_since_last_time = self.slept.swap(0, Ordering::SeqCst);

            // How much time passed since the last time this SwanlingUser looped through all
            // tasks, accounting for time waiting between SwanlingTasks due to `set_wait_time`.
            let elapsed = (now - request_cadence.last_time).as_millis() as u64
                - request_cadence.delays_since_last_time;

            // Update `minimum_cadence` if this was the fastest seen.
            if elapsed < request_cadence.minimum_cadence || request_cadence.minimum_cadence == 0 {
                request_cadence.minimum_cadence = elapsed;
            // Update `maximum_cadence` if this was the slowest seen.
            } else if elapsed > request_cadence.maximum_cadence {
                request_cadence.maximum_cadence = elapsed;
            }

            // Update request_cadence metrics based on the timing of the current request.
            request_cadence.counter += 1;
            request_cadence.total_elapsed += elapsed;
            request_cadence.last_time = now;
            request_cadence.average_cadence =
                request_cadence.total_elapsed / request_cadence.counter;

            if request_cadence.counter > 3 {
                if request_cadence.coordinated_omission_counter < 0 {
                    debug!(
                        "user {} enabled coordinated omission mitigation",
                        thread_number
                    );
                    request_cadence.coordinated_omission_counter += 1;
                }
                // Calculate the expected cadence for this SwanlingTask request.
                let cadence = match co_mitigation {
                    // Expected cadence is the average time between requests.
                    SwanlingCoordinatedOmissionMitigation::Average => {
                        request_cadence.average_cadence
                    }
                    // Expected cadence is the maximum time between requests.
                    SwanlingCoordinatedOmissionMitigation::Maximum => {
                        request_cadence.maximum_cadence
                    }
                    // Expected cadence is the minimum time between requests.
                    SwanlingCoordinatedOmissionMitigation::Minimum => {
                        request_cadence.minimum_cadence
                    }
                    // This is not possible as we would have exited already if coordinated
                    // omission mitigation was disabled.
                    SwanlingCoordinatedOmissionMitigation::Disabled => unreachable!(),
                };
                if elapsed > (cadence * 2) {
                    debug!(
                        "user {}: coordinated_omission_mitigation: elapsed({}) > cadence({})",
                        thread_number, elapsed, cadence
                    );
                    request_cadence.coordinated_omission_counter += 1;
                    request_cadence.coordinated_omission_mitigation = elapsed;
                } else {
                    request_cadence.coordinated_omission_mitigation = 0;
                }
                // Always track the expected cadence.
                request_cadence.user_cadence = cadence;
            }
        } else {
            // Coordinated Omission Mitigation defaults to average.
            unreachable!();
        }
    }
/// Tracks the time it takes for the current SwanlingUser to loop through all SwanlingTasks
/// if Coordinated Omission Mitigation is enabled.
Tracks the time it takes for the current SwanlingUser to loop through all SwanlingTasks
if Coordinated Omission Mitigation is enabled.
Tracks the time it takes for the current SwanlingUser to loop through all SwanlingTasks if Coordinated Omission Mitigation is enabled .
pub(crate) async fn update_request_cadence(&self, thread_number: usize) {
        if let Some(co_mitigation) = self.config.co_mitigation.as_ref() {
            // Return immediately if coordinated omission mitigation is disabled.
            if co_mitigation == &SwanlingCoordinatedOmissionMitigation::Disabled {
                return;
            }
            // Grab a read/write-copy of the `requests_cadence` object to update the
            // current request cadence.
            let mut request_cadence = self.request_cadence.write().await;
            // Grab the current timestamp to calculate the difference since the last
            // time through the loop.
            let now = std::time::Instant::now();
            // Swap out the `slept` counter, which is the total time the SwanlingUser slept
            // between tasks, a potentially randomly changing value. Reset to 0 for the
            // next loop through all SwanlingTasks.
            request_cadence.delays_since_last_time = self.slept.swap(0, Ordering::SeqCst);
            // How much time passed since the last time this SwanlingUser looped through all
            // tasks, accounting for time waiting between SwanlingTasks due to `set_wait_time`.
            let elapsed = (now - request_cadence.last_time).as_millis() as u64
                - request_cadence.delays_since_last_time;
            // Update `minimum_cadence` if this was the fastest seen.
            if elapsed < request_cadence.minimum_cadence || request_cadence.minimum_cadence == 0 {
                request_cadence.minimum_cadence = elapsed;
            // Update `maximum_cadence` if this was the slowest seen.
            } else if elapsed > request_cadence.maximum_cadence {
                request_cadence.maximum_cadence = elapsed;
            }
            // Update request_cadence metrics based on the timing of the current request.
            request_cadence.counter += 1;
            request_cadence.total_elapsed += elapsed;
            request_cadence.last_time = now;
            request_cadence.average_cadence =
                request_cadence.total_elapsed / request_cadence.counter;
            if request_cadence.counter > 3 {
                if request_cadence.coordinated_omission_counter < 0 {
                    debug!(
                        "user {} enabled coordinated omission mitigation",
                        thread_number
                    );
                    request_cadence.coordinated_omission_counter += 1;
                }
                // Calculate the expected cadence for this SwanlingTask request.
                let cadence = match co_mitigation {
                    // Expected cadence is the average time between requests.
                    SwanlingCoordinatedOmissionMitigation::Average => {
                        request_cadence.average_cadence
                    }
                    // Expected cadence is the maximum time between requests.
                    SwanlingCoordinatedOmissionMitigation::Maximum => {
                        request_cadence.maximum_cadence
                    }
                    // Expected cadence is the minimum time between requests.
                    SwanlingCoordinatedOmissionMitigation::Minimum => {
                        request_cadence.minimum_cadence
                    }
                    // This is not possible as we would have exited already if coordinated
                    // omission mitigation was disabled.
                    SwanlingCoordinatedOmissionMitigation::Disabled => unreachable!(),
                };
                if elapsed > (cadence * 2) {
                    debug!(
                        "user {}: coordinated_omission_mitigation: elapsed({}) > cadence({})",
                        thread_number, elapsed, cadence
                    );
                    request_cadence.coordinated_omission_counter += 1;
                    request_cadence.coordinated_omission_mitigation = elapsed;
                } else {
                    request_cadence.coordinated_omission_mitigation = 0;
                }
                // Always track the expected cadence.
                request_cadence.user_cadence = cadence;
            }
        } else {
            // Coordinated Omission Mitigation defaults to average.
            unreachable!();
        }
    }
pub ( crate ) async fn update_request_cadence ( & self , thread_number : usize ) { if let Some ( co_mitigation ) = self . config . co_mitigation . as_ref ( ) { // Return immediately if coordinated omission mitigation is disabled. if co_mitigation == & SwanlingCoordinatedOmissionMitigation :: Disabled { return ; } // Grab a read/write-copy of the `requests_cadence` object to update the // current request cadence. let mut request_cadence = self . request_cadence . write ( ) . await ; // Grab the current timestamp to calculate the difference since the last // time through the loop. let now = std :: time :: Instant :: now ( ) ; // Swap out the `slept` counter, which is the total time the SwanlingUser slept // between tasks, a potentially randomly changing value. Reset to 0 for the // next loop through all SwanlingTasks. request_cadence . delays_since_last_time = self . slept . swap ( 0 , Ordering :: SeqCst ) ; // How much time passed since the last time this SwanlingUser looped through all // tasks, accounting for time waiting between SwanlingTasks due to `set_wait_time`. let elapsed = ( now - request_cadence . last_time ) . as_millis ( ) as u64 - request_cadence . delays_since_last_time ; // Update `minimum_cadence` if this was the fastest seen. if elapsed < request_cadence . minimum_cadence || request_cadence . minimum_cadence == 0 { request_cadence . minimum_cadence = elapsed ; // Update `maximum_cadence` if this was the slowest seen. } else if elapsed > request_cadence . maximum_cadence { request_cadence . maximum_cadence = elapsed ; } // Update request_cadence metrics based on the timing of the current request. request_cadence . counter += 1 ; request_cadence . total_elapsed += elapsed ; request_cadence . last_time = now ; request_cadence . average_cadence = request_cadence . total_elapsed / request_cadence . counter ; if request_cadence . counter > 3 { if request_cadence . coordinated_omission_counter < 0 { debug ! ( " " thread_number ) ; request_cadence . coordinated_omission_counter += 1 ; } // Calculate the expected cadence for this SwanlingTask request. let cadence = match co_mitigation { // Expected cadence is the average time between requests. SwanlingCoordinatedOmissionMitigation :: Average => { request_cadence . average_cadence } // Expected cadence is the maximum time between requests. SwanlingCoordinatedOmissionMitigation :: Maximum => { request_cadence . maximum_cadence } // Expected cadence is the minimum time between requests. SwanlingCoordinatedOmissionMitigation :: Minimum => { request_cadence . minimum_cadence } // This is not possible as we would have exited already if coordinated // omission mitigation was disabled. SwanlingCoordinatedOmissionMitigation :: Disabled => unreachable ! ( ) , } ; if elapsed > ( cadence * 2 ) { debug ! ( " " thread_number elapsed cadence ) ; request_cadence . coordinated_omission_counter += 1 ; request_cadence . coordinated_omission_mitigation = elapsed ; } else { request_cadence . coordinated_omission_mitigation = 0 ; } // Always track the expected cadence. request_cadence . user_cadence = cadence ; } } else { // Coordinated Omission Mitigation defaults to average. unreachable ! ( ) ; } }
Tracks the time it takes for the current SwanlingUser to loop through all SwanlingTasks
if Coordinated Omission Mitigation is enabled.
Tracks the time it takes for the current SwanlingUser to loop through all SwanlingTasks if Coordinated Omission Mitigation is enabled .
&self
thread_number
usize