Skip to content

Commit

Permalink
make AggregatorHandle abort explcit
Browse files Browse the repository at this point in the history
Instead of aborting the aggregator task upon dropping the
`AggregatorHandle`, we now provide an explicit `abort` method. If the
user chooses, they can discard the aggregator handle and while they will
lose the possibility to clean up the aggregator task, they will be
otherwise unaffected.
  • Loading branch information
hds committed Jul 24, 2023
1 parent 1d0213d commit 95937c5
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions console-subscriber/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,8 @@ impl Server {
/// tokio::spawn(serve);
/// # // Avoid a warning that `console_layer` and `aggregator_handle` are unused.
/// # drop(console_layer);
/// # drop(aggregator_handle);
/// # let mut aggregator_handle = aggregator_handle;
/// # aggregator_handle.abort();
/// # });
/// ```
///
Expand Down Expand Up @@ -1031,8 +1032,8 @@ impl Server {
/// The `InstrumentServer<Server>` can be used to construct a router which
/// can be added to a [`tonic`] gRPC server.
///
/// The [`AggregatorHandle`] must be kept until after the server has been
/// shut down.
/// The [`AggregatorHandle`] can be used to abort the associated aggregator task
/// after the server has been shut down.
///
/// See the [`Server::into_parts`] documentation for usage.
#[non_exhaustive]
Expand All @@ -1050,15 +1051,32 @@ pub struct ServerParts {

/// Aggregator handle.
///
/// This object is returned from [`Server::into_parts`] and must be
/// kept as long as the `InstrumentServer<Server>` - which is also
/// returned - is in use.
/// This object is returned from [`Server::into_parts`]. It can be
/// used to abort the aggregator task.
///
/// The aggregator collects the traces that implement the async runtime
/// being observed and prepares them to be served by the gRPC server.
///
/// Normally, if the server, started with [`Server::serve`] or
/// [`Server::serve_with`] stops for any reason, the aggregator is aborted,
/// hoewver, if the server was started with the [`InstrumentServer`] returned
/// from [`Server::into_parts`], then it is the responsibility of the user
/// of the API to stop the aggregator task by calling [`abort`] on this
/// object.
///
/// [`abort`]: fn@crate::Aggregator::abort
pub struct AggregatorHandle {
join_handle: JoinHandle<()>,
}

impl Drop for AggregatorHandle {
fn drop(&mut self) {
impl AggregatorHandle {
/// Aborts the task running this aggregator.
///
/// To avoid having a disconnected aggregator running forever, this
/// method should be called when the [`tonic::transport::Server`] started
/// with the [`InstrumentServer`] also returned from [`Server::into_parts`]
/// stops running.
pub fn abort(&mut self) {
self.join_handle.abort();
}
}
Expand Down

0 comments on commit 95937c5

Please sign in to comment.