Skip to content

Commit 05b9f5b

Browse files
authored
feat(subscriber): name tasks spawned by the console subscriber (console-rs#117)
This branch updates the `console-subscriber` crate so that the "aggregate" and "serve" tasks spawned by the console are spawned with names. This allows the user to distinguish between tasks spawned by the console subscriber and tasks spawned by other parts of the application. Note that this does *not* include the tasks spawned by Tonic to handle each accepted client connection. Those tasks are spawned inside of Tonic, rather than by the console-subscriber crate, so we can't easily add our own names to them. We could probably fix that by using Tonic's lower level APIs to spawn our own per-connection tasks, and give them names...but that seems like a follow-up PR. This is a first pass on console-rs#109 (although it isn't a *complete* solution due to the above issue with Tonic). Screenshot: ![image](https://user-images.githubusercontent.com/2796466/132248558-8417f2c8-317b-4d2a-85a9-c851dacba587.png)
1 parent 0685482 commit 05b9f5b

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

console-subscriber/src/lib.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -548,16 +548,16 @@ impl Server {
548548
.aggregator
549549
.take()
550550
.expect("cannot start server multiple times");
551-
let aggregate = tokio::spawn(aggregate.run());
551+
let aggregate = spawn_named(aggregate.run(), "console::aggregate");
552552
let addr = self.addr;
553-
let res = builder
553+
let serve = builder
554554
.add_service(proto::instrument::instrument_server::InstrumentServer::new(
555555
self,
556556
))
557-
.serve(addr)
558-
.await;
557+
.serve(addr);
558+
let res = spawn_named(serve, "console::serve").await;
559559
aggregate.abort();
560-
res.map_err(Into::into)
560+
res?.map_err(Into::into)
561561
}
562562
}
563563

@@ -650,3 +650,18 @@ impl WakeOp {
650650
}
651651
}
652652
}
653+
654+
#[track_caller]
655+
pub(crate) fn spawn_named<T>(
656+
task: impl std::future::Future<Output = T> + Send + 'static,
657+
_name: &str,
658+
) -> tokio::task::JoinHandle<T>
659+
where
660+
T: Send + 'static,
661+
{
662+
#[cfg(tokio_unstable)]
663+
return tokio::task::Builder::new().name(_name).spawn(task);
664+
665+
#[cfg(not(tokio_unstable))]
666+
tokio::spawn(task)
667+
}

0 commit comments

Comments
 (0)