fix: Use SystemTime instead of Instant everywhere

If a time value doesn't need to be sent to another host, saved to the db or otherwise used across
program restarts, a monotonically nondecreasing clock (`Instant`) should be used. But as `Instant`
may use `libc::clock_gettime(CLOCK_MONOTONIC)`, e.g. on Android, and does not advance while being in
deep sleep mode, get rid of `Instant` in favor of using `SystemTime`, but add `tools::Time` as an
alias for it with the appropriate comment so that it's clear why `Instant` isn't used in those
places and to protect from unwanted usages of `Instant` in the future. Also this can help to switch
to another clock impl if we find any.
This commit is contained in:
iequidoo
2023-12-14 20:42:25 -03:00
committed by iequidoo
parent f4ed63c54c
commit 31ee3feb57
10 changed files with 56 additions and 45 deletions

View File

@@ -2,7 +2,7 @@
pub mod send;
use std::time::{Duration, SystemTime};
use std::time::Duration;
use anyhow::{bail, format_err, Context as _, Error, Result};
use async_smtp::response::{Category, Code, Detail};
@@ -28,6 +28,7 @@ use crate::scheduler::connectivity::ConnectivityStore;
use crate::socks::Socks5Config;
use crate::sql;
use crate::stock_str::unencrypted_email;
use crate::tools::{self, time_elapsed};
/// SMTP connection, write and read timeout.
const SMTP_TIMEOUT: Duration = Duration::from_secs(60);
@@ -43,7 +44,7 @@ pub(crate) struct Smtp {
/// Timestamp of last successful send/receive network interaction
/// (eg connect or send succeeded). On initialization and disconnect
/// it is set to None.
last_success: Option<SystemTime>,
last_success: Option<tools::Time>,
pub(crate) connectivity: ConnectivityStore,
@@ -72,11 +73,7 @@ impl Smtp {
/// have been successfully used the last 60 seconds
pub fn has_maybe_stale_connection(&self) -> bool {
if let Some(last_success) = self.last_success {
SystemTime::now()
.duration_since(last_success)
.unwrap_or_default()
.as_secs()
> 60
time_elapsed(&last_success).as_secs() > 60
} else {
false
}
@@ -336,7 +333,7 @@ impl Smtp {
}
self.transport = Some(transport);
self.last_success = Some(SystemTime::now());
self.last_success = Some(tools::Time::now());
context.emit_event(EventType::SmtpConnected(format!(
"SMTP-LOGIN as {} ok",