1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use crate::error::{Error, ErrorKind, ErrorResponse};
use crate::telemetry::SyncTelemetryPing;
use std::collections::HashMap;
use std::time::{Duration, SystemTime};
#[derive(Debug, Clone, PartialEq)]
pub enum ServiceStatus {
Ok,
NetworkError,
ServiceError,
AuthenticationError,
BackedOff,
Interrupted,
OtherError,
}
impl ServiceStatus {
pub fn from_err(err: &Error) -> ServiceStatus {
match err.kind() {
ErrorKind::TokenserverHttpError(status) => {
if *status == 401 {
ServiceStatus::AuthenticationError
} else {
ServiceStatus::ServiceError
}
}
ErrorKind::BackoffError(_) => ServiceStatus::ServiceError,
ErrorKind::StorageHttpError(ref e) => match e {
ErrorResponse::Unauthorized { .. } => ServiceStatus::AuthenticationError,
_ => ServiceStatus::ServiceError,
},
ErrorKind::RequestError(_)
| ErrorKind::UnexpectedStatus(_)
| ErrorKind::HawkError(_) => ServiceStatus::NetworkError,
ErrorKind::Interrupted(_) => ServiceStatus::Interrupted,
_ => ServiceStatus::OtherError,
}
}
}
#[derive(Debug)]
pub struct SyncResult {
pub service_status: ServiceStatus,
pub declined: Option<Vec<String>>,
pub result: Result<(), Error>,
pub engine_results: HashMap<String, Result<(), Error>>,
pub telemetry: SyncTelemetryPing,
pub next_sync_after: Option<std::time::SystemTime>,
}
fn advance_backoff(cur_best: SystemTime, r: &Result<(), Error>) -> SystemTime {
if let Err(e) = r {
if let Some(time) = e.get_backoff() {
return std::cmp::max(time, cur_best);
}
}
cur_best
}
impl SyncResult {
pub(crate) fn set_sync_after(&mut self, backoff_duration: Duration) {
let now = SystemTime::now();
let toplevel = advance_backoff(now + backoff_duration, &self.result);
let sync_after = self
.engine_results
.values()
.fold(toplevel, |b, r| advance_backoff(b, r));
if sync_after <= now {
self.next_sync_after = None;
} else {
self.next_sync_after = Some(sync_after);
}
}
}