Skip to content

Commit

Permalink
30s -> 5mins
Browse files Browse the repository at this point in the history
  • Loading branch information
ellie committed May 6, 2024
1 parent 6412adc commit 6addbb5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions crates/atuin-client/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,14 @@ pub struct Preview {

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Daemon {
/// Use the daemon to sync
/// If enabled, requires a running daemon with `atuin daemon`
pub enabled: bool,

/// The daemon will handle sync on an interval. How often to sync, in seconds.
pub sync_frequency: u64,

/// The path to the unix socket used by the daemon
pub socket_path: String,
}

Expand All @@ -360,6 +367,7 @@ impl Default for Daemon {
fn default() -> Self {
Self {
enabled: false,
sync_frequency: 300,
socket_path: "".to_string(),
}
}
Expand Down Expand Up @@ -693,6 +701,7 @@ impl Settings {
.set_default("keymap_cursor", HashMap::<String, String>::new())?
.set_default("smart_sort", false)?
.set_default("store_failed", true)?
.set_default("daemon.sync_frequency", 300)?
.set_default(
"prefers_reduced_motion",
std::env::var("NO_MOTION")
Expand Down
1 change: 1 addition & 0 deletions crates/atuin-daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ tonic = "0.11"
prost = "0.12"
prost-types = "0.12"
tokio-stream = {version="0.1.14", features=["net"]}
rand.workspace = true

[build-dependencies]
tonic-build = "0.11"
44 changes: 34 additions & 10 deletions crates/atuin-daemon/src/server/sync.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use eyre::{Result, WrapErr};
use eyre::Result;
use rand::Rng;
use tokio::time::{self, MissedTickBehavior};

use atuin_client::{
encryption,
history::store::HistoryStore,
record::{sqlite_store::SqliteStore, sync},
settings::Settings,
};

pub async fn worker(settings: Settings, store: SqliteStore) -> Result<()> {
tracing::info!("booting sync worker");
let mut ticker = time::interval(time::Duration::from_secs(5));

let mut ticker = time::interval(time::Duration::from_secs(settings.daemon.sync_frequency));

// IMPORTANT: without this, if we miss ticks because a sync takes ages or is otherwise delayed,
// we may end up running a lot of syncs in a hot loop. No bueno!
Expand All @@ -20,12 +20,36 @@ pub async fn worker(settings: Settings, store: SqliteStore) -> Result<()> {
ticker.tick().await;
tracing::info!("sync worker tick");

let (uploaded, downloaded) = sync::sync(&settings, &store).await?;
let res = sync::sync(&settings, &store).await;

if let Err(e) = res {
tracing::error!("sync tick failed with {e}");
let mut rng = rand::thread_rng();

let new_interval = ticker.period().as_secs_f64() * rng.gen_range(2.0..2.2);

// Don't backoff by more than 2hrs
if new_interval > 2.0 * 60.0 * 60.0 {
continue;
}

ticker = time::interval(time::Duration::from_secs(new_interval as u64));
ticker.reset_after(time::Duration::from_secs(new_interval as u64));

tracing::error!("backing off, next sync tick in {new_interval}");
} else {
let (uploaded, downloaded) = res.unwrap();

tracing::info!(
uploaded = ?uploaded,
downloaded = ?downloaded,
"sync complete"
);

tracing::info!(
uploaded = ?uploaded,
downloaded = ?downloaded,
"sync complete"
);
// Reset backoff on success
if ticker.period().as_secs() != settings.daemon.sync_frequency {
ticker = time::interval(time::Duration::from_secs(settings.daemon.sync_frequency));
}
}
}
}

0 comments on commit 6addbb5

Please sign in to comment.