spin_common/
sloth.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//! Warn on slow operations

use tokio::task::JoinHandle;
use tokio::time::{sleep, Duration};

/// Print a warning message after the given duration unless the returned
/// [`SlothGuard`] is dropped first.
pub fn warn_if_slothful(warn_after_ms: u64, message: impl Into<String>) -> SlothGuard {
    let message = message.into();
    let warning = tokio::spawn(async move {
        sleep(Duration::from_millis(warn_after_ms)).await;
        eprintln!("{message}");
    });
    SlothGuard { warning }
}

/// Returned by [`warn_if_slothful`]; cancels the warning when dropped.
#[must_use]
pub struct SlothGuard {
    warning: JoinHandle<()>,
}

impl Drop for SlothGuard {
    fn drop(&mut self) {
        self.warning.abort()
    }
}