@@ -45,11 +45,10 @@ use dag::{DagGet, DagPut};
45
45
use either:: Either ;
46
46
use futures:: {
47
47
channel:: {
48
- mpsc:: { channel , Sender , UnboundedReceiver } ,
48
+ mpsc:: UnboundedReceiver ,
49
49
oneshot:: { self , channel as oneshot_channel, Sender as OneshotSender } ,
50
50
} ,
51
51
future:: BoxFuture ,
52
- sink:: SinkExt ,
53
52
stream:: { BoxStream , Stream } ,
54
53
FutureExt , StreamExt , TryStreamExt ,
55
54
} ;
@@ -85,7 +84,7 @@ pub use self::{
85
84
path:: IpfsPath ,
86
85
repo:: { PinKind , PinMode } ,
87
86
} ;
88
- use async_rt:: AbortableJoinHandle ;
87
+ use async_rt:: { AbortableJoinHandle , CommunicationTask } ;
89
88
use ipld_core:: cid:: Cid ;
90
89
use ipld_core:: ipld:: Ipld ;
91
90
use std:: borrow:: Borrow ;
@@ -338,9 +337,8 @@ pub struct Ipfs {
338
337
key : Keypair ,
339
338
keystore : Keystore ,
340
339
identify_conf : IdentifyConfiguration ,
341
- to_task : Sender < IpfsEvent > ,
340
+ to_task : CommunicationTask < IpfsEvent > ,
342
341
record_key_validator : HashMap < String , Arc < dyn Fn ( & str ) -> anyhow:: Result < Key > + Sync + Send > > ,
343
- _guard : AbortableJoinHandle < ( ) > ,
344
342
_gc_guard : AbortableJoinHandle < ( ) > ,
345
343
}
346
344
@@ -986,35 +984,18 @@ impl<C: NetworkBehaviour<ToSwarm = Infallible> + Send> UninitializedIpfs<C> {
986
984
}
987
985
}
988
986
989
- let mut _guard = AbortableJoinHandle :: empty ( ) ;
990
- let mut _gc_guard = AbortableJoinHandle :: empty ( ) ;
991
-
992
- let ( to_task, receiver) = channel :: < IpfsEvent > ( 1 ) ;
993
987
let id_conf = options. identify_configuration . clone ( ) ;
994
988
995
989
let keystore = options. keystore . clone ( ) ;
996
990
997
- let mut ipfs = Ipfs {
998
- span : facade_span,
999
- repo,
1000
- identify_conf : id_conf,
1001
- key : keys. clone ( ) ,
1002
- keystore,
1003
- to_task,
1004
- record_key_validator,
1005
- _guard,
1006
- _gc_guard,
1007
- } ;
1008
-
1009
991
//Note: If `All` or `Pinned` are used, we would have to auto adjust the amount of
1010
992
// provider records by adding the amount of blocks to the config.
1011
993
//TODO: Add persistent layer for kad store
1012
994
let blocks = match options. provider {
1013
995
RepoProvider :: None => vec ! [ ] ,
1014
- RepoProvider :: All => ipfs . repo . list_blocks ( ) . await . collect :: < Vec < _ > > ( ) . await ,
996
+ RepoProvider :: All => repo. list_blocks ( ) . await . collect :: < Vec < _ > > ( ) . await ,
1015
997
RepoProvider :: Pinned => {
1016
- ipfs. repo
1017
- . list_pins ( None )
998
+ repo. list_pins ( None )
1018
999
. await
1019
1000
. filter_map ( |result| futures:: future:: ready ( result. map ( |( cid, _) | cid) . ok ( ) ) )
1020
1001
. collect ( )
@@ -1047,7 +1028,7 @@ impl<C: NetworkBehaviour<ToSwarm = Infallible> + Send> UninitializedIpfs<C> {
1047
1028
let swarm = create_swarm (
1048
1029
& keys,
1049
1030
& options,
1050
- & ipfs . repo ,
1031
+ & repo,
1051
1032
exec_span,
1052
1033
( custom_behaviour, custom_transport) ,
1053
1034
) ?;
@@ -1058,7 +1039,7 @@ impl<C: NetworkBehaviour<ToSwarm = Infallible> + Send> UninitializedIpfs<C> {
1058
1039
1059
1040
let gc_handle = gc_config. map ( |config| {
1060
1041
async_rt:: task:: spawn_abortable ( {
1061
- let repo = ipfs . repo . clone ( ) ;
1042
+ let repo = repo. clone ( ) ;
1062
1043
async move {
1063
1044
let GCConfig { duration, trigger } = config;
1064
1045
let use_config_timer = duration != Duration :: ZERO ;
@@ -1127,13 +1108,7 @@ impl<C: NetworkBehaviour<ToSwarm = Infallible> + Send> UninitializedIpfs<C> {
1127
1108
} )
1128
1109
} ) . unwrap_or ( AbortableJoinHandle :: empty ( ) ) ;
1129
1110
1130
- let mut fut = task:: IpfsTask :: new (
1131
- swarm,
1132
- repo_events. fuse ( ) ,
1133
- receiver. fuse ( ) ,
1134
- & ipfs. repo ,
1135
- options. connection_event_cap ,
1136
- ) ;
1111
+ let mut fut = task:: IpfsTask :: new ( swarm, & repo, options. connection_event_cap ) ;
1137
1112
fut. swarm_event = swarm_event;
1138
1113
fut. local_external_addr = local_external_addr;
1139
1114
@@ -1163,26 +1138,33 @@ impl<C: NetworkBehaviour<ToSwarm = Infallible> + Send> UninitializedIpfs<C> {
1163
1138
}
1164
1139
}
1165
1140
1166
- let main_handle = async_rt:: task:: spawn_abortable ( {
1167
- async move {
1141
+ let main_handle = async_rt:: task:: spawn_coroutine_with_context (
1142
+ ( repo_events, swarm_span, fut) ,
1143
+ |( r_events, swarm_span, mut fut) , recv : futures:: channel:: mpsc:: Receiver < IpfsEvent > | async move {
1144
+ fut. from_facade . replace ( recv. fuse ( ) ) ;
1145
+ fut. repo_events . replace ( r_events. fuse ( ) ) ;
1168
1146
//Note: For now this is not configurable as its meant for internal testing purposes but may change in the future
1169
1147
let as_fut = false ;
1170
-
1171
1148
let fut = if as_fut {
1172
1149
fut. boxed ( )
1173
1150
} else {
1174
1151
fut. run ( ) . boxed ( )
1175
1152
} ;
1153
+ fut. instrument ( swarm_span) . await
1154
+ } ,
1155
+ ) ;
1176
1156
1177
- fut. await
1178
- }
1179
- . instrument ( swarm_span)
1180
- } ) ;
1157
+ let ipfs = Ipfs {
1158
+ span : facade_span,
1159
+ repo,
1160
+ identify_conf : id_conf,
1161
+ key : keys. clone ( ) ,
1162
+ keystore,
1163
+ to_task : main_handle,
1164
+ record_key_validator,
1165
+ _gc_guard : gc_handle,
1166
+ } ;
1181
1167
1182
- unsafe {
1183
- ipfs. _guard . replace ( main_handle) ;
1184
- ipfs. _gc_guard . replace ( gc_handle) ;
1185
- }
1186
1168
Ok ( ipfs)
1187
1169
}
1188
1170
}
@@ -2633,17 +2615,23 @@ impl Ipfs {
2633
2615
}
2634
2616
2635
2617
/// Exit daemon.
2636
- pub async fn exit_daemon ( mut self ) {
2618
+ pub async fn exit_daemon ( self ) {
2637
2619
// FIXME: this is a stopgap measure needed while repo is part of the struct Ipfs instead of
2638
2620
// the background task or stream. After that this could be handled by dropping.
2639
2621
self . repo . shutdown ( ) ;
2640
2622
2641
2623
// ignoring the error because it'd mean that the background task had already been dropped
2642
2624
let _ = self . to_task . try_send ( IpfsEvent :: Exit ) ;
2643
2625
2644
- // terminte task that handles GC and spawn task
2626
+ // TODO: Determine if we want to kill the task directly or let it gracefully close after completing all events
2627
+ // self.to_task.abort();;
2628
+
2629
+ // terminte task that handles GC
2645
2630
self . _gc_guard . abort ( ) ;
2646
- self . _guard . abort ( ) ;
2631
+
2632
+ // yield to the runtime to allow runtime to process pending tasks
2633
+ // TODO: Possibly remove along with async signature
2634
+ // tokio::task::yield_now().await;
2647
2635
}
2648
2636
}
2649
2637
0 commit comments