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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2018-2026 the Deno authors. MIT license.
//! Reactor abstraction for timer and I/O primitives.
//!
//! Currently used by [`UserTimer`](crate::web_timeout::UserTimer) to abstract
//! over the timer backend. The default implementation (`reactor-tokio` feature)
//! delegates to tokio.
//!
//! Note: `uv_compat` does **not** use this trait -- it talks to tokio directly
//! because it needs lower-level control (poll_accept, try_read, try_write).
use std::future::Future;
use std::ops::Add;
use std::pin::Pin;
use std::task::Context;
use std::task::Poll;
use std::time::Duration;
/// Abstraction over the async I/O reactor (tokio, mio, io_uring, custom).
/// This is the only seam between deno_core and the underlying async runtime.
pub trait Reactor: 'static {
type Timer: ReactorTimer;
type Instant: ReactorInstant;
/// Create a new one-shot timer that fires at the given instant.
fn timer(&self, deadline: Self::Instant) -> Self::Timer;
/// Get the current instant.
fn now(&self) -> Self::Instant;
/// Poll the reactor for I/O readiness. This is called during the "poll" phase.
/// Drives the underlying event source (epoll/kqueue/iocp).
/// `timeout` = None means block indefinitely, Some(Duration::ZERO) means non-blocking.
fn poll(&self, cx: &mut Context, timeout: Option<Duration>) -> Poll<()>;
/// Spawn a future onto the reactor's executor (if it has one).
/// Returns a handle that can be polled for the result.
fn spawn(
&self,
fut: Pin<Box<dyn Future<Output = ()> + 'static>>,
) -> Pin<Box<dyn Future<Output = ()>>>;
}
/// A timer future that can be reset to fire at a different deadline.
pub trait ReactorTimer: Future<Output = ()> + Unpin {
fn reset(&mut self, deadline: impl Into<Self::Instant>)
where
Self: Sized;
type Instant: ReactorInstant;
/// The deadline this timer is set to fire at.
fn deadline(&self) -> Self::Instant;
}
/// An instant in time, used for timer deadlines.
pub trait ReactorInstant:
Copy + Ord + Add<Duration, Output = Self> + Send + Sync + 'static
{
fn now() -> Self;
fn elapsed(&self) -> Duration;
fn checked_add(&self, duration: Duration) -> Option<Self>;
}
/// The default reactor type, selected by feature flags.
/// When `reactor-tokio` is enabled, this is `TokioReactor`.
#[cfg(feature = "reactor-tokio")]
pub type DefaultReactor = crate::reactor_tokio::TokioReactor;