Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "init4-bin-base"

description = "Internal utilities for binaries produced by the init4 team"
keywords = ["init4", "bin", "base"]
version = "0.18.0-rc.1"
version = "0.18.0-rc.2"
edition = "2021"
rust-version = "1.85"
authors = ["init4", "James Prestwich", "evalir"]
Expand Down
23 changes: 21 additions & 2 deletions src/perms/oauth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ use oauth2::{
EndpointSet, HttpClientError, RefreshToken, RequestTokenError, Scope, StandardErrorResponse,
StandardTokenResponse, TokenResponse, TokenUrl,
};
use std::{future::IntoFuture, pin::Pin};
use tokio::{
sync::watch::{self, Ref},
task::JoinHandle,
};
use tracing::Instrument;

type Token = StandardTokenResponse<EmptyExtraTokenFields, BasicTokenType>;

Expand Down Expand Up @@ -63,7 +65,14 @@ impl OAuthConfig {

/// A self-refreshing, periodically fetching authenticator for the block
/// builder. This task periodically fetches a new token, and sends it to all
/// active [`SharedToken`]s via a [`tokio::sync::watch`] channel..
/// active [`SharedToken`]s via a [`tokio::sync::watch`] channel.
///
/// This task can be spawned using the [`Authenticator::spawn`] method, which
/// will create a new tokio task that runs the refresh loop in the background,
/// in the current [`tracing`] span. Alternately, the [`IntoFuture`]
/// implementation can be used to create a future that runs the refresh loop,
/// and can be isntrumented with the [`Instrument`] trait, and then spawned or
/// awaited as desired.
#[derive(Debug)]
pub struct Authenticator {
/// Configuration
Expand Down Expand Up @@ -173,7 +182,17 @@ impl Authenticator {
/// interval may be configured via the
/// [`OAuthConfig::oauth_token_refresh_interval`] property.
pub fn spawn(self) -> JoinHandle<()> {
tokio::spawn(self.task_future())
tokio::spawn(self.task_future().in_current_span())
}
}

impl IntoFuture for Authenticator {
type Output = ();

type IntoFuture = Pin<Box<dyn std::future::Future<Output = ()> + Send>>;

fn into_future(self) -> Self::IntoFuture {
Box::pin(self.task_future())
}
}

Expand Down