p2p_chat/sync/engine/performance.rs
1//! This module defines data structures and constants for tracking the performance
2//! of mailbox providers.
3use std::time::{Duration, Instant};
4
5/// The maximum number of consecutive failures before a mailbox is considered unreliable.
6pub const MAX_CONSECUTIVE_FAILURES: u32 = 3;
7/// The maximum number of failures within a specified time window before a mailbox is considered unreliable.
8pub const MAX_FAILURES_IN_WINDOW: u32 = 5;
9/// The duration (in seconds) for the failure window.
10pub const FAILURE_WINDOW_SECONDS: u64 = 60; // 1 minute
11
12/// Represents the performance metrics of a mailbox provider.
13#[derive(Debug, Clone)]
14pub struct MailboxPerformance {
15 /// The total count of successful interactions.
16 pub success_count: u32,
17 /// The total count of failed interactions.
18 pub failure_count: u32,
19 /// The number of consecutive failed interactions.
20 pub consecutive_failures: u32,
21 /// The `Instant` of the last successful interaction.
22 pub last_success: Option<Instant>,
23 /// The `Instant` of the last failed interaction.
24 pub last_failure: Option<Instant>,
25 /// The exponentially-weighted moving average response time.
26 pub avg_response_time: Duration,
27}
28
29impl MailboxPerformance {
30 /// Creates a new `MailboxPerformance` instance with default values.
31 pub fn new() -> Self {
32 Self {
33 success_count: 0,
34 failure_count: 0,
35 consecutive_failures: 0,
36 last_success: None,
37 last_failure: None,
38 avg_response_time: Duration::from_millis(1000), // Default to 1 second
39 }
40 }
41}