ntp-timer 0.2.0

A lightweight NTP time synchronization library for Rust applications. Provides in-process cached global time with background sync and clock-jump detection.
Documentation
//! # NTP 计时器
//!
//! 一个轻量级的 Rust NTP 时间同步库。
//! 提供进程内缓存的全局时间,具有自动后台同步和时钟跳跃检测功能。
//!
//! ## 特性
//!
//! - **NTP 时间同步**:从多个 NTP 服务器获取精准时间戳
//! - **指数退避重试**:自动采用指数退避策略重试(1秒 → 2秒 → 4秒...)
//! - **死循环重试**:持续尝试所有服务器,直到至少一个成功
//! - **内存缓存**:<0.1ms 的快速本地计算
//! - **时钟跳跃检测**:自动检测和处理系统时钟异常调整
//! - **后台同步**:异步定期同步任务,不阻塞主线程
//! - **完全可配置**:所有关键参数都可自定义
//! - **线程安全**:通过 `Arc<Mutex>` 支持并发访问
//!
//! ## 快速开始
//!
//! ```no_run
//! use ntp_timer::{Config, TimerManager};
//! use std::sync::Arc;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // 创建默认配置
//!     let config = Config::default()
//!         .with_sync_interval(600)      // 每 10 分钟同步一次
//!         .with_jump_threshold(5)       // 时钟跳跃阈值:5 秒
//!         .with_max_retries(10)         // 每个服务器最多重试 10 次
//!         .with_initial_retry_interval(1); // 初始延迟 1 秒
//!
//!     // 初始化全局计时器
//!     TimerManager::init_global_timer(Arc::new(config)).await?;
//!
//!     // 检查初始化是否完成
//!     if TimerManager::is_initialized() {
//!         // 获取当前时间戳(快速路径,<0.1ms)
//!         let timestamp = TimerManager::get_timestamp()?;
//!         println!("当前时间戳: {}", timestamp);
//!
//!         // 手动同步
//!         let synced_ts = TimerManager::manual_sync().await?;
//!         println!("同步后的时间戳: {}", synced_ts);
//!     }
//!
//!     Ok(())
//! }
//! ```
//!
//! ## 架构
//!
//! - **GlobalTimer**:核心数据结构,保存 NTP 时间戳和初始化状态
//! - **TimerManager**:公开 API,提供全局计时器访问(单例模式)
//! - **Config**:可配置参数(同步间隔、NTP 服务器、重试策略等)
//! - **NTP Module**:低层 NTP 协议实现,包括指数退避和死循环重试

pub mod config;
pub mod error;
pub mod ntp;
pub mod sync;
pub mod timer;

pub use config::Config;
pub use error::TimerError;
pub use sync::TimerManager;
pub use timer::GlobalTimer;

/// 本 crate 中 Result 的便利类型别名
pub type Result<T> = std::result::Result<T, TimerError>;