@@ -4,7 +4,6 @@ use std::{
44 collections:: { hash_map, HashMap } ,
55 num:: NonZeroU64 ,
66 sync:: Arc ,
7- thread:: JoinHandle ,
87 time:: Duration ,
98} ;
109
@@ -18,6 +17,9 @@ use serde::{Deserialize, Serialize};
1817use tokio:: sync:: oneshot;
1918use tracing:: { debug, error, error_span, trace, warn} ;
2019
20+ #[ cfg( wasm_browser) ]
21+ use tracing:: Instrument ;
22+
2123use crate :: {
2224 api:: {
2325 protocol:: { AuthorListResponse , ListResponse } ,
@@ -228,6 +230,7 @@ struct OpenReplica {
228230pub struct SyncHandle {
229231 tx : async_channel:: Sender < Action > ,
230232 #[ cfg( wasm_browser) ]
233+ #[ allow( unused) ]
231234 join_handle : Arc < Option < n0_future:: task:: JoinHandle < ( ) > > > ,
232235 #[ cfg( not( wasm_browser) ) ]
233236 join_handle : Arc < Option < std:: thread:: JoinHandle < ( ) > > > ,
@@ -275,21 +278,22 @@ impl SyncHandle {
275278 metrics : metrics. clone ( ) ,
276279 } ;
277280
281+ let span = error_span ! ( "sync" , %me) ;
278282 #[ cfg( wasm_browser) ]
279- let join_handle = n0_future:: task:: spawn ( actor. run_async ( ) ) ;
283+ let join_handle = n0_future:: task:: spawn ( actor. run_async ( ) . instrument ( span ) ) ;
280284
281285 #[ cfg( not( wasm_browser) ) ]
282286 let join_handle = std:: thread:: Builder :: new ( )
283287 . name ( "sync-actor" . to_string ( ) )
284288 . spawn ( move || {
285- let span = error_span ! ( "sync" , %me) ;
286289 let _enter = span. enter ( ) ;
287290
288291 if let Err ( err) = actor. run_in_thread ( ) {
289292 error ! ( "Sync actor closed with error: {err:?}" ) ;
290293 }
291294 } )
292295 . expect ( "failed to spawn thread" ) ;
296+
293297 let join_handle = Arc :: new ( Some ( join_handle) ) ;
294298 SyncHandle {
295299 tx : action_tx,
@@ -602,15 +606,22 @@ impl SyncHandle {
602606
603607impl Drop for SyncHandle {
604608 fn drop ( & mut self ) {
609+ #[ cfg( wasm_browser) ]
610+ {
611+ let tx = self . tx . clone ( ) ;
612+ n0_future:: task:: spawn ( async move {
613+ tx. send ( Action :: Shutdown { reply : None } ) . await . ok ( ) ;
614+ } ) ;
615+ }
605616 // this means we're dropping the last reference
617+ #[ cfg( not( wasm_browser) ) ]
606618 if let Some ( handle) = Arc :: get_mut ( & mut self . join_handle ) {
607619 // this call is the reason tx can not be a tokio mpsc channel.
608620 // we have no control about where drop is called, yet tokio send_blocking panics
609621 // when called from inside a tokio runtime.
610622 self . tx . send_blocking ( Action :: Shutdown { reply : None } ) . ok ( ) ;
611623 let handle = handle. take ( ) . expect ( "this can only run once" ) ;
612624
613- #[ cfg( not( wasm_browser) ) ]
614625 if let Err ( err) = handle. join ( ) {
615626 warn ! ( ?err, "Failed to join sync actor" ) ;
616627 }
@@ -628,10 +639,7 @@ struct Actor {
628639}
629640
630641impl Actor {
631- async fn run_in_task ( self ) {
632- self . run_async ( ) . await
633- }
634-
642+ #[ cfg( not( wasm_browser) ) ]
635643 fn run_in_thread ( self ) -> Result < ( ) > {
636644 let rt = tokio:: runtime:: Builder :: new_current_thread ( )
637645 . enable_time ( )
@@ -779,37 +787,46 @@ impl Actor {
779787 hash,
780788 len,
781789 reply,
782- } => send_reply_with ( reply, self , move |this| {
783- let author = get_author ( & mut this. store , & author) ?;
784- let mut replica = this. states . replica ( namespace, & mut this. store ) ?;
785- replica. insert ( & key, & author, hash, len) ?;
786- this. metrics . new_entries_local . inc ( ) ;
787- this. metrics . new_entries_local_size . inc_by ( len) ;
788- Ok ( ( ) )
789- } ) ,
790+ } => {
791+ send_reply_with_async ( reply, self , async move |this| {
792+ let author = get_author ( & mut this. store , & author) ?;
793+ let mut replica = this. states . replica ( namespace, & mut this. store ) ?;
794+ replica. insert ( & key, & author, hash, len) . await ?;
795+ this. metrics . new_entries_local . inc ( ) ;
796+ this. metrics . new_entries_local_size . inc_by ( len) ;
797+ Ok ( ( ) )
798+ } )
799+ . await
800+ }
790801 ReplicaAction :: DeletePrefix { author, key, reply } => {
791- send_reply_with ( reply, self , |this| {
802+ send_reply_with_async ( reply, self , async |this| {
792803 let author = get_author ( & mut this. store , & author) ?;
793804 let mut replica = this. states . replica ( namespace, & mut this. store ) ?;
794- let res = replica. delete_prefix ( & key, & author) ?;
805+ let res = replica. delete_prefix ( & key, & author) . await ?;
795806 Ok ( res)
796807 } )
808+ . await
797809 }
798810 ReplicaAction :: InsertRemote {
799811 entry,
800812 from,
801813 content_status,
802814 reply,
803- } => send_reply_with ( reply, self , move |this| {
804- let mut replica = this
805- . states
806- . replica_if_syncing ( & namespace, & mut this. store ) ?;
807- let len = entry. content_len ( ) ;
808- replica. insert_remote_entry ( entry, from, content_status) ?;
809- this. metrics . new_entries_remote . inc ( ) ;
810- this. metrics . new_entries_remote_size . inc_by ( len) ;
811- Ok ( ( ) )
812- } ) ,
815+ } => {
816+ send_reply_with_async ( reply, self , async move |this| {
817+ let mut replica = this
818+ . states
819+ . replica_if_syncing ( & namespace, & mut this. store ) ?;
820+ let len = entry. content_len ( ) ;
821+ replica
822+ . insert_remote_entry ( entry, from, content_status)
823+ . await ?;
824+ this. metrics . new_entries_remote . inc ( ) ;
825+ this. metrics . new_entries_remote_size . inc_by ( len) ;
826+ Ok ( ( ) )
827+ } )
828+ . await
829+ }
813830
814831 ReplicaAction :: SyncInitialMessage { reply } => {
815832 send_reply_with ( reply, self , move |this| {
@@ -1064,6 +1081,14 @@ fn send_reply_with<T>(
10641081 sender. send ( f ( this) ) . map_err ( send_reply_error)
10651082}
10661083
1084+ async fn send_reply_with_async < T > (
1085+ sender : oneshot:: Sender < Result < T > > ,
1086+ this : & mut Actor ,
1087+ f : impl AsyncFnOnce ( & mut Actor ) -> Result < T > ,
1088+ ) -> Result < ( ) , SendReplyError > {
1089+ sender. send ( f ( this) . await ) . map_err ( send_reply_error)
1090+ }
1091+
10671092fn send_reply_error < T > ( _err : T ) -> SendReplyError {
10681093 SendReplyError
10691094}
0 commit comments