From 60c4fd1b7a07630021ea2c02386e6184453fe148 Mon Sep 17 00:00:00 2001 From: James Date: Mon, 15 Dec 2025 11:18:32 -0500 Subject: [PATCH] feat: add an IntoFuture impl for the Authenticator task --- Cargo.toml | 2 +- src/perms/oauth.rs | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0df72c4..f4f5fe7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] diff --git a/src/perms/oauth.rs b/src/perms/oauth.rs index 654cc38..30579dd 100644 --- a/src/perms/oauth.rs +++ b/src/perms/oauth.rs @@ -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; @@ -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 @@ -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 + Send>>; + + fn into_future(self) -> Self::IntoFuture { + Box::pin(self.task_future()) } }