kestrel-protocol-timer 0.1.12

基于时间轮(Timing Wheel)算法的高性能异步定时器系统
Documentation
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//! 定时器配置模块
//!
//! 提供分层的配置结构和 Builder 模式,用于配置时间轮、服务和批处理行为。

use crate::error::TimerError;
use std::time::Duration;

/// 分层时间轮配置
///
/// 用于配置2层时间轮的参数。L0 层处理短延迟任务,L1 层处理长延迟任务。
///
/// # 示例
/// ```no_run
/// use kestrel_protocol_timer::HierarchicalWheelConfig;
/// use std::time::Duration;
///
/// let config = HierarchicalWheelConfig {
///     l0_tick_duration: Duration::from_millis(10),
///     l0_slot_count: 512,
///     l1_tick_duration: Duration::from_secs(1),
///     l1_slot_count: 60,
/// };
/// ```
#[derive(Debug, Clone)]
pub struct HierarchicalWheelConfig {
    /// L0 层(底层)每个 tick 的时间长度
    pub l0_tick_duration: Duration,
    /// L0 层槽位数量(必须是 2 的幂次方)
    pub l0_slot_count: usize,
    
    /// L1 层(高层)每个 tick 的时间长度
    pub l1_tick_duration: Duration,
    /// L1 层槽位数量(必须是 2 的幂次方)
    pub l1_slot_count: usize,
}

impl Default for HierarchicalWheelConfig {
    fn default() -> Self {
        Self {
            l0_tick_duration: Duration::from_millis(10),
            l0_slot_count: 512,
            l1_tick_duration: Duration::from_secs(1),
            l1_slot_count: 64,
        }
    }
}

/// 时间轮配置
///
/// 用于配置分层时间轮的参数。系统仅支持分层模式。
///
/// # 示例
/// ```no_run
/// use kestrel_protocol_timer::WheelConfig;
/// use std::time::Duration;
///
/// // 使用默认配置(分层模式)
/// let config = WheelConfig::default();
///
/// // 使用 Builder 自定义配置
/// let config = WheelConfig::builder()
///     .l0_tick_duration(Duration::from_millis(20))
///     .l0_slot_count(1024)
///     .l1_tick_duration(Duration::from_secs(2))
///     .l1_slot_count(128)
///     .build()
///     .unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct WheelConfig {
    /// 分层时间轮配置
    pub hierarchical: HierarchicalWheelConfig,
}

impl Default for WheelConfig {
    fn default() -> Self {
        Self {
            hierarchical: HierarchicalWheelConfig::default(),
        }
    }
}

impl WheelConfig {
    /// 创建配置构建器
    pub fn builder() -> WheelConfigBuilder {
        WheelConfigBuilder::default()
    }
}

/// 时间轮配置构建器
#[derive(Debug, Clone)]
pub struct WheelConfigBuilder {
    hierarchical: HierarchicalWheelConfig,
}

impl Default for WheelConfigBuilder {
    fn default() -> Self {
        Self {
            hierarchical: HierarchicalWheelConfig::default(),
        }
    }
}

impl WheelConfigBuilder {
    /// 设置 L0 层 tick 时长
    pub fn l0_tick_duration(mut self, duration: Duration) -> Self {
        self.hierarchical.l0_tick_duration = duration;
        self
    }

    /// 设置 L0 层槽位数量
    pub fn l0_slot_count(mut self, count: usize) -> Self {
        self.hierarchical.l0_slot_count = count;
        self
    }

    /// 设置 L1 层 tick 时长
    pub fn l1_tick_duration(mut self, duration: Duration) -> Self {
        self.hierarchical.l1_tick_duration = duration;
        self
    }

    /// 设置 L1 层槽位数量
    pub fn l1_slot_count(mut self, count: usize) -> Self {
        self.hierarchical.l1_slot_count = count;
        self
    }

