Skip to main content

timerwheel/
executor.rs

1// Copyright © 2026-present The Timerwheel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15pub(crate) mod builder;
16pub(crate) mod metric_sink;
17pub(crate) mod metrics;
18pub(crate) mod panic_handler;
19pub(crate) mod pool;
20pub(crate) mod task;
21pub(crate) mod worker;
22
23pub use crate::executor::builder::PoolBuilder;
24pub use crate::executor::metric_sink::{MetricSink, NoopMetricSink};
25pub use crate::executor::metrics::ExecutorMetrics;
26pub use crate::executor::panic_handler::{NoopPanicHandler, PanicHandler};
27pub use crate::executor::pool::Pool;
28pub use crate::executor::task::{BoxTask, Task};
29use crate::{Error, Result};
30
31/// Rejected executor task together with the rejection reason.
32pub struct RejectedTask {
33    error: Error,
34    task: BoxTask,
35}
36
37impl RejectedTask {
38    /// Creates a rejection that preserves ownership of the original task.
39    pub fn new(error: Error, task: BoxTask) -> Self {
40        Self { error, task }
41    }
42
43    /// Returns the rejection reason.
44    pub fn error(&self) -> &Error {
45        &self.error
46    }
47
48    /// Returns the original task.
49    pub fn into_task(self) -> BoxTask {
50        self.task
51    }
52
53    /// Splits the rejection into reason and original task.
54    pub fn into_parts(self) -> (Error, BoxTask) {
55        (self.error, self.task)
56    }
57}
58
59/// Non-blocking execution boundary used by the timer scheduler.
60pub trait Executor: Send + Sync + 'static {
61    /// Attempts to accept a task without waiting for worker capacity.
62    fn try_execute(&self, task: BoxTask) -> std::result::Result<(), RejectedTask>;
63
64    /// Stops the executor and releases resources owned by it.
65    fn shutdown(&self) -> Result<()>;
66
67    /// Returns an immutable metrics snapshot.
68    fn metrics(&self) -> ExecutorMetrics;
69}