mirror of
https://git.asonix.dog/asonix/relay.git
synced 2024-11-14 05:31:02 +00:00
Remove direct dependency on time
This commit is contained in:
parent
05288b56e1
commit
726a479025
3 changed files with 15 additions and 13 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -339,7 +339,6 @@ dependencies = [
|
|||
"sled",
|
||||
"structopt",
|
||||
"thiserror",
|
||||
"time 0.3.5",
|
||||
"toml",
|
||||
"tracing",
|
||||
"tracing-actix-web",
|
||||
|
|
|
@ -47,7 +47,6 @@ sha2 = "0.10"
|
|||
sled = "0.34.6"
|
||||
structopt = "0.3.12"
|
||||
thiserror = "1.0"
|
||||
time = "0.3.5"
|
||||
tracing = "0.1"
|
||||
tracing-awc = { version = "0.1.0-beta.10" }
|
||||
tracing-error = "0.2"
|
||||
|
|
|
@ -13,12 +13,16 @@ use std::{
|
|||
atomic::{AtomicUsize, Ordering},
|
||||
Arc,
|
||||
},
|
||||
time::SystemTime,
|
||||
time::{Duration, SystemTime},
|
||||
};
|
||||
use time::OffsetDateTime;
|
||||
use tracing::{debug, info, warn};
|
||||
use tracing_awc::Tracing;
|
||||
|
||||
const ONE_SECOND: u64 = 1;
|
||||
const ONE_MINUTE: u64 = 60 * ONE_SECOND;
|
||||
const ONE_HOUR: u64 = 60 * ONE_MINUTE;
|
||||
const ONE_DAY: u64 = 24 * ONE_HOUR;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct Breakers {
|
||||
inner: Arc<DashMap<String, Breaker>>,
|
||||
|
@ -91,8 +95,8 @@ impl Default for Breakers {
|
|||
#[derive(Debug)]
|
||||
struct Breaker {
|
||||
failures: usize,
|
||||
last_attempt: OffsetDateTime,
|
||||
last_success: OffsetDateTime,
|
||||
last_attempt: SystemTime,
|
||||
last_success: SystemTime,
|
||||
}
|
||||
|
||||
impl Breaker {
|
||||
|
@ -100,30 +104,30 @@ impl Breaker {
|
|||
10
|
||||
}
|
||||
|
||||
fn failure_wait() -> time::Duration {
|
||||
time::Duration::days(1)
|
||||
fn failure_wait() -> Duration {
|
||||
Duration::from_secs(ONE_DAY)
|
||||
}
|
||||
|
||||
fn should_try(&self) -> bool {
|
||||
self.failures < Self::failure_threshold()
|
||||
|| self.last_attempt + Self::failure_wait() < OffsetDateTime::now_utc()
|
||||
|| self.last_attempt + Self::failure_wait() < SystemTime::now()
|
||||
}
|
||||
|
||||
fn fail(&mut self) {
|
||||
self.failures += 1;
|
||||
self.last_attempt = OffsetDateTime::now_utc();
|
||||
self.last_attempt = SystemTime::now();
|
||||
}
|
||||
|
||||
fn succeed(&mut self) {
|
||||
self.failures = 0;
|
||||
self.last_attempt = OffsetDateTime::now_utc();
|
||||
self.last_success = OffsetDateTime::now_utc();
|
||||
self.last_attempt = SystemTime::now();
|
||||
self.last_success = SystemTime::now();
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Breaker {
|
||||
fn default() -> Self {
|
||||
let now = OffsetDateTime::now_utc();
|
||||
let now = SystemTime::now();
|
||||
|
||||
Breaker {
|
||||
failures: 0,
|
||||
|
|
Loading…
Reference in a new issue