2023-09-05 02:51:27 +00:00
|
|
|
use std::{
|
|
|
|
future::Future,
|
2023-09-30 21:26:43 +00:00
|
|
|
sync::{Arc, OnceLock},
|
2023-09-05 02:51:27 +00:00
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
|
|
|
|
2023-09-30 21:26:43 +00:00
|
|
|
static NOOP_WAKER: OnceLock<std::task::Waker> = OnceLock::new();
|
|
|
|
|
|
|
|
fn noop_waker() -> &'static std::task::Waker {
|
|
|
|
NOOP_WAKER.get_or_init(|| std::task::Waker::from(Arc::new(NoopWaker)))
|
|
|
|
}
|
|
|
|
|
|
|
|
struct NoopWaker;
|
|
|
|
impl std::task::Wake for NoopWaker {
|
|
|
|
fn wake(self: std::sync::Arc<Self>) {}
|
|
|
|
fn wake_by_ref(self: &std::sync::Arc<Self>) {}
|
|
|
|
}
|
|
|
|
|
2023-09-05 02:51:27 +00:00
|
|
|
pub(crate) type LocalBoxFuture<'a, T> = std::pin::Pin<Box<dyn Future<Output = T> + 'a>>;
|
|
|
|
|
2023-09-30 21:26:43 +00:00
|
|
|
pub(crate) trait NowOrNever: Future {
|
|
|
|
fn now_or_never(self) -> Option<Self::Output>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
let fut = std::pin::pin!(self);
|
|
|
|
|
|
|
|
let mut cx = std::task::Context::from_waker(noop_waker());
|
|
|
|
|
|
|
|
match fut.poll(&mut cx) {
|
|
|
|
std::task::Poll::Pending => None,
|
|
|
|
std::task::Poll::Ready(out) => Some(out),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 02:51:27 +00:00
|
|
|
pub(crate) trait WithTimeout: Future {
|
2023-10-21 00:08:11 +00:00
|
|
|
fn with_timeout(self, duration: Duration) -> tokio::time::Timeout<Self>
|
2023-09-05 02:51:27 +00:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2023-10-21 00:08:11 +00:00
|
|
|
tokio::time::timeout(duration, self)
|
2023-09-05 02:51:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) trait WithMetrics: Future {
|
|
|
|
fn with_metrics(self, name: &'static str) -> MetricsFuture<Self>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
MetricsFuture {
|
|
|
|
future: self,
|
|
|
|
metrics: Metrics {
|
|
|
|
name,
|
|
|
|
start: Instant::now(),
|
|
|
|
complete: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-09 18:15:23 +00:00
|
|
|
pub(crate) trait WithPollTimer: Future {
|
|
|
|
fn with_poll_timer(self, name: &'static str) -> PollTimer<Self>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
PollTimer { name, inner: self }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-30 21:26:43 +00:00
|
|
|
impl<F> NowOrNever for F where F: Future {}
|
2023-09-05 02:51:27 +00:00
|
|
|
impl<F> WithMetrics for F where F: Future {}
|
|
|
|
impl<F> WithTimeout for F where F: Future {}
|
2024-03-09 18:15:23 +00:00
|
|
|
impl<F> WithPollTimer for F where F: Future {}
|
2023-09-05 02:51:27 +00:00
|
|
|
|
|
|
|
pin_project_lite::pin_project! {
|
|
|
|
pub(crate) struct MetricsFuture<F> {
|
|
|
|
#[pin]
|
|
|
|
future: F,
|
|
|
|
|
|
|
|
metrics: Metrics,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Metrics {
|
|
|
|
name: &'static str,
|
|
|
|
start: Instant,
|
|
|
|
complete: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F> Future for MetricsFuture<F>
|
|
|
|
where
|
|
|
|
F: Future,
|
|
|
|
{
|
|
|
|
type Output = F::Output;
|
|
|
|
|
|
|
|
fn poll(
|
|
|
|
self: std::pin::Pin<&mut Self>,
|
|
|
|
cx: &mut std::task::Context<'_>,
|
|
|
|
) -> std::task::Poll<Self::Output> {
|
|
|
|
let this = self.project();
|
|
|
|
|
|
|
|
let out = std::task::ready!(this.future.poll(cx));
|
|
|
|
|
|
|
|
this.metrics.complete = true;
|
|
|
|
|
|
|
|
std::task::Poll::Ready(out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Metrics {
|
|
|
|
fn drop(&mut self) {
|
2023-12-27 00:06:38 +00:00
|
|
|
metrics::histogram!(self.name, "complete" => self.complete.to_string())
|
|
|
|
.record(self.start.elapsed().as_secs_f64());
|
2023-09-05 02:51:27 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-09 18:15:23 +00:00
|
|
|
|
|
|
|
pin_project_lite::pin_project! {
|
|
|
|
pub(crate) struct PollTimer<F> {
|
|
|
|
name: &'static str,
|
|
|
|
|
|
|
|
#[pin]
|
|
|
|
inner: F,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F> Future for PollTimer<F>
|
|
|
|
where
|
|
|
|
F: Future,
|
|
|
|
{
|
|
|
|
type Output = F::Output;
|
|
|
|
|
|
|
|
fn poll(
|
|
|
|
self: std::pin::Pin<&mut Self>,
|
|
|
|
cx: &mut std::task::Context<'_>,
|
|
|
|
) -> std::task::Poll<Self::Output> {
|
|
|
|
let start = Instant::now();
|
|
|
|
|
|
|
|
let this = self.project();
|
|
|
|
|
|
|
|
let out = this.inner.poll(cx);
|
|
|
|
|
|
|
|
let elapsed = start.elapsed();
|
|
|
|
if elapsed > Duration::from_micros(10) {
|
|
|
|
metrics::counter!(crate::init_metrics::FUTURE_POLL_TIMER_EXCEEDED, "timer" => this.name.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
if elapsed > Duration::from_secs(1) {
|
|
|
|
tracing::warn!(
|
|
|
|
"Future {} polled for {} seconds",
|
|
|
|
this.name,
|
|
|
|
elapsed.as_secs()
|
|
|
|
);
|
|
|
|
} else if elapsed > Duration::from_millis(1) {
|
|
|
|
tracing::warn!("Future {} polled for {} ms", this.name, elapsed.as_millis());
|
|
|
|
} else if elapsed > Duration::from_micros(200) {
|
|
|
|
tracing::debug!(
|
|
|
|
"Future {} polled for {} microseconds",
|
|
|
|
this.name,
|
|
|
|
elapsed.as_micros(),
|
|
|
|
);
|
|
|
|
} else if elapsed > Duration::from_micros(1) {
|
|
|
|
tracing::trace!(
|
|
|
|
"Future {} polled for {} microseconds",
|
|
|
|
this.name,
|
|
|
|
elapsed.as_micros()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
out
|
|
|
|
}
|
|
|
|
}
|