8151c6e
use std::future::Future;
use tokio::task_local;
task_local! {
static REQUEST_ID: String;
}
pub async fn with_request_id<F: Future>(request_id: String, future: F) -> F::Output {
REQUEST_ID.scope(request_id, future).await
}
pub fn request_id() -> Option<String> {
REQUEST_ID.try_with(Clone::clone).ok()
}
/// Prefers an explicitly passed id, then the id of the current request scope.
pub fn resolve_request_id(explicit: Option<&str>) -> String {
explicit
.filter(|value| !value.is_empty())
.map(str::to_owned)
.or_else(request_id)
.unwrap_or_default()
}