mirror of
https://github.com/guilhermewerner/mini-redis
synced 2025-06-15 14:35:13 +00:00
25 lines
479 B
Rust
25 lines
479 B
Rust
#![allow(non_snake_case)]
|
|
|
|
use std::sync::Arc;
|
|
use std::thread;
|
|
use std::time::{Duration, Instant};
|
|
use tokio::sync::Notify;
|
|
|
|
async fn delay(dur: Duration) {
|
|
let when = Instant::now() + dur;
|
|
let notify = Arc::new(Notify::new());
|
|
let notify2 = notify.clone();
|
|
|
|
thread::spawn(move || {
|
|
let now = Instant::now();
|
|
|
|
if now < when {
|
|
thread::sleep(when - now);
|
|
}
|
|
|
|
notify2.notify_one();
|
|
});
|
|
|
|
notify.notified().await;
|
|
}
|