Skip to main content

zenith_stack/
lib.rs

1//! # Zenith — 全协议栈高性能网络框架
2//!
3//! Zenith 是一个面向极端性能场景的全协议栈网络框架,集成:
4//! - **AF_XDP + eBPF**:零拷贝数据面、bpf_link 原子挂载、双 Bank 热更新
5//! - **TLS 1.3**:rustls 集成、证书代际管理、SNI/ALPN 路由
6//! - **HTTP/1.1 / HTTP/2 / HTTP/3**:RFC 7230 / 7540 / 9114 全协议实现
7//! - **Web 框架**:编译期 Trie 路由、类型化 Extractor、中间件 DAG
8//! - **Proxy / Cache / WAF / Forward**:反向代理、缓存、Web 应用防火墙、L4 转发
9//! - **Runtime**:三级 Supervisor、ChangeSet 热切换、RuntimeGraph 拓扑规划
10//!
11//! ## 按需导入
12//!
13//! Zenith 采用 **feature-gated facade** 设计,默认仅启用最小核心(`api` + `core`),
14//! 其余能力按需开启,避免拖入不必要的重依赖(如 `libbpf-rs`、`rustls`、`ring`)。
15//!
16//! ### 最小依赖(仅类型定义)
17//!
18//! ```toml
19//! [dependencies]
20//! zenith = { path = "...", default-features = false, features = ["api"] }
21//! ```
22//!
23//! ### 仅 Web 框架(不拖入 AF_XDP / eBPF)
24//!
25//! ```toml
26//! [dependencies]
27//! zenith = { path = "...", default-features = false, features = ["web"] }
28//! ```
29//!
30//! ### 仅 HTTP 协议解析(无 Linux 数据面)
31//!
32//! ```toml
33//! [dependencies]
34//! zenith = { path = "...", default-features = false, features = ["http"] }
35//! ```
36//!
37//! ### 完整生产协议栈
38//!
39//! ```toml
40//! [dependencies]
41//! zenith = { path = "...", features = ["full-stack"] }
42//! ```
43//!
44//! ### 全部能力(含测试工具)
45//!
46//! ```toml
47//! [dependencies]
48//! zenith = { path = "...", features = ["full"] }
49//! ```
50//!
51//! ## Feature 矩阵
52//!
53//! | Feature | 启用能力 | 拉入重依赖 |
54//! |---|---|---|
55//! | `api` | 公共类型定义(CanonicalRequest/Response 等) | 无 |
56//! | `core` | 核心基础设施(FramePool/Ledger/时间轮) | `thiserror` |
57//! | `net` | L2-L4 协议解析、TCP/UDP/QUIC 状态机 | 无(`linux` 默认关闭) |
58//! | `linux` | AF_XDP Socket、UMEM、四环操作 | `libbpf-rs`、`libc` |
59//! | `ebpf` | eBPF 程序加载、双 Bank 热更新 | `libbpf-rs`、`libc`、`nix`(经 `libbpf-sys`) |
60//! | `tls` | TLS 1.3 引擎、证书代际管理 | `rustls`、`ring` |
61//! | `http1` / `http2` / `http3` | HTTP 协议解析器 | 依赖 `net` |
62//! | `web` | 最小 Web 框架(路由/中间件/静态文件/H1-H2-H3/TLS/WAF/缓存/代理);不含 runtime/linux/ebpf(实测不拖入 libbpf/io_uring/tokio);启用 `runtime` 特性自动升级为 full(AF_XDP 桥 + Supervisor + ChangeSet + WAF 卸载闭环) | `serde`、`serde_json`、`rustls` |
63//! | `proxy` | 反向代理、负载均衡、熔断器 | 无 |
64//! | `cache` | HTTP 缓存层 | 无 |
65//! | `waf` | Web 应用防火墙 | 无 |
66//! | `forward` | L4 转发 | 无 |
67//! | `runtime` | 全链路数据面运行时 | 依赖 `net` + `linux` + `ebpf` |
68//! | `capability` | 硬件/内核能力检测 | 无 |
69//! | `observability` | 指标、tracing、健康检查 | 无 |
70//! | `testkit` | 测试工具、Demo、压力测试 | 依赖 `full` |
71//! | `http` | 组合:http1 + http2 + http3 | — |
72//! | `full-stack` | 组合:完整生产协议栈 | — |
73//! | `full` | 组合:全部能力 | — |
74//!
75//! # 设计原则
76//! - 热路径零堆分配(固定容量数组替代 String/Vec)
77//! - 单线程本地存储、无锁访问
78//! - 编译期路由注册、运行期零开销分发
79//! - 所有 unsafe 封装在 `zenith-linux` 内部,上层零 unsafe
80
81#![deny(unsafe_code)]
82#![deny(missing_debug_implementations)]
83#![warn(missing_docs)]
84
85// ─────────────────────────────────────────────────────────────────────────────
86// 公共 API 类型(默认启用)
87// ─────────────────────────────────────────────────────────────────────────────
88
89/// Zenith 公共 API 与类型定义
90#[cfg(feature = "api")]
91pub extern crate zenith_api;
92
93/// Zenith 公共 API 重导出
94///
95/// 通过 `zenith_stack::api` 可访问所有公共类型,等价于直接使用 `zenith_api` crate。
96#[cfg(feature = "api")]
97pub mod api {
98    pub use zenith_api::*;
99}
100
101// ─────────────────────────────────────────────────────────────────────────────
102// 核心基础设施(默认启用)
103// ─────────────────────────────────────────────────────────────────────────────
104
105/// Zenith 核心基础设施
106#[cfg(feature = "core")]
107pub extern crate zenith_foundation;
108
109/// Zenith 核心基础设施重导出
110#[cfg(feature = "core")]
111pub mod core {
112    pub use zenith_foundation::*;
113}
114
115// ─────────────────────────────────────────────────────────────────────────────
116// 网络协议层(按需启用)
117// ─────────────────────────────────────────────────────────────────────────────
118
119/// Zenith 网络地址与传输层抽象
120#[cfg(feature = "net")]
121pub extern crate zenith_net;
122
123/// Zenith 网络层重导出
124#[cfg(feature = "net")]
125pub mod net {
126    pub use zenith_net::*;
127}
128
129/// Zenith Linux 平台抽象层(AF_XDP / UMEM / Ring)
130#[cfg(all(feature = "linux", target_os = "linux"))]
131pub extern crate zenith_linux;
132
133/// Zenith Linux 平台重导出
134#[cfg(all(feature = "linux", target_os = "linux"))]
135pub mod linux {
136    pub use zenith_linux::*;
137}
138
139/// Zenith eBPF 程序管理
140#[cfg(all(feature = "ebpf", target_os = "linux"))]
141pub extern crate zenith_ebpf;
142
143/// Zenith eBPF 重导出
144#[cfg(all(feature = "ebpf", target_os = "linux"))]
145pub mod ebpf {
146    pub use zenith_ebpf::*;
147}
148
149/// Zenith TLS 1.3 安全引擎
150#[cfg(feature = "tls")]
151pub extern crate zenith_tls;
152
153/// Zenith TLS 重导出
154#[cfg(feature = "tls")]
155pub mod tls {
156    pub use zenith_tls::*;
157}
158
159// ─────────────────────────────────────────────────────────────────────────────
160// HTTP 协议层(按需启用)
161// ─────────────────────────────────────────────────────────────────────────────
162
163/// Zenith HTTP/1.1 协议解析器
164#[cfg(feature = "http1")]
165pub extern crate zenith_http1;
166
167/// Zenith HTTP/1.1 重导出
168#[cfg(feature = "http1")]
169pub mod http1 {
170    pub use zenith_http1::*;
171}
172
173/// Zenith HTTP/2 协议实现
174#[cfg(feature = "http2")]
175pub extern crate zenith_http2;
176
177/// Zenith HTTP/2 重导出
178#[cfg(feature = "http2")]
179pub mod http2 {
180    pub use zenith_http2::*;
181}
182
183/// Zenith HTTP/3 协议实现
184#[cfg(feature = "http3")]
185pub extern crate zenith_http3;
186
187/// Zenith HTTP/3 重导出
188#[cfg(feature = "http3")]
189pub mod http3 {
190    pub use zenith_http3::*;
191}
192
193// ─────────────────────────────────────────────────────────────────────────────
194// 应用层(按需启用)
195// ─────────────────────────────────────────────────────────────────────────────
196
197/// Zenith Web 应用框架
198#[cfg(feature = "web")]
199pub extern crate zenith_web;
200
201/// Zenith Web 框架重导出
202#[cfg(feature = "web")]
203pub mod web {
204    pub use zenith_web::*;
205}
206
207/// Zenith 反向代理
208#[cfg(feature = "proxy")]
209pub extern crate zenith_proxy;
210
211/// Zenith 代理重导出
212#[cfg(feature = "proxy")]
213pub mod proxy {
214    pub use zenith_proxy::*;
215}
216
217/// Zenith HTTP 缓存层
218#[cfg(feature = "cache")]
219pub extern crate zenith_cache;
220
221/// Zenith 缓存重导出
222#[cfg(feature = "cache")]
223pub mod cache {
224    pub use zenith_cache::*;
225}
226
227/// Zenith Web 应用防火墙
228#[cfg(feature = "waf")]
229pub extern crate zenith_waf;
230
231/// Zenith WAF 重导出
232#[cfg(feature = "waf")]
233pub mod waf {
234    pub use zenith_waf::*;
235}
236
237/// Zenith L4 转发层
238#[cfg(feature = "forward")]
239pub extern crate zenith_forward;
240
241/// Zenith 转发重导出
242#[cfg(feature = "forward")]
243pub mod forward {
244    pub use zenith_forward::*;
245}
246
247// ─────────────────────────────────────────────────────────────────────────────
248// 运行时与治理(按需启用)
249// ─────────────────────────────────────────────────────────────────────────────
250
251/// Zenith 全链路数据面运行时
252#[cfg(feature = "runtime")]
253pub extern crate zenith_runtime;
254
255/// Zenith 运行时重导出
256#[cfg(feature = "runtime")]
257pub mod runtime {
258    pub use zenith_runtime::*;
259}
260
261/// Zenith 能力检测
262#[cfg(feature = "capability")]
263pub extern crate zenith_capability;
264
265/// Zenith 能力检测重导出
266#[cfg(feature = "capability")]
267pub mod capability {
268    pub use zenith_capability::*;
269}
270
271/// Zenith 可观测性
272#[cfg(feature = "observability")]
273pub extern crate zenith_observability;
274
275/// Zenith 可观测性重导出
276#[cfg(feature = "observability")]
277pub mod observability {
278    pub use zenith_observability::*;
279}
280
281/// Zenith 测试工具箱
282#[cfg(feature = "testkit")]
283pub extern crate zenith_testkit;
284
285/// Zenith 测试工具重导出
286#[cfg(feature = "testkit")]
287pub mod testkit {
288    pub use zenith_testkit::*;
289}
290
291/// Zenith 全链路指纹识别与阻断
292#[cfg(feature = "fingerprint")]
293pub extern crate zenith_fingerprint;
294
295/// Zenith 指纹识别重导出
296#[cfg(feature = "fingerprint")]
297pub mod fingerprint {
298    pub use zenith_fingerprint::*;
299}
300
301// ─────────────────────────────────────────────────────────────────────────────
302// 全局便捷 API(启用 runtime feature 时可用)
303// ─────────────────────────────────────────────────────────────────────────────
304
305/// 全局 Tokio 异步运行时便捷函数(feature = `runtime`)
306///
307/// 消除跨 crate 传递 `Handle` / 每个二进制重复写 `Builder::new_multi_thread()` 的样板代码。
308/// 所有函数 Fail-Closed:未初始化时自动按 `RuntimeConfig::auto()` 懒加载。
309///
310/// # 示例
311/// ```ignore
312/// use zenith_stack::{spawn, block_on};
313///
314/// // 无需显式构建 Runtime,任何位置都能直接 spawn
315/// let task = spawn(async { 42 });
316/// let result = block_on(task);
317/// assert_eq!(result, Ok(42));
318/// ```
319#[cfg(feature = "runtime")]
320pub mod rt {
321    /// 显式初始化全局运行时(幂等、线程安全、可重入)。
322    /// 若全局已被其他线程先初始化,本次 config 被忽略,返回已存在引用。
323    pub use zenith_runtime::init_global;
324    /// 获取全局运行时引用(自动懒加载)
325    pub use zenith_runtime::global_runtime;
326    /// 获取全局 Tokio Handle(Clone 零成本)
327    pub use zenith_runtime::handle;
328    /// 在全局运行时上 [`tokio::spawn`](无需传 Handle)
329    pub use zenith_runtime::spawn;
330    /// 用全局运行时 `block_on` 执行 Future
331    pub use zenith_runtime::block_on;
332    /// 全局运行时类型
333    pub use zenith_runtime::GlobalRuntime;
334    /// 运行时配置(worker_threads / enable_io / enable_time / stack_size)
335    pub use zenith_runtime::RuntimeConfig;
336}
337
338// ─────────────────────────────────────────────────────────────────────────────
339// Prelude:常用类型一键导入
340// ─────────────────────────────────────────────────────────────────────────────
341
342/// Prelude 模块:按启用的特性提供常用类型一键导入
343///
344/// # 用法
345///
346/// ```no_run
347/// use zenith_stack::prelude::*;
348/// ```
349pub mod prelude {
350    #[cfg(feature = "api")]
351    pub use zenith_api::{
352        CanonicalRequest, CanonicalResponse, Method, Protocol, Transport,
353    };
354
355    #[cfg(feature = "core")]
356    pub use zenith_foundation::{CoreError, CoreResult, FrameId, FramePool, FrameToken};
357
358    #[cfg(feature = "web")]
359    pub use zenith_web::{App, WebError};
360
361    /// 启用 runtime 时,prelude 直接导出 spawn/block_on/handle 零配置使用
362    #[cfg(feature = "runtime")]
363    pub use zenith_runtime::{block_on, handle, spawn};
364}
365
366#[cfg(test)]
367mod tests {
368    // 仅验证 facade 在默认特性下可编译
369    #[test]
370    fn facade_compiles_with_default_features() {
371        #[cfg(feature = "api")]
372        let _ = zenith_api::Method::Get;
373
374        #[cfg(feature = "core")]
375        let _ = zenith_foundation::CoreError::internal("test");
376    }
377}