    /// 构建配置并进行验证
    ///
    /// # 返回
    /// - `Ok(WheelConfig)`: 配置有效
    /// - `Err(TimerError)`: 配置验证失败
    ///
    /// # 验证规则
    /// - L0 tick 时长必须大于 0
    /// - L1 tick 时长必须大于 0
    /// - L0 槽位数量必须大于 0 且是 2 的幂次方
    /// - L1 槽位数量必须大于 0 且是 2 的幂次方
    /// - L1 tick 必须是 L0 tick 的整数倍
    pub fn build(self) -> Result<WheelConfig, TimerError> {
        let h = &self.hierarchical;
        
        // 验证 L0 层配置
        if h.l0_tick_duration.is_zero() {
            return Err(TimerError::InvalidConfiguration {
                field: "l0_tick_duration".to_string(),
                reason: "L0 层 tick 时长必须大于 0".to_string(),
            });
        }

        if h.l0_slot_count == 0 {
            return Err(TimerError::InvalidSlotCount {
                slot_count: h.l0_slot_count,
                reason: "L0 层槽位数量必须大于 0",
            });
        }

        if !h.l0_slot_count.is_power_of_two() {
            return Err(TimerError::InvalidSlotCount {
                slot_count: h.l0_slot_count,
                reason: "L0 层槽位数量必须是 2 的幂次方",
            });
        }

        // 验证 L1 层配置
        if h.l1_tick_duration.is_zero() {
            return Err(TimerError::InvalidConfiguration {
                field: "l1_tick_duration".to_string(),
                reason: "L1 层 tick 时长必须大于 0".to_string(),
            });
        }

        if h.l1_slot_count == 0 {
            return Err(TimerError::InvalidSlotCount {
                slot_count: h.l1_slot_count,
                reason: "L1 层槽位数量必须大于 0",
            });
        }

        if !h.l1_slot_count.is_power_of_two() {
            return Err(TimerError::InvalidSlotCount {
                slot_count: h.l1_slot_count,
                reason: "L1 层槽位数量必须是 2 的幂次方",
            });
        }

        // 验证 L1 tick 是 L0 tick 的整数倍
        let l0_ms = h.l0_tick_duration.as_millis() as u64;
        let l1_ms = h.l1_tick_duration.as_millis() as u64;
        if l1_ms % l0_ms != 0 {
            return Err(TimerError::InvalidConfiguration {
                field: "l1_tick_duration".to_string(),
                reason: format!(
                    "L1 tick 时长 ({} ms) 必须是 L0 tick 时长 ({} ms) 的整数倍",
                    l1_ms, l0_ms
                ),
            });
        }

        Ok(WheelConfig {
            hierarchical: self.hierarchical,
        })
    }
}

/// 服务配置
///
/// 用于配置 TimerService 的通道容量。
///
/// # 示例
/// ```no_run
/// use kestrel_protocol_timer::ServiceConfig;
///
/// // 使用默认配置
/// let config = ServiceConfig::default();
///
/// // 使用 Builder 自定义配置
/// let config = ServiceConfig::builder()
///     .command_channel_capacity(1024)
///     .timeout_channel_capacity(2000)
///     .build()
///     .unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct ServiceConfig {
    /// 命令通道容量
    pub command_channel_capacity: usize,
    /// 超时通道容量
    pub timeout_channel_capacity: usize,
}

impl Default for ServiceConfig {
    fn default() -> Self {
        Self {
            command_channel_capacity: 512,
            timeout_channel_capacity: 1000,
        }
    }
}

impl ServiceConfig {
    /// 创建配置构建器
    pub fn builder() -> ServiceConfigBuilder {
        ServiceConfigBuilder::default()
    }
}

/// 服务配置构建器
#[derive(Debug, Clone)]
pub struct ServiceConfigBuilder {
    command_channel_capacity: usize,
    timeout_channel_capacity: usize,
}

