Skip to content

Commit

Permalink
feat: Cache system time instead of looking at the clock several times…
Browse files Browse the repository at this point in the history
… in a row

The system clock may be adjusted and even go back, so caching system time in code sections where
it's not supposed to change may even protect from races/bugs.
  • Loading branch information
iequidoo committed Dec 14, 2023
1 parent 47fc60b commit b0f2267
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
8 changes: 5 additions & 3 deletions src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2792,12 +2792,14 @@ pub(crate) async fn create_send_msg_job(
);
}

let now = time();

if rendered_msg.is_gossiped {
msg.chat_id.set_gossiped_timestamp(context, time()).await?;
msg.chat_id.set_gossiped_timestamp(context, now).await?;
}

if let Some(last_added_location_id) = rendered_msg.last_added_location_id {
if let Err(err) = location::set_kml_sent_timestamp(context, msg.chat_id, time()).await {
if let Err(err) = location::set_kml_sent_timestamp(context, msg.chat_id, now).await {
error!(context, "Failed to set kml sent_timestamp: {err:#}.");
}
if !msg.hidden {
Expand All @@ -2816,7 +2818,7 @@ pub(crate) async fn create_send_msg_job(
}

if attach_selfavatar {
if let Err(err) = msg.chat_id.set_selfavatar_timestamp(context, time()).await {
if let Err(err) = msg.chat_id.set_selfavatar_timestamp(context, now).await {
error!(context, "Failed to set selfavatar timestamp: {err:#}.");
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,12 @@ impl RecentlySeenLoop {
async fn run(context: Context, interrupt: Receiver<RecentlySeenInterrupt>) {
type MyHeapElem = (Reverse<i64>, ContactId);

let now = SystemTime::now();
let now_ts = now
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_default()
.as_secs() as i64;

// Priority contains all recently seen sorted by the timestamp
// when they become not recently seen.
//
Expand All @@ -1702,7 +1708,7 @@ impl RecentlySeenLoop {
.query_map(
"SELECT id, last_seen FROM contacts
WHERE last_seen > ?",
(time() - SEEN_RECENTLY_SECONDS,),
(now_ts - SEEN_RECENTLY_SECONDS,),
|row| {
let contact_id: ContactId = row.get("id")?;
let last_seen: i64 = row.get("last_seen")?;
Expand All @@ -1717,8 +1723,6 @@ impl RecentlySeenLoop {
.unwrap_or_default();

loop {
let now = SystemTime::now();

let (until, contact_id) =
if let Some((Reverse(timestamp), contact_id)) = unseen_queue.peek() {
(
Expand Down
16 changes: 12 additions & 4 deletions src/imex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ async fn export_backup(context: &Context, dir: &Path, passphrase: String) -> Res
let _d1 = DeleteOnDrop(temp_db_path.clone());
let _d2 = DeleteOnDrop(temp_path.clone());

export_database(context, &temp_db_path, passphrase)
export_database(context, &temp_db_path, passphrase, now)
.await
.context("could not export database")?;

Expand Down Expand Up @@ -762,19 +762,27 @@ where
/// overwritten.
///
/// This also verifies that IO is not running during the export.
async fn export_database(context: &Context, dest: &Path, passphrase: String) -> Result<()> {
async fn export_database(
context: &Context,
dest: &Path,
passphrase: String,
timestamp: i64,
) -> Result<()> {
ensure!(
!context.scheduler.is_running().await,
"cannot export backup, IO is running"
);
let now = time().try_into().context("32-bit UNIX time overflow")?;
let timestamp = timestamp.try_into().context("32-bit UNIX time overflow")?;

// TODO: Maybe introduce camino crate for UTF-8 paths where we need them.
let dest = dest
.to_str()
.with_context(|| format!("path {} is not valid unicode", dest.display()))?;

context.sql.set_raw_config_int("backup_time", now).await?;
context
.sql
.set_raw_config_int("backup_time", timestamp)
.await?;
sql::housekeeping(context).await.log_err(context).ok();
context
.sql
Expand Down
3 changes: 2 additions & 1 deletion src/imex/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use crate::context::Context;
use crate::message::{Message, Viewtype};
use crate::qr::{self, Qr};
use crate::stock_str::backup_transfer_msg_body;
use crate::tools::time;
use crate::{e2ee, EventType};

use super::{export_database, DBFILE_BACKUP_NAME};
Expand Down Expand Up @@ -158,7 +159,7 @@ impl BackupProvider {
// Generate the token up front: we also use it to encrypt the database.
let token = AuthToken::generate();
context.emit_event(SendProgress::Started.into());
export_database(context, dbfile, token.to_string())
export_database(context, dbfile, token.to_string(), time())
.await
.context("Database export failed")?;
context.emit_event(SendProgress::DatabaseExported.into());
Expand Down
10 changes: 6 additions & 4 deletions src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ impl Kml {
match chrono::NaiveDateTime::parse_from_str(&val, "%Y-%m-%dT%H:%M:%SZ") {
Ok(res) => {
self.curr.timestamp = res.timestamp();
if self.curr.timestamp > time() {
self.curr.timestamp = time();
let now = time();
if self.curr.timestamp > now {
self.curr.timestamp = now;
}
}
Err(_err) => {
Expand Down Expand Up @@ -333,12 +334,13 @@ pub async fn set(context: &Context, latitude: f64, longitude: f64, accuracy: f64
return Ok(true);
}
let mut continue_streaming = false;
let now = time();

let chats = context
.sql
.query_map(
"SELECT id FROM chats WHERE locations_send_until>?;",
(time(),),
(now,),
|row| row.get::<_, i32>(0),
|chats| {
chats
Expand All @@ -356,7 +358,7 @@ pub async fn set(context: &Context, latitude: f64, longitude: f64, accuracy: f64
latitude,
longitude,
accuracy,
time(),
now,
chat_id,
ContactId::SELF,
)).await.context("Failed to store location")?;
Expand Down

0 comments on commit b0f2267

Please sign in to comment.