Skip to content

Commit 4d21829

Browse files
committed
feat: add an IntoFuture impl for the Authenticator task
1 parent d8e9c92 commit 4d21829

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/perms/oauth.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ use oauth2::{
1111
EndpointSet, HttpClientError, RefreshToken, RequestTokenError, Scope, StandardErrorResponse,
1212
StandardTokenResponse, TokenResponse, TokenUrl,
1313
};
14+
use std::{future::IntoFuture, pin::Pin};
1415
use tokio::{
1516
sync::watch::{self, Ref},
1617
task::JoinHandle,
1718
};
19+
use tracing::Instrument;
1820

1921
type Token = StandardTokenResponse<EmptyExtraTokenFields, BasicTokenType>;
2022

@@ -63,7 +65,14 @@ impl OAuthConfig {
6365

6466
/// A self-refreshing, periodically fetching authenticator for the block
6567
/// builder. This task periodically fetches a new token, and sends it to all
66-
/// active [`SharedToken`]s via a [`tokio::sync::watch`] channel..
68+
/// active [`SharedToken`]s via a [`tokio::sync::watch`] channel.
69+
///
70+
/// This task can be spawned using the [`Authenticator::spawn`] method, which
71+
/// will create a new tokio task that runs the refresh loop in the background,
72+
/// in the current [`tracing`] span. Alternately, the [`IntoFuture`]
73+
/// implementation can be used to create a future that runs the refresh loop,
74+
/// and can be isntrumented with the [`Instrument`] trait, and then spawned or
75+
/// awaited as desired.
6776
#[derive(Debug)]
6877
pub struct Authenticator {
6978
/// Configuration
@@ -173,7 +182,17 @@ impl Authenticator {
173182
/// interval may be configured via the
174183
/// [`OAuthConfig::oauth_token_refresh_interval`] property.
175184
pub fn spawn(self) -> JoinHandle<()> {
176-
tokio::spawn(self.task_future())
185+
tokio::spawn(self.task_future().in_current_span())
186+
}
187+
}
188+
189+
impl IntoFuture for Authenticator {
190+
type Output = ();
191+
192+
type IntoFuture = Pin<Box<dyn std::future::Future<Output = ()> + Send>>;
193+
194+
fn into_future(self) -> Self::IntoFuture {
195+
Box::pin(self.task_future())
177196
}
178197
}
179198

0 commit comments

Comments
 (0)