impl Default for ServiceConfigBuilder {
    fn default() -> Self {
        let config = ServiceConfig::default();
        Self {
            command_channel_capacity: config.command_channel_capacity,
            timeout_channel_capacity: config.timeout_channel_capacity,
        }
    }
}

impl ServiceConfigBuilder {
    /// 设置命令通道容量
    pub fn command_channel_capacity(mut self, capacity: usize) -> Self {
        self.command_channel_capacity = capacity;
        self
    }

    /// 设置超时通道容量
    pub fn timeout_channel_capacity(mut self, capacity: usize) -> Self {
        self.timeout_channel_capacity = capacity;
        self
    }

    /// 构建配置并进行验证
    ///
    /// # 返回
    /// - `Ok(ServiceConfig)`: 配置有效
    /// - `Err(TimerError)`: 配置验证失败
    ///
    /// # 验证规则
    /// - 所有通道容量必须大于 0
    pub fn build(self) -> Result<ServiceConfig, TimerError> {
        if self.command_channel_capacity == 0 {
            return Err(TimerError::InvalidConfiguration {
                field: "command_channel_capacity".to_string(),
                reason: "命令通道容量必须大于 0".to_string(),
            });
        }

        if self.timeout_channel_capacity == 0 {
            return Err(TimerError::InvalidConfiguration {
                field: "timeout_channel_capacity".to_string(),
                reason: "超时通道容量必须大于 0".to_string(),
            });
        }

        Ok(ServiceConfig {
            command_channel_capacity: self.command_channel_capacity,
            timeout_channel_capacity: self.timeout_channel_capacity,
        })
    }
}

/// 批处理配置
///
/// 用于配置批量操作的优化参数。
///
/// # 示例
/// ```no_run
/// use kestrel_protocol_timer::BatchConfig;
///
/// // 使用默认配置
/// let config = BatchConfig::default();
///
/// // 自定义配置
/// let config = BatchConfig {
///     small_batch_threshold: 20,
/// };
/// ```
#[derive(Debug, Clone)]
pub struct BatchConfig {
    /// 小批量阈值,用于批量取消优化
    /// 
    /// 当批量取消的任务数量小于等于此值时,直接逐个取消而不进行分组排序
    pub small_batch_threshold: usize,
}

impl Default for BatchConfig {
    fn default() -> Self {
        Self {
            small_batch_threshold: 10,
        }
    }
}

/// 顶层定时器配置
///
/// 组合所有子配置,提供完整的定时器系统配置。
///
/// # 示例
/// ```no_run
/// use kestrel_protocol_timer::TimerConfig;
///
/// // 使用默认配置
/// let config = TimerConfig::default();
///
/// // 使用 Builder 自定义配置(仅配置服务参数)
/// let config = TimerConfig::builder()
///     .command_channel_capacity(1024)
///     .timeout_channel_capacity(2000)
///     .build()
///     .unwrap();
/// ```
#[derive(Debug, Clone)]
pub struct TimerConfig {
    /// 时间轮配置
    pub wheel: WheelConfig,
    /// 服务配置
    pub service: ServiceConfig,
    /// 批处理配置
    pub batch: BatchConfig,
}

impl Default for TimerConfig {
    fn default() -> Self {
        Self {
            wheel: WheelConfig::default(),
            service: ServiceConfig::default(),
            batch: BatchConfig::default(),
        }
    }
}

impl TimerConfig {
    /// 创建配置构建器
    pub fn builder() -> TimerConfigBuilder {
        TimerConfigBuilder::default()
    }
}

/// 顶层定时器配置构建器
#[derive(Debug)]
pub struct TimerConfigBuilder {
    wheel_builder: WheelConfigBuilder,
    service_builder: ServiceConfigBuilder,
    batch_config: BatchConfig,
}

impl Default for TimerConfigBuilder {
    fn default() -> Self {
        Self {
            wheel_builder: WheelConfigBuilder::default(),
            service_builder: ServiceConfigBuilder::default(),
            batch_config: BatchConfig::default(),
        }
    }
}

