Remove direct dependency on time

This commit is contained in:
Aode (Lion) 2022-01-17 17:57:06 -06:00
parent 05288b56e1
commit 726a479025
3 changed files with 15 additions and 13 deletions

1
Cargo.lock generated
View file

@ -339,7 +339,6 @@ dependencies = [
"sled", "sled",
"structopt", "structopt",
"thiserror", "thiserror",
"time 0.3.5",
"toml", "toml",
"tracing", "tracing",
"tracing-actix-web", "tracing-actix-web",

View file

@ -47,7 +47,6 @@ sha2 = "0.10"
sled = "0.34.6" sled = "0.34.6"
structopt = "0.3.12" structopt = "0.3.12"
thiserror = "1.0" thiserror = "1.0"
time = "0.3.5"
tracing = "0.1" tracing = "0.1"
tracing-awc = { version = "0.1.0-beta.10" } tracing-awc = { version = "0.1.0-beta.10" }
tracing-error = "0.2" tracing-error = "0.2"

View file

@ -13,12 +13,16 @@ use std::{
atomic::{AtomicUsize, Ordering}, atomic::{AtomicUsize, Ordering},
Arc, Arc,
}, },
time::SystemTime, time::{Duration, SystemTime},
}; };
use time::OffsetDateTime;
use tracing::{debug, info, warn}; use tracing::{debug, info, warn};
use tracing_awc::Tracing; 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)] #[derive(Clone)]
pub(crate) struct Breakers { pub(crate) struct Breakers {
inner: Arc<DashMap<String, Breaker>>, inner: Arc<DashMap<String, Breaker>>,
@ -91,8 +95,8 @@ impl Default for Breakers {
#[derive(Debug)] #[derive(Debug)]
struct Breaker { struct Breaker {
failures: usize, failures: usize,
last_attempt: OffsetDateTime, last_attempt: SystemTime,
last_success: OffsetDateTime, last_success: SystemTime,
} }
impl Breaker { impl Breaker {
@ -100,30 +104,30 @@ impl Breaker {
10 10
} }
fn failure_wait() -> time::Duration { fn failure_wait() -> Duration {
time::Duration::days(1) Duration::from_secs(ONE_DAY)
} }
fn should_try(&self) -> bool { fn should_try(&self) -> bool {
self.failures < Self::failure_threshold() 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) { fn fail(&mut self) {
self.failures += 1; self.failures += 1;
self.last_attempt = OffsetDateTime::now_utc(); self.last_attempt = SystemTime::now();
} }
fn succeed(&mut self) { fn succeed(&mut self) {
self.failures = 0; self.failures = 0;
self.last_attempt = OffsetDateTime::now_utc(); self.last_attempt = SystemTime::now();
self.last_success = OffsetDateTime::now_utc(); self.last_success = SystemTime::now();
} }
} }
impl Default for Breaker { impl Default for Breaker {
fn default() -> Self { fn default() -> Self {
let now = OffsetDateTime::now_utc(); let now = SystemTime::now();
Breaker { Breaker {
failures: 0, failures: 0,