@@ -43,6 +43,7 @@ use spacetimedb_lib::ConnectionId;
4343use spacetimedb_lib:: Identity ;
4444use spacetimedb_paths:: server:: { ReplicaDir , SnapshotsPath } ;
4545use spacetimedb_primitives:: * ;
46+ use spacetimedb_runtime:: sync:: watch;
4647use spacetimedb_runtime:: Handle ;
4748use spacetimedb_sats:: memory_usage:: MemoryUsage ;
4849use spacetimedb_sats:: raw_identifier:: RawIdentifier ;
@@ -62,7 +63,6 @@ use std::borrow::Cow;
6263use std:: io;
6364use std:: ops:: RangeBounds ;
6465use std:: sync:: Arc ;
65- use tokio:: sync:: watch;
6666
6767pub use super :: persistence:: { DiskSizeFn , Durability , Persistence } ;
6868pub use super :: snapshot:: SnapshotWorker ;
@@ -1079,7 +1079,10 @@ const VIEWS_EXPIRATION: std::time::Duration = std::time::Duration::from_secs(10
10791079const VIEW_CLEANUP_BUDGET : std:: time:: Duration = std:: time:: Duration :: from_millis ( 10 ) ;
10801080
10811081/// Spawn a background task that periodically cleans up expired views
1082- pub fn spawn_view_cleanup_loop ( db : Arc < RelationalDB > ) -> tokio:: task:: AbortHandle {
1082+ pub fn spawn_view_cleanup_loop (
1083+ db : Arc < RelationalDB > ,
1084+ handle : & spacetimedb_runtime:: Handle ,
1085+ ) -> spacetimedb_runtime:: AbortHandle {
10831086 fn run_view_cleanup ( db : & RelationalDB ) {
10841087 match db. with_auto_commit ( Workload :: Internal , |tx| {
10851088 tx. clear_expired_views ( VIEWS_EXPIRATION , VIEW_CLEANUP_BUDGET )
@@ -1106,23 +1109,19 @@ pub fn spawn_view_cleanup_loop(db: Arc<RelationalDB>) -> tokio::task::AbortHandl
11061109 }
11071110 }
11081111
1109- tokio:: spawn ( async move {
1110- loop {
1111- // Offload actual cleanup to blocking thread pool, as `VIEW_CLEANUP_BUDGET` is defined
1112- // in milliseconds, which may be too long for async tasks.
1113- let db = db. clone ( ) ;
1114- let db_identity = db. database_identity ( ) ;
1115- tokio:: task:: spawn_blocking ( move || run_view_cleanup ( & db) )
1116- . await
1117- . inspect_err ( |e| {
1118- log:: error!( "[{}] DATABASE: failed to run view cleanup task: {}" , db_identity, e) ;
1119- } )
1120- . ok ( ) ;
1112+ let handle_clone = handle. clone ( ) ;
1113+ handle
1114+ . spawn ( async move {
1115+ loop {
1116+ // Offload actual cleanup to blocking thread pool, as `VIEW_CLEANUP_BUDGET` is defined
1117+ // in milliseconds, which may be too long for async tasks.
1118+ let db = db. clone ( ) ;
1119+ handle_clone. spawn_blocking ( move || run_view_cleanup ( & db) ) . await ;
11211120
1122- tokio :: time :: sleep ( VIEWS_EXPIRATION ) . await ;
1123- }
1124- } )
1125- . abort_handle ( )
1121+ handle_clone . sleep ( VIEWS_EXPIRATION ) . await ;
1122+ }
1123+ } )
1124+ . abort_handle ( )
11261125}
11271126impl RelationalDB {
11281127 pub fn create_table ( & self , tx : & mut MutTx , schema : TableSchema ) -> Result < TableId , DBError > {
@@ -1753,8 +1752,8 @@ pub async fn local_history(
17531752/// Suitable **only** for non-replicated databases.
17541753pub async fn snapshot_watching_commitlog_compressor (
17551754 mut snapshot_rx : watch:: Receiver < u64 > ,
1756- mut clog_tx : Option < tokio :: sync:: mpsc:: Sender < u64 > > ,
1757- mut snap_tx : Option < tokio :: sync:: mpsc:: Sender < u64 > > ,
1755+ mut clog_tx : Option < spacetimedb_runtime :: sync:: mpsc:: Sender < u64 > > ,
1756+ mut snap_tx : Option < spacetimedb_runtime :: sync:: mpsc:: Sender < u64 > > ,
17581757 durability : LocalDurability ,
17591758 runtime : Handle ,
17601759) {
0 commit comments