impl TimerConfigBuilder {
    /// 设置命令通道容量
    pub fn command_channel_capacity(mut self, capacity: usize) -> Self {
        self.service_builder = self.service_builder.command_channel_capacity(capacity);
        self
    }

    /// 设置超时通道容量
    pub fn timeout_channel_capacity(mut self, capacity: usize) -> Self {
        self.service_builder = self.service_builder.timeout_channel_capacity(capacity);
        self
    }

    /// 设置小批量阈值
    pub fn small_batch_threshold(mut self, threshold: usize) -> Self {
        self.batch_config.small_batch_threshold = threshold;
        self
    }

    /// 构建配置并进行验证
    ///
    /// # 返回
    /// - `Ok(TimerConfig)`: 配置有效
    /// - `Err(TimerError)`: 配置验证失败
    pub fn build(self) -> Result<TimerConfig, TimerError> {
        Ok(TimerConfig {
            wheel: self.wheel_builder.build()?,
            service: self.service_builder.build()?,
            batch: self.batch_config,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_wheel_config_default() {
        let config = WheelConfig::default();
        assert_eq!(config.hierarchical.l0_tick_duration, Duration::from_millis(10));
        assert_eq!(config.hierarchical.l0_slot_count, 512);
        assert_eq!(config.hierarchical.l1_tick_duration, Duration::from_secs(1));
        assert_eq!(config.hierarchical.l1_slot_count, 64);
    }

    #[test]
    fn test_wheel_config_builder() {
        let config = WheelConfig::builder()
            .l0_tick_duration(Duration::from_millis(20))
            .l0_slot_count(1024)
            .l1_tick_duration(Duration::from_secs(2))
            .l1_slot_count(128)
            .build()
            .unwrap();

        assert_eq!(config.hierarchical.l0_tick_duration, Duration::from_millis(20));
        assert_eq!(config.hierarchical.l0_slot_count, 1024);
        assert_eq!(config.hierarchical.l1_tick_duration, Duration::from_secs(2));
        assert_eq!(config.hierarchical.l1_slot_count, 128);
    }

    #[test]
    fn test_wheel_config_validation_zero_tick() {
        let result = WheelConfig::builder()
            .l0_tick_duration(Duration::ZERO)
            .build();

        assert!(result.is_err());
    }

    #[test]
    fn test_wheel_config_validation_invalid_slot_count() {
        let result = WheelConfig::builder()
            .l0_slot_count(100)
            .build();

        assert!(result.is_err());
    }

    #[test]
    fn test_service_config_default() {
        let config = ServiceConfig::default();
        assert_eq!(config.command_channel_capacity, 512);
        assert_eq!(config.timeout_channel_capacity, 1000);
    }

    #[test]
    fn test_service_config_builder() {
        let config = ServiceConfig::builder()
            .command_channel_capacity(1024)
            .timeout_channel_capacity(2000)
            .build()
            .unwrap();

        assert_eq!(config.command_channel_capacity, 1024);
        assert_eq!(config.timeout_channel_capacity, 2000);
    }

    #[test]
    fn test_batch_config_default() {
        let config = BatchConfig::default();
        assert_eq!(config.small_batch_threshold, 10);
    }

    #[test]
    fn test_timer_config_default() {
        let config = TimerConfig::default();
        assert_eq!(config.wheel.hierarchical.l0_slot_count, 512);
        assert_eq!(config.service.command_channel_capacity, 512);
        assert_eq!(config.batch.small_batch_threshold, 10);
    }

    #[test]
    fn test_timer_config_builder() {
        let config = TimerConfig::builder()
            .command_channel_capacity(1024)
            .timeout_channel_capacity(2000)
            .small_batch_threshold(20)
            .build()
            .unwrap();

        assert_eq!(config.service.command_channel_capacity, 1024);
        assert_eq!(config.service.timeout_channel_capacity, 2000);
        assert_eq!(config.batch.small_batch_threshold, 20);
    }
}