Skip to content

[pull] main from tursodatabase:main #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions libsql/src/database/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,38 +644,54 @@ cfg_sync! {

let mut bg_abort: Option<std::sync::Arc<crate::sync::DropAbort>> = None;


if let Some(sync_interval) = sync_interval {
let (cancel_tx, mut cancel_rx) = tokio::sync::oneshot::channel::<()>();

let sync_span = tracing::debug_span!("sync_interval");
let _enter = sync_span.enter();

let sync_ctx = db.sync_ctx.as_ref().unwrap().clone();
{
let mut ctx = sync_ctx.lock().await;
crate::sync::bootstrap_db(&mut ctx).await?;
tracing::debug!("finished bootstrap with sync interval");
}

// db.connect creates a local db file, so it is important that we always call
// `bootstrap_db` (for synced dbs) before calling connect. Otherwise, the sync
// protocol skips calling `export` endpoint causing slowdown in initial bootstrap.
let conn = db.connect()?;
let jh = tokio::spawn(

tokio::spawn(
async move {
let mut interval = tokio::time::interval(sync_interval);

loop {
tracing::trace!("trying to sync");
let mut ctx = sync_ctx.lock().await;
if remote_writes {
if let Err(e) = crate::sync::try_pull(&mut ctx, &conn).await {
tracing::error!("sync error: {}", e);
}
} else {
if let Err(e) = crate::sync::sync_offline(&mut ctx, &conn).await {
tracing::error!("sync error: {}", e);
tokio::select! {
_ = &mut cancel_rx => break,
_ = interval.tick() => {
tracing::debug!("trying to sync");

let mut ctx = sync_ctx.lock().await;

let result = if remote_writes {
crate::sync::try_pull(&mut ctx, &conn).await
} else {
crate::sync::sync_offline(&mut ctx, &conn).await
};

if let Err(e) = result {
tracing::error!("Error syncing database: {}", e);
}
}
}
tokio::time::sleep(sync_interval).await;
}
}
.instrument(tracing::info_span!("sync_interval")),
.instrument(tracing::debug_span!("sync interval thread")),
);

bg_abort.replace(std::sync::Arc::new(crate::sync::DropAbort(jh.abort_handle())));
bg_abort.replace(std::sync::Arc::new(crate::sync::DropAbort(Some(cancel_tx))));
}

Ok(Database {
Expand Down
9 changes: 6 additions & 3 deletions libsql/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bytes::Bytes;
use chrono::Utc;
use http::{HeaderValue, StatusCode};
use hyper::Body;
use tokio::{io::AsyncWriteExt as _, task::AbortHandle};
use tokio::io::AsyncWriteExt as _;
use uuid::Uuid;

#[cfg(test)]
Expand Down Expand Up @@ -81,11 +81,14 @@ pub struct PushResult {
baton: Option<String>,
}

pub struct DropAbort(pub AbortHandle);
pub struct DropAbort(pub Option<tokio::sync::oneshot::Sender<()>>);

impl Drop for DropAbort {
fn drop(&mut self) {
self.0.abort();
tracing::debug!("aborting");
if let Some(sender) = self.0.take() {
let _ = sender.send(());
}
}
}

Expand Down