@@ -132,14 +132,23 @@ pub struct IpfsOptions {
132
132
/// Enables mdns for peer discovery and announcement when true.
133
133
pub mdns : bool ,
134
134
135
- /// Custom Kademlia protocol name.
135
+ /// Custom Kademlia protocol name. When set to `None`, the global DHT name is used instead of
136
+ /// the LAN dht name.
136
137
///
137
138
/// The name given here is passed to [`libp2p_kad::KademliaConfig::set_protocol_name`].
138
139
///
139
140
/// [`libp2p_kad::KademliaConfig::set_protocol_name`]: https://docs.rs/libp2p-kad/*/libp2p_kad/struct.KademliaConfig.html##method.set_protocol_name
140
141
pub kad_protocol : Option < String > ,
142
+
141
143
/// Bound listening addresses; by default the node will not listen on any address.
142
144
pub listening_addrs : Vec < Multiaddr > ,
145
+
146
+ /// The span for tracing purposes, `None` value is converted to `tracing::trace_span!("ipfs")`.
147
+ ///
148
+ /// All futures returned by `Ipfs`, background task actions and swarm actions are instrumented
149
+ /// with this span or spans referring to this as their parent. Setting this other than `None`
150
+ /// default is useful when running multiple nodes.
151
+ pub span : Option < Span > ,
143
152
}
144
153
145
154
impl fmt:: Debug for IpfsOptions {
@@ -153,6 +162,7 @@ impl fmt::Debug for IpfsOptions {
153
162
. field ( "mdns" , & self . mdns )
154
163
. field ( "kad_protocol" , & self . kad_protocol )
155
164
. field ( "listening_addrs" , & self . listening_addrs )
165
+ . field ( "span" , & self . span )
156
166
. finish ( )
157
167
}
158
168
}
@@ -170,6 +180,7 @@ impl IpfsOptions {
170
180
// default to lan kad for go-ipfs use in tests
171
181
kad_protocol : Some ( "/ipfs/lan/kad/1.0.0" . to_owned ( ) ) ,
172
182
listening_addrs : vec ! [ "/ip4/127.0.0.1/tcp/0" . parse( ) . unwrap( ) ] ,
183
+ span : None ,
173
184
}
174
185
}
175
186
}
@@ -205,6 +216,7 @@ impl IpfsOptions {
205
216
mdns : bool ,
206
217
kad_protocol : Option < String > ,
207
218
listening_addrs : Vec < Multiaddr > ,
219
+ span : Option < Span > ,
208
220
) -> Self {
209
221
Self {
210
222
ipfs_path,
@@ -213,6 +225,7 @@ impl IpfsOptions {
213
225
mdns,
214
226
kad_protocol,
215
227
listening_addrs,
228
+ span,
216
229
}
217
230
}
218
231
}
@@ -263,6 +276,7 @@ impl Default for IpfsOptions {
263
276
mdns : true ,
264
277
kad_protocol : None ,
265
278
listening_addrs : Vec :: new ( ) ,
279
+ span : None ,
266
280
}
267
281
}
268
282
}
@@ -275,24 +289,24 @@ impl Default for IpfsOptions {
275
289
///
276
290
/// The facade is created through [`UninitializedIpfs`] which is configured with [`IpfsOptions`].
277
291
#[ derive( Debug ) ]
278
- pub struct Ipfs < Types : IpfsTypes > ( Arc < IpfsInner < Types > > ) ;
292
+ pub struct Ipfs < Types : IpfsTypes > {
293
+ span : Span ,
294
+ repo : Arc < Repo < Types > > ,
295
+ keys : DebuggableKeypair < Keypair > ,
296
+ to_task : Sender < IpfsEvent > ,
297
+ }
279
298
280
299
impl < Types : IpfsTypes > Clone for Ipfs < Types > {
281
300
fn clone ( & self ) -> Self {
282
- Ipfs ( Arc :: clone ( & self . 0 ) )
301
+ Ipfs {
302
+ span : self . span . clone ( ) ,
303
+ repo : Arc :: clone ( & self . repo ) ,
304
+ keys : self . keys . clone ( ) ,
305
+ to_task : self . to_task . clone ( ) ,
306
+ }
283
307
}
284
308
}
285
309
286
- /// The internal shared implementation of [`Ipfs`].
287
- #[ derive( Debug ) ]
288
- #[ doc( hidden) ]
289
- pub struct IpfsInner < Types : IpfsTypes > {
290
- pub span : Span ,
291
- repo : Repo < Types > ,
292
- keys : DebuggableKeypair < Keypair > ,
293
- to_task : Sender < IpfsEvent > ,
294
- }
295
-
296
310
type Channel < T > = OneshotSender < Result < T , Error > > ;
297
311
298
312
/// Events used internally to communicate with the swarm, which is executed in the the background
@@ -355,8 +369,7 @@ enum IpfsEvent {
355
369
356
370
/// Configured Ipfs which can only be started.
357
371
pub struct UninitializedIpfs < Types : IpfsTypes > {
358
- repo : Repo < Types > ,
359
- span : Span ,
372
+ repo : Arc < Repo < Types > > ,
360
373
keys : Keypair ,
361
374
options : IpfsOptions ,
362
375
repo_events : Receiver < RepoEvent > ,
@@ -369,23 +382,21 @@ impl<Types: IpfsTypes> UninitializedIpfs<Types> {
369
382
/// The span is attached to all operations called on the later created `Ipfs` along with all
370
383
/// operations done in the background task as well as tasks spawned by the underlying
371
384
/// `libp2p::Swarm`.
372
- pub async fn new ( options : IpfsOptions , span : Option < Span > ) -> Self {
385
+ pub fn new ( options : IpfsOptions ) -> Self {
373
386
let repo_options = RepoOptions :: from ( & options) ;
374
387
let ( repo, repo_events) = create_repo ( repo_options) ;
375
388
let keys = options. keypair . clone ( ) ;
376
- let span = span. unwrap_or_else ( || trace_span ! ( "ipfs" ) ) ;
377
389
378
390
UninitializedIpfs {
379
- repo,
380
- span,
391
+ repo : Arc :: new ( repo) ,
381
392
keys,
382
393
options,
383
394
repo_events,
384
395
}
385
396
}
386
397
387
- pub async fn default ( ) -> Self {
388
- Self :: new ( IpfsOptions :: default ( ) , None ) . await
398
+ pub fn default ( ) -> Self {
399
+ Self :: new ( IpfsOptions :: default ( ) )
389
400
}
390
401
391
402
/// Initialize the ipfs node. The returned `Ipfs` value is cloneable, send and sync, and the
@@ -395,25 +406,31 @@ impl<Types: IpfsTypes> UninitializedIpfs<Types> {
395
406
396
407
let UninitializedIpfs {
397
408
repo,
398
- span,
399
409
keys,
400
410
repo_events,
401
- options,
411
+ mut options,
402
412
} = self ;
403
413
404
414
repo. init ( ) . await ?;
405
415
406
416
let ( to_task, receiver) = channel :: < IpfsEvent > ( 1 ) ;
407
417
408
- let ipfs = Ipfs ( Arc :: new ( IpfsInner {
409
- span,
410
- repo,
418
+ let facade_span = options
419
+ . span
420
+ . take ( )
421
+ . unwrap_or_else ( || tracing:: trace_span!( "ipfs" ) ) ;
422
+
423
+ let swarm_span = tracing:: trace_span!( parent: facade_span. clone( ) , "swarm" ) ;
424
+
425
+ let ipfs = Ipfs {
426
+ span : facade_span,
427
+ repo : repo. clone ( ) ,
411
428
keys : DebuggableKeypair ( keys) ,
412
429
to_task,
413
- } ) ) ;
430
+ } ;
414
431
415
432
let swarm_options = SwarmOptions :: from ( & options) ;
416
- let swarm = create_swarm ( swarm_options, ipfs . clone ( ) ) . await ?;
433
+ let swarm = create_swarm ( swarm_options, swarm_span , repo ) . await ?;
417
434
418
435
let IpfsOptions {
419
436
listening_addrs, ..
@@ -434,14 +451,6 @@ impl<Types: IpfsTypes> UninitializedIpfs<Types> {
434
451
}
435
452
}
436
453
437
- impl < Types : IpfsTypes > Deref for Ipfs < Types > {
438
- type Target = IpfsInner < Types > ;
439
-
440
- fn deref ( & self ) -> & Self :: Target {
441
- & self . 0
442
- }
443
- }
444
-
445
454
impl < Types : IpfsTypes > Ipfs < Types > {
446
455
/// Return an [`IpldDag`] for DAG operations
447
456
pub fn dag ( & self ) -> IpldDag < Types > {
@@ -1243,13 +1252,13 @@ impl<Types: IpfsTypes> Ipfs<Types> {
1243
1252
}
1244
1253
1245
1254
/// Exit daemon.
1246
- pub async fn exit_daemon ( self ) {
1255
+ pub async fn exit_daemon ( mut self ) {
1247
1256
// FIXME: this is a stopgap measure needed while repo is part of the struct Ipfs instead of
1248
1257
// the background task or stream. After that this could be handled by dropping.
1249
1258
self . repo . shutdown ( ) ;
1250
1259
1251
1260
// ignoring the error because it'd mean that the background task had already been dropped
1252
- let _ = self . to_task . clone ( ) . try_send ( IpfsEvent :: Exit ) ;
1261
+ let _ = self . to_task . try_send ( IpfsEvent :: Exit ) ;
1253
1262
}
1254
1263
}
1255
1264
@@ -1675,10 +1684,9 @@ mod node {
1675
1684
1676
1685
impl Node {
1677
1686
pub async fn new < T : AsRef < str > > ( name : T ) -> Self {
1678
- let opts = IpfsOptions :: inmemory_with_generated_keys ( ) ;
1679
- Node :: with_options ( opts)
1680
- . instrument ( trace_span ! ( "ipfs" , node = name. as_ref( ) ) )
1681
- . await
1687
+ let mut opts = IpfsOptions :: inmemory_with_generated_keys ( ) ;
1688
+ opts. span = Some ( trace_span ! ( "ipfs" , node = name. as_ref( ) ) ) ;
1689
+ Self :: with_options ( opts) . await
1682
1690
}
1683
1691
1684
1692
pub async fn connect ( & self , addr : Multiaddr ) -> Result < ( ) , Error > {
@@ -1687,12 +1695,9 @@ mod node {
1687
1695
}
1688
1696
1689
1697
pub async fn with_options ( opts : IpfsOptions ) -> Self {
1690
- let span = Some ( Span :: current ( ) ) ;
1691
1698
let id = opts. keypair . public ( ) . into_peer_id ( ) ;
1692
1699
1693
- let ( ipfs, fut) : ( Ipfs < TestTypes > , _ ) = UninitializedIpfs :: new ( opts, span)
1694
- . in_current_span ( )
1695
- . await
1700
+ let ( ipfs, fut) : ( Ipfs < TestTypes > , _ ) = UninitializedIpfs :: new ( opts)
1696
1701
. start ( )
1697
1702
. in_current_span ( )
1698
1703
. await
0 commit comments