@@ -644,38 +644,54 @@ cfg_sync! {
644
644
645
645
let mut bg_abort: Option <std:: sync:: Arc <crate :: sync:: DropAbort >> = None ;
646
646
647
+
647
648
if let Some ( sync_interval) = sync_interval {
649
+ let ( cancel_tx, mut cancel_rx) = tokio:: sync:: oneshot:: channel:: <( ) >( ) ;
650
+
651
+ let sync_span = tracing:: debug_span!( "sync_interval" ) ;
652
+ let _enter = sync_span. enter( ) ;
653
+
648
654
let sync_ctx = db. sync_ctx. as_ref( ) . unwrap( ) . clone( ) ;
649
655
{
650
656
let mut ctx = sync_ctx. lock( ) . await ;
651
657
crate :: sync:: bootstrap_db( & mut ctx) . await ?;
658
+ tracing:: debug!( "finished bootstrap with sync interval" ) ;
652
659
}
653
660
654
661
// db.connect creates a local db file, so it is important that we always call
655
662
// `bootstrap_db` (for synced dbs) before calling connect. Otherwise, the sync
656
663
// protocol skips calling `export` endpoint causing slowdown in initial bootstrap.
657
664
let conn = db. connect( ) ?;
658
- let jh = tokio:: spawn(
665
+
666
+ tokio:: spawn(
659
667
async move {
668
+ let mut interval = tokio:: time:: interval( sync_interval) ;
669
+
660
670
loop {
661
- tracing:: trace!( "trying to sync" ) ;
662
- let mut ctx = sync_ctx. lock( ) . await ;
663
- if remote_writes {
664
- if let Err ( e) = crate :: sync:: try_pull( & mut ctx, & conn) . await {
665
- tracing:: error!( "sync error: {}" , e) ;
666
- }
667
- } else {
668
- if let Err ( e) = crate :: sync:: sync_offline( & mut ctx, & conn) . await {
669
- tracing:: error!( "sync error: {}" , e) ;
671
+ tokio:: select! {
672
+ _ = & mut cancel_rx => break ,
673
+ _ = interval. tick( ) => {
674
+ tracing:: debug!( "trying to sync" ) ;
675
+
676
+ let mut ctx = sync_ctx. lock( ) . await ;
677
+
678
+ let result = if remote_writes {
679
+ crate :: sync:: try_pull( & mut ctx, & conn) . await
680
+ } else {
681
+ crate :: sync:: sync_offline( & mut ctx, & conn) . await
682
+ } ;
683
+
684
+ if let Err ( e) = result {
685
+ tracing:: error!( "Error syncing database: {}" , e) ;
686
+ }
670
687
}
671
688
}
672
- tokio:: time:: sleep( sync_interval) . await ;
673
689
}
674
690
}
675
- . instrument( tracing:: info_span !( "sync_interval " ) ) ,
691
+ . instrument( tracing:: debug_span !( "sync interval thread " ) ) ,
676
692
) ;
677
693
678
- bg_abort. replace( std:: sync:: Arc :: new( crate :: sync:: DropAbort ( jh . abort_handle ( ) ) ) ) ;
694
+ bg_abort. replace( std:: sync:: Arc :: new( crate :: sync:: DropAbort ( Some ( cancel_tx ) ) ) ) ;
679
695
}
680
696
681
697
Ok ( Database {
0 commit comments