| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
use std::collections::HashSet; |
| 10 |
use std::time::Duration; |
| 11 |
|
| 12 |
use chrono::{DateTime, Utc}; |
| 13 |
|
| 14 |
use crate::error::SyncKitError; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 19 |
pub enum SyncState { |
| 20 |
|
| 21 |
Idle, |
| 22 |
|
| 23 |
Syncing, |
| 24 |
|
| 25 |
Error(String), |
| 26 |
|
| 27 |
SubscriptionRequired, |
| 28 |
|
| 29 |
LoggedOut, |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
pub trait SyncObserver: Send + Sync { |
| 36 |
|
| 37 |
fn on_status(&self, _state: SyncState) {} |
| 38 |
|
| 39 |
fn on_changes_applied(&self, _tables: &HashSet<String>) {} |
| 40 |
|
| 41 |
fn on_subscription_required(&self) {} |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
#[derive(Debug, Default, Clone, Copy)] |
| 46 |
pub struct NoopObserver; |
| 47 |
impl SyncObserver for NoopObserver {} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
pub fn backoff_delay(consecutive_failures: u32) -> Duration { |
| 53 |
let mins = 2u64.pow(consecutive_failures.min(4)).min(15); |
| 54 |
Duration::from_secs(mins * 60) |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
pub fn interval_elapsed( |
| 60 |
last_sync: Option<DateTime<Utc>>, |
| 61 |
interval_minutes: i64, |
| 62 |
now: DateTime<Utc>, |
| 63 |
) -> bool { |
| 64 |
match last_sync { |
| 65 |
None => true, |
| 66 |
Some(prev) => now.signed_duration_since(prev).num_minutes() >= interval_minutes, |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
pub fn is_subscription_required(err: &SyncKitError) -> bool { |
| 72 |
matches!(err, SyncKitError::Server { status: 402, .. }) |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
pub fn is_auth_lost(err: &SyncKitError) -> bool { |
| 78 |
matches!( |
| 79 |
err, |
| 80 |
SyncKitError::TokenExpired | SyncKitError::NotAuthenticated |
| 81 |
) || matches!( |
| 82 |
err, |
| 83 |
SyncKitError::Server { |
| 84 |
status: 401 | 403, |
| 85 |
.. |
| 86 |
} |
| 87 |
) |
| 88 |
} |
| 89 |
|
| 90 |
#[cfg(test)] |
| 91 |
mod tests { |
| 92 |
use super::*; |
| 93 |
|
| 94 |
#[test] |
| 95 |
fn backoff_is_exponential_then_capped() { |
| 96 |
assert_eq!(backoff_delay(0).as_secs(), 60); |
| 97 |
assert_eq!(backoff_delay(1).as_secs(), 2 * 60); |
| 98 |
assert_eq!(backoff_delay(2).as_secs(), 4 * 60); |
| 99 |
assert_eq!(backoff_delay(3).as_secs(), 8 * 60); |
| 100 |
assert_eq!(backoff_delay(4).as_secs(), 15 * 60); |
| 101 |
assert_eq!(backoff_delay(50).as_secs(), 15 * 60); |
| 102 |
} |
| 103 |
|
| 104 |
#[test] |
| 105 |
fn interval_elapsed_rules() { |
| 106 |
let now = Utc::now(); |
| 107 |
assert!( |
| 108 |
interval_elapsed(None, 15, now), |
| 109 |
"never-synced is always due" |
| 110 |
); |
| 111 |
let five_ago = now - chrono::Duration::minutes(5); |
| 112 |
assert!(!interval_elapsed(Some(five_ago), 15, now), "too soon"); |
| 113 |
let twenty_ago = now - chrono::Duration::minutes(20); |
| 114 |
assert!( |
| 115 |
interval_elapsed(Some(twenty_ago), 15, now), |
| 116 |
"past the interval" |
| 117 |
); |
| 118 |
} |
| 119 |
|
| 120 |
#[test] |
| 121 |
fn error_classification() { |
| 122 |
let sub = SyncKitError::Server { |
| 123 |
status: 402, |
| 124 |
message: "pay".into(), |
| 125 |
retry_after_secs: None, |
| 126 |
}; |
| 127 |
assert!(is_subscription_required(&sub)); |
| 128 |
assert!(!is_auth_lost(&sub)); |
| 129 |
|
| 130 |
assert!(is_auth_lost(&SyncKitError::TokenExpired)); |
| 131 |
assert!(is_auth_lost(&SyncKitError::Server { |
| 132 |
status: 401, |
| 133 |
message: String::new(), |
| 134 |
retry_after_secs: None |
| 135 |
})); |
| 136 |
assert!(!is_subscription_required(&SyncKitError::TokenExpired)); |
| 137 |
|
| 138 |
let other = SyncKitError::Internal("boom".into()); |
| 139 |
assert!(!is_subscription_required(&other) && !is_auth_lost(&other)); |
| 140 |
} |
| 141 |
} |
| 142 |
|