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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
use crate::config::{BatchConfig, ServiceConfig, WheelConfig};
use crate::task::{CallbackWrapper, TaskId, TaskCompletionReason};
use crate::wheel::Wheel;
use parking_lot::Mutex;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::oneshot;
use tokio::task::JoinHandle;
/// 完成通知接收器,用于接收定时器完成通知
pub struct CompletionReceiver(pub oneshot::Receiver<TaskCompletionReason>);
/// 定时器句柄,用于管理定时器的生命周期
///
/// 注意:此类型不实现 Clone,以防止重复取消同一个定时器。
/// 每个定时器只应有一个所有者。
pub struct TimerHandle {
pub(crate) task_id: TaskId,
pub(crate) wheel: Arc<Mutex<Wheel>>,
pub(crate) completion_rx: CompletionReceiver,
}
impl TimerHandle {
pub(crate) fn new(task_id: TaskId, wheel: Arc<Mutex<Wheel>>, completion_rx: oneshot::Receiver<TaskCompletionReason>) -> Self {
Self { task_id, wheel, completion_rx: CompletionReceiver(completion_rx) }
}
/// 取消定时器
///
/// # 返回
/// 如果任务存在且成功取消返回 true,否则返回 false
///
/// # 示例
/// ```no_run
/// # use kestrel_protocol_timer::{TimerWheel, CallbackWrapper};
/// # use std::time::Duration;
/// #
/// # #[tokio::main]
/// # async fn main() {
/// let timer = TimerWheel::with_defaults();
/// let callback = Some(CallbackWrapper::new(|| async {}));
/// let task = TimerWheel::create_task(Duration::from_secs(1), callback);
/// let handle = timer.register(task);
///
/// // 取消定时器
/// let success = handle.cancel();
/// println!("取消成功: {}", success);
/// # }
/// ```
pub fn cancel(&self) -> bool {
let mut wheel = self.wheel.lock();
wheel.cancel(self.task_id)
}
/// 获取完成通知接收器的可变引用
///
/// # 示例
/// ```no_run
/// # use kestrel_protocol_timer::{TimerWheel, CallbackWrapper};
/// # use std::time::Duration;
/// #
/// # #[tokio::main]
/// # async fn main() {
/// let timer = TimerWheel::with_defaults();
/// let callback = Some(CallbackWrapper::new(|| async {
/// println!("Timer fired!");
/// }));
/// let task = TimerWheel::create_task(Duration::from_secs(1), callback);
/// let handle = timer.register(task);
///
/// // 等待定时器完成(使用 into_completion_receiver 消耗句柄)
/// handle.into_completion_receiver().0.await.ok();
/// println!("Timer completed!");
/// # }
/// ```
pub fn completion_receiver(&mut self) -> &mut CompletionReceiver {
&mut self.completion_rx
}
/// 消耗句柄,返回完成通知接收器
///
/// # 示例
/// ```no_run
/// # use kestrel_protocol_timer::{TimerWheel, CallbackWrapper};
/// # use std::time::Duration;
/// #
/// # #[tokio::main]
/// # async fn main() {
/// let timer = TimerWheel::with_defaults();
/// let callback = Some(CallbackWrapper::new(|| async {
/// println!("Timer fired!");
/// }));
/// let task = TimerWheel::create_task(Duration::from_secs(1), callback);
/// let handle = timer.register(task);
///
/// // 等待定时器完成
/// handle.into_completion_receiver().0.await.ok();
/// println!("Timer completed!");
/// # }
/// ```
pub fn into_completion_receiver(self) -> CompletionReceiver {
self.completion_rx
}
}
/// 批量定时器句柄,用于管理批量调度的定时器
///
/// 通过共享 Wheel 引用减少内存开销,同时提供批量操作和迭代器访问能力。
///
/// 注意:此类型不实现 Clone,以防止重复取消同一批定时器。
/// 如需访问单个定时器句柄,请使用 `into_iter()` 或 `into_handles()` 进行转换。
pub struct BatchHandle {
pub(crate) task_ids: Vec<TaskId>,
pub(crate) wheel: Arc<Mutex<Wheel>>,
pub(crate) completion_rxs: Vec<oneshot::Receiver<TaskCompletionReason>>,
}
impl BatchHandle {
pub(crate) fn new(task_ids: Vec<TaskId>, wheel: Arc<Mutex<Wheel>>, completion_rxs: Vec<oneshot::Receiver<TaskCompletionReason>>) -> Self {
Self { task_ids, wheel, completion_rxs }
}
/// 批量取消所有定时器
///
/// # 返回
/// 成功取消的任务数量
///
/// # 示例
/// ```no_run
/// # use kestrel_protocol_timer::{TimerWheel, CallbackWrapper};
/// # use std::time::Duration;
/// #
/// # #[tokio::main]
/// # async fn main() {
/// let timer = TimerWheel::with_defaults();
/// let delays: Vec<Duration> = (0..10)
/// .map(|_| Duration::from_secs(1))
/// .collect();
/// let tasks = TimerWheel::create_batch(delays);
/// let batch = timer.register_batch(tasks);
///
/// let cancelled = batch.cancel_all();
/// println!("取消了 {} 个定时器", cancelled);
/// # }
/// ```
pub fn cancel_all(self) -> usize {
let mut wheel = self.wheel.lock();
wheel.cancel_batch(&self.task_ids)
}
/// 将批量句柄转换为单个定时器句柄的 Vec
///
/// 消耗 BatchHandle,为每个任务创建独立的 TimerHandle。
///
/// # 示例
/// ```no_run
/// # use kestrel_protocol_timer::{TimerWheel, CallbackWrapper};
/// # use std::time::Duration;
/// #
/// # #[tokio::main]
/// # async fn main() {
/// let timer = TimerWheel::with_defaults();
/// let delays: Vec<Duration> = (0..3)
/// .map(|_| Duration::from_secs(1))
/// .collect();
/// let tasks = TimerWheel::create_batch(delays);
/// let batch = timer.register_batch(tasks);
///
/// // 转换为独立的句柄
/// let handles = batch.into_handles();
/// for handle in handles {
/// // 可以单独操作每个句柄
/// }
/// # }
/// ```
pub fn into_handles(self) -> Vec<TimerHandle> {
self.task_ids
.into_iter()
.zip(self.completion_rxs.into_iter())
.map(|(task_id, rx)| {
TimerHandle::new(task_id, self.wheel.clone(), rx)
})
.collect()
}
/// 获取批量任务的数量
pub fn len(&self) -> usize {
self.task_ids.len()
}
/// 检查批量任务是否为空
pub fn is_empty(&self) -> bool {
self.task_ids.is_empty()
}
/// 获取所有任务 ID 的引用
pub fn task_ids(&self) -> &[TaskId] {
&self.task_ids
}
/// 获取所有完成通知接收器的引用
///
/// # 返回
/// 所有任务的完成通知接收器列表引用
pub fn completion_receivers(&mut self) -> &mut Vec<oneshot::Receiver<TaskCompletionReason>> {
&mut self.completion_rxs
}
/// 消耗句柄,返回所有完成通知接收器
///
/// # 返回
/// 所有任务的完成通知接收器列表
///
/// # 示例
/// ```no_run
/// # use kestrel_protocol_timer::{TimerWheel, CallbackWrapper};
/// # use std::time::Duration;
/// #
/// # #[tokio::main]
/// # async fn main() {
/// let timer = TimerWheel::with_defaults();
/// let delays: Vec<Duration> = (0..3)
/// .map(|_| Duration::from_secs(1))
/// .collect();
/// let tasks = TimerWheel::create_batch(delays);
/// let batch = timer.register_batch(tasks);
///
/// // 获取所有完成通知接收器
/// let receivers = batch.into_completion_receivers();
/// for rx in receivers {
/// tokio::spawn(async move {
/// if rx.await.is_ok() {
/// println!("A timer completed!");
/// }
/// });
/// }
/// # }
/// ```
pub fn into_completion_receivers(self) -> Vec<oneshot::Receiver<TaskCompletionReason>> {
self.completion_rxs
}
}
/// 实现 IntoIterator,允许直接迭代 BatchHandle
///
/// # 示例
/// ```no_run
/// # use kestrel_protocol_timer::{TimerWheel, CallbackWrapper};
/// # use std::time::Duration;
/// #
/// # #[tokio::main]
/// # async fn main() {
/// let timer = TimerWheel::with_defaults();
/// let delays: Vec<Duration> = (0..3)
/// .map(|_| Duration::from_secs(1))
/// .collect();
/// let tasks = TimerWheel::create_batch(delays);
/// let batch = timer.register_batch(tasks);
///
/// // 直接迭代,每个元素都是独立的 TimerHandle
/// for handle in batch {
/// // 可以单独操作每个句柄
/// }
/// # }
/// ```
impl IntoIterator for BatchHandle {
type Item = TimerHandle;
type IntoIter = BatchHandleIter;
fn into_iter(self) -> Self::IntoIter {
BatchHandleIter {
task_ids: self.task_ids.into_iter(),
completion_rxs: self.completion_rxs.into_iter(),
wheel: self.wheel,
}
}
}
/// BatchHandle 的迭代器
pub struct BatchHandleIter {
task_ids: std::vec::IntoIter<TaskId>,
completion_rxs: std::vec::IntoIter<oneshot::Receiver<TaskCompletionReason>>,
wheel: Arc<Mutex<Wheel>>,
}
impl Iterator for BatchHandleIter {
type Item = TimerHandle;
fn next(&mut self) -> Option<Self::Item> {
match (self.task_ids.next(), self.completion_rxs.next()) {
(Some(task_id), Some(rx)) => {
Some(TimerHandle::new(task_id, self.wheel.clone(), rx))
}
_ => None,
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.task_ids.size_hint()
}
}
impl ExactSizeIterator for BatchHandleIter {
fn len(&self) -> usize {
self.task_ids.len()
}
}
/// 时间轮定时器管理器
pub struct TimerWheel {
/// 时间轮唯一标识符
/// 时间轮实例(使用 Arc<Mutex> 包装以支持多线程访问)
wheel: Arc<Mutex<Wheel>>,
/// 后台 tick 循环任务句柄
tick_handle: Option<JoinHandle<()>>,
}
impl TimerWheel {
/// 创建新的定时器管理器
///
/// # 参数
/// - `config`: 时间轮配置(已经过验证)
///
/// # 示例
/// ```no_run
/// use kestrel_protocol_timer::{TimerWheel, WheelConfig, TimerTask, BatchConfig};
/// use std::time::Duration;
///
/// #[tokio::main]
/// async fn main() {
/// let config = WheelConfig::builder()
/// .l0_tick_duration(Duration::from_millis(10))
/// .l0_slot_count(512)
/// .l1_tick_duration(Duration::from_secs(1))
/// .l1_slot_count(64)
/// .build()
/// .unwrap();
/// let timer = TimerWheel::new(config, BatchConfig::default());
///
/// // 使用两步式 API
/// let task = TimerWheel::create_task(Duration::from_secs(1), None);
/// let handle = timer.register(task);
/// }
/// ```
pub fn new(config: WheelConfig, batch_config: BatchConfig) -> Self {
let tick_duration = config.hierarchical.l0_tick_duration;
let wheel = Wheel::new(config, batch_config);
let wheel = Arc::new(Mutex::new(wheel));
let wheel_clone = wheel.clone();
// 启动后台 tick 循环
let tick_handle = tokio::spawn(async move {
Self::tick_loop(wheel_clone, tick_duration).await;
});
Self {
wheel,
tick_handle: Some(tick_handle),
}
}
/// 创建带默认配置的定时器管理器(分层模式)
/// - L0 层 tick 时长: 10ms, 槽位数量: 512
/// - L1 层 tick 时长: 1s, 槽位数量: 64
///
/// # 示例
/// ```no_run
/// use kestrel_protocol_timer::TimerWheel;
///
/// #[tokio::main]
/// async fn main() {
/// let timer = TimerWheel::with_defaults();
/// }
/// ```
pub fn with_defaults() -> Self {
Self::new(WheelConfig::default(), BatchConfig::default())
}
/// 创建与此时间轮绑定的 TimerService(使用默认配置)
///
/// # 返回
/// 绑定到此时间轮的 TimerService 实例
///
/// # 参数
/// - `service_config`: 服务配置
///
/// # 示例
/// ```no_run
/// use kestrel_protocol_timer::{TimerWheel, TimerService, CallbackWrapper, ServiceConfig};
/// use std::time::Duration;
///
///
/// #[tokio::main]
/// async fn main() {
/// let timer = TimerWheel::with_defaults();
/// let mut service = timer.create_service(ServiceConfig::default());
///
/// // 使用两步式 API 通过 service 批量调度定时器
/// let callbacks: Vec<(Duration, Option<CallbackWrapper>)> = (0..5)
/// .map(|_| (Duration::from_millis(100), Some(CallbackWrapper::new(|| async {}))))
/// .collect();
/// let tasks = TimerService::create_batch_with_callbacks(callbacks);
/// service.register_batch(tasks).unwrap();
///
/// // 接收超时通知
/// let mut rx = service.take_receiver().unwrap();
/// while let Some(task_id) = rx.recv().await {
/// println!("Task {:?} completed", task_id);
/// }
/// }
/// ```
pub fn create_service(&self, service_config: ServiceConfig) -> crate::service::TimerService {
crate::service::TimerService::new(self.wheel.clone(), service_config)
}
/// 创建与此时间轮绑定的 TimerService(使用自定义配置)
///
/// # 参数
/// - `config`: 服务配置
///
/// # 返回
/// 绑定到此时间轮的 TimerService 实例
///
/// # 示例
/// ```no_run
/// use kestrel_protocol_timer::{TimerWheel, ServiceConfig};
///
/// #[tokio::main]
/// async fn main() {
/// let timer = TimerWheel::with_defaults();
/// let config = ServiceConfig::builder()
/// .command_channel_capacity(1024)
/// .timeout_channel_capacity(2000)
/// .build()
/// .unwrap();
/// let service = timer.create_service_with_config(config);
/// }
/// ```
pub fn create_service_with_config(&self, config: ServiceConfig) -> crate::service::TimerService {
crate::service::TimerService::new(self.wheel.clone(), config)
}
/// 创建定时器任务(申请阶段)
///
/// # 参数
/// - `delay`: 延迟时间
/// - `callback`: 实现了 TimerCallback trait 的回调对象
///
/// # 返回
/// 返回 TimerTask,需要通过 `register()` 注册到时间轮
///
/// # 示例
/// ```no_run
/// use kestrel_protocol_timer::{TimerWheel, TimerTask, CallbackWrapper};
/// use std::time::Duration;
///
///
/// #[tokio::main]
/// async fn main() {
/// let timer = TimerWheel::with_defaults();
///
/// // 步骤 1: 创建任务
/// let task = TimerWheel::create_task(Duration::from_secs(1), Some(CallbackWrapper::new(|| async {
/// println!("Timer fired!");
/// })));
///
/// // 获取任务 ID
/// let task_id = task.get_id();
/// println!("Created task: {:?}", task_id);
///
/// // 步骤 2: 注册任务
/// let handle = timer.register(task);
/// }
/// ```
#[inline]
pub fn create_task(delay: Duration, callback: Option<CallbackWrapper>) -> crate::task::TimerTask {
crate::task::TimerTask::new(delay, callback)
}
/// 批量创建定时器任务(申请阶段)
///
/// # 参数
/// - `callbacks`: (延迟时间, 回调) 的元组列表
///
/// # 返回
/// 返回 TimerTask 列表,需要通过 `register_batch()` 注册到时间轮
///
/// # 示例
/// ```no_run
/// use kestrel_protocol_timer::{TimerWheel, TimerTask, CallbackWrapper};
/// use std::time::Duration;
/// use std::sync::Arc;
/// use std::sync::atomic::{AtomicU32, Ordering};
///
/// #[tokio::main]
/// async fn main() {
/// let timer = TimerWheel::with_defaults();
/// let counter = Arc::new(AtomicU32::new(0));
///
/// // 步骤 1: 批量创建任务
/// let delays: Vec<Duration> = (0..3)
/// .map(|_| Duration::from_millis(100))
/// .collect();
///
/// let tasks = TimerWheel::create_batch(delays);
/// println!("Created {} tasks", tasks.len());
///
/// // 步骤 2: 批量注册任务
/// let batch = timer.register_batch(tasks);
/// }
/// ```
#[inline]
pub fn create_batch(delays: Vec<Duration>) -> Vec<crate::task::TimerTask>
{
delays
.into_iter()
.map(|delay| crate::task::TimerTask::new(delay, None))
.collect()
}
/// 批量创建定时器任务(申请阶段)
///
/// # 参数
/// - `callbacks`: (延迟时间, 回调) 的元组列表
///
/// # 返回
/// 返回 TimerTask 列表,需要通过 `register_batch()` 注册到时间轮
///
/// # 示例
/// ```no_run
/// use kestrel_protocol_timer::{TimerWheel, TimerTask, CallbackWrapper};
/// use std::time::Duration;
/// use std::sync::Arc;
/// use std::sync::atomic::{AtomicU32, Ordering};
///
/// #[tokio::main]
/// async fn main() {
/// let timer = TimerWheel::with_defaults();
/// let counter = Arc::new(AtomicU32::new(0));
///
/// // 步骤 1: 批量创建任务
/// let delays: Vec<Duration> = (0..3)
/// .map(|_| Duration::from_millis(100))
/// .collect();
/// let callbacks: Vec<(Duration, Option<CallbackWrapper>)> = delays
/// .into_iter()
/// .map(|delay| {
/// let counter = Arc::clone(&counter);
/// let callback = Some(CallbackWrapper::new(move || {
/// let counter = Arc::clone(&counter);
/// async move {
/// counter.fetch_add(1, Ordering::SeqCst);
/// }
/// }));
/// (delay, callback)
/// })
/// .collect();
///
/// let tasks = TimerWheel::create_batch_with_callbacks(callbacks);
/// println!("Created {} tasks", tasks.len());
///
/// // 步骤 2: 批量注册任务
/// let batch = timer.register_batch(tasks);
/// }
/// ```
#[inline]
pub fn create_batch_with_callbacks(callbacks: Vec<(Duration, Option<CallbackWrapper>)>) -> Vec<crate::task::TimerTask>
{
callbacks
.into_iter()
.map(|(delay, callback)| crate::task::TimerTask::new(delay, callback))
.collect()
}
/// 注册定时器任务到时间轮(注册阶段)
///
/// # 参数
/// - `task`: 通过 `create_task()` 创建的任务
///
/// # 返回
/// 返回定时器句柄,可用于取消定时器和接收完成通知
///
/// # 示例
/// ```no_run
/// use kestrel_protocol_timer::{TimerWheel, TimerTask, CallbackWrapper};
///
/// use std::time::Duration;
///
/// #[tokio::main]
/// async fn main() {
/// let timer = TimerWheel::with_defaults();
///
/// let task = TimerWheel::create_task(Duration::from_secs(1), Some(CallbackWrapper::new(|| async {
/// println!("Timer fired!");
/// })));
/// let task_id = task.get_id();
///
/// let handle = timer.register(task);
///
/// // 等待定时器完成
/// handle.into_completion_receiver().0.await.ok();
/// }
/// ```
#[inline]
pub fn register(&self, task: crate::task::TimerTask) -> TimerHandle {
let (completion_tx, completion_rx) = oneshot::channel();
let notifier = crate::task::CompletionNotifier(completion_tx);
let task_id = task.id;
// 单次加锁完成所有操作
{
let mut wheel_guard = self.wheel.lock();
wheel_guard.insert(task, notifier);
}
TimerHandle::new(task_id, self.wheel.clone(), completion_rx)
}
/// 批量注册定时器任务到时间轮(注册阶段)
///
/// # 参数
/// - `tasks`: 通过 `create_batch()` 创建的任务列表
///
/// # 返回
/// 返回批量定时器句柄
///
/// # 示例
/// ```no_run
/// use kestrel_protocol_timer::{TimerWheel, TimerTask};
/// use std::time::Duration;
///
/// #[tokio::main]
/// async fn main() {
/// let timer = TimerWheel::with_defaults();
///
/// let delays: Vec<Duration> = (0..3)
/// .map(|_| Duration::from_secs(1))
/// .collect();
/// let tasks = TimerWheel::create_batch(delays);
///
/// let batch = timer.register_batch(tasks);
/// println!("Registered {} timers", batch.len());
/// }
/// ```
#[inline]
pub fn register_batch(&self, tasks: Vec<crate::task::TimerTask>) -> BatchHandle {
let task_count = tasks.len();
let mut completion_rxs = Vec::with_capacity(task_count);
let mut task_ids = Vec::with_capacity(task_count);
let mut prepared_tasks = Vec::with_capacity(task_count);
// 步骤1: 准备所有 channels 和 notifiers(无锁)
// 优化:使用 for 循环代替 map + collect,避免闭包捕获开销
for task in tasks {
let (completion_tx, completion_rx) = oneshot::channel();
let notifier = crate::task::CompletionNotifier(completion_tx);
task_ids.push(task.id);
completion_rxs.push(completion_rx);
prepared_tasks.push((task, notifier));
}
// 步骤2: 单次加锁,批量插入
{
let mut wheel_guard = self.wheel.lock();
wheel_guard.insert_batch(prepared_tasks);
}
BatchHandle::new(task_ids, self.wheel.clone(), completion_rxs)
}
/// 取消定时器
///
/// # 参数
/// - `task_id`: 任务 ID
///
/// # 返回
/// 如果任务存在且成功取消返回 true,否则返回 false
///
/// # 示例
/// ```no_run
/// use kestrel_protocol_timer::{TimerWheel, TimerTask, CallbackWrapper};
///
/// use std::time::Duration;
///
/// #[tokio::main]
/// async fn main() {
/// let timer = TimerWheel::with_defaults();
///
/// let task = TimerWheel::create_task(Duration::from_secs(10), Some(CallbackWrapper::new(|| async {
/// println!("Timer fired!");
/// })));
/// let task_id = task.get_id();
/// let _handle = timer.register(task);
///
/// // 使用任务 ID 取消
/// let cancelled = timer.cancel(task_id);
/// println!("取消成功: {}", cancelled);
/// }
/// ```
#[inline]
pub fn cancel(&self, task_id: TaskId) -> bool {
let mut wheel = self.wheel.lock();
wheel.cancel(task_id)
}
/// 批量取消定时器
///
/// # 参数
/// - `task_ids`: 要取消的任务 ID 列表
///
/// # 返回
/// 成功取消的任务数量
///
/// # 性能优势
/// - 批量处理减少锁竞争
/// - 内部优化批量取消操作
///
/// # 示例
/// ```no_run
/// use kestrel_protocol_timer::{TimerWheel, TimerTask};
/// use std::time::Duration;
///
/// #[tokio::main]
/// async fn main() {
/// let timer = TimerWheel::with_defaults();
///
/// // 创建多个定时器
/// let task1 = TimerWheel::create_task(Duration::from_secs(10), None);
/// let task2 = TimerWheel::create_task(Duration::from_secs(10), None);
/// let task3 = TimerWheel::create_task(Duration::from_secs(10), None);
///
/// let task_ids = vec![task1.get_id(), task2.get_id(), task3.get_id()];
///
/// let _h1 = timer.register(task1);
/// let _h2 = timer.register(task2);
/// let _h3 = timer.register(task3);
///
/// // 批量取消
/// let cancelled = timer.cancel_batch(&task_ids);
/// println!("已取消 {} 个定时器", cancelled);
/// }
/// ```
#[inline]
pub fn cancel_batch(&self, task_ids: &[TaskId]) -> usize {
let mut wheel = self.wheel.lock();
wheel.cancel_batch(task_ids)
}
/// 推迟定时器
///
/// # 参数
/// - `task_id`: 要推迟的任务 ID
/// - `new_delay`: 新的延迟时间(从当前时间点重新计算)
/// - `callback`: 新的回调函数,传入 `None` 保持原回调不变,传入 `Some` 替换为新回调
///
/// # 返回
/// 如果任务存在且成功推迟返回 true,否则返回 false
///
/// # 注意
/// - 推迟后任务 ID 保持不变
/// - 原有的 completion_receiver 仍然有效
///
/// # 示例
///
/// ## 保持原回调
/// ```no_run
/// use kestrel_protocol_timer::{TimerWheel, TimerTask, CallbackWrapper};
/// use std::time::Duration;
///
///
/// #[tokio::main]
/// async fn main() {
/// let timer = TimerWheel::with_defaults();
///
/// let task = TimerWheel::create_task(Duration::from_secs(5), Some(CallbackWrapper::new(|| async {
/// println!("Timer fired!");
/// })));
/// let task_id = task.get_id();
/// let _handle = timer.register(task);
///
/// // 推迟到 10 秒后触发(保持原回调)
/// let success = timer.postpone(task_id, Duration::from_secs(10), None);
/// println!("推迟成功: {}", success);
/// }
/// ```
///
/// ## 替换为新回调
/// ```no_run
/// use kestrel_protocol_timer::{TimerWheel, TimerTask, CallbackWrapper};
/// use std::time::Duration;
///
/// #[tokio::main]
/// async fn main() {
/// let timer = TimerWheel::with_defaults();
///
/// let task = TimerWheel::create_task(Duration::from_secs(5), Some(CallbackWrapper::new(|| async {
/// println!("Original callback!");
/// })));
/// let task_id = task.get_id();
/// let _handle = timer.register(task);
///
/// // 推迟到 10 秒后触发(并替换为新回调)
/// let success = timer.postpone(task_id, Duration::from_secs(10), Some(CallbackWrapper::new(|| async {
/// println!("New callback!");
/// })));
/// println!("推迟成功: {}", success);
/// }
/// ```
#[inline]
pub fn postpone(
&self,
task_id: TaskId,
new_delay: Duration,
callback: Option<CallbackWrapper>,
) -> bool {
let mut wheel = self.wheel.lock();
wheel.postpone(task_id, new_delay, callback)
}
/// 批量推迟定时器(保持原回调)
///
/// # 参数
/// - `updates`: (任务ID, 新延迟) 的元组列表
///
/// # 返回
/// 成功推迟的任务数量
///
/// # 注意
/// - 此方法会保持所有任务的原回调不变
/// - 如需替换回调,请使用 `postpone_batch_with_callbacks`
///
/// # 性能优势
/// - 批量处理减少锁竞争
/// - 内部优化批量推迟操作
///
/// # 示例
/// ```no_run
/// use kestrel_protocol_timer::{TimerWheel, TimerTask, CallbackWrapper};
/// use std::time::Duration;
///
/// #[tokio::main]
/// async fn main() {
/// let timer = TimerWheel::with_defaults();
///
/// // 创建多个带回调的定时器
/// let task1 = TimerWheel::create_task(Duration::from_secs(5), Some(CallbackWrapper::new(|| async {
/// println!("Task 1 fired!");
/// })));
/// let task2 = TimerWheel::create_task(Duration::from_secs(5), Some(CallbackWrapper::new(|| async {
/// println!("Task 2 fired!");
/// })));
/// let task3 = TimerWheel::create_task(Duration::from_secs(5), Some(CallbackWrapper::new(|| async {
/// println!("Task 3 fired!");
/// })));
///
/// let task_ids = vec![
/// (task1.get_id(), Duration::from_secs(10)),
/// (task2.get_id(), Duration::from_secs(15)),
/// (task3.get_id(), Duration::from_secs(20)),
/// ];
///
/// timer.register(task1);
/// timer.register(task2);
/// timer.register(task3);
///
/// // 批量推迟(保持原回调)
/// let postponed = timer.postpone_batch(task_ids);
/// println!("已推迟 {} 个定时器", postponed);
/// }
/// ```
#[inline]
pub fn postpone_batch(&self, updates: Vec<(TaskId, Duration)>) -> usize {
let mut wheel = self.wheel.lock();
wheel.postpone_batch(updates)
}
/// 批量推迟定时器(替换回调)
///
/// # 参数
/// - `updates`: (任务ID, 新延迟, 新回调) 的元组列表
///
/// # 返回
/// 成功推迟的任务数量
///
/// # 性能优势
/// - 批量处理减少锁竞争
/// - 内部优化批量推迟操作
///
/// # 示例
/// ```no_run
/// use kestrel_protocol_timer::{TimerWheel, TimerTask, CallbackWrapper};
/// use std::time::Duration;
/// use std::sync::Arc;
/// use std::sync::atomic::{AtomicU32, Ordering};
///
/// #[tokio::main]
/// async fn main() {
/// let timer = TimerWheel::with_defaults();
/// let counter = Arc::new(AtomicU32::new(0));
///
/// // 创建多个定时器
/// let task1 = TimerWheel::create_task(Duration::from_secs(5), None);
/// let task2 = TimerWheel::create_task(Duration::from_secs(5), None);
///
/// let id1 = task1.get_id();
/// let id2 = task2.get_id();
///
/// timer.register(task1);
/// timer.register(task2);
///
/// // 批量推迟并替换回调
/// let updates: Vec<_> = vec![id1, id2]
/// .into_iter()
/// .map(|id| {
/// let counter = Arc::clone(&counter);
/// (id, Duration::from_secs(10), Some(CallbackWrapper::new(move || {
/// let counter = Arc::clone(&counter);
/// async move { counter.fetch_add(1, Ordering::SeqCst); }
/// })))
/// })
/// .collect();
/// let postponed = timer.postpone_batch_with_callbacks(updates);
/// println!("已推迟 {} 个定时器", postponed);
/// }
/// ```
#[inline]
pub fn postpone_batch_with_callbacks(
&self,
updates: Vec<(TaskId, Duration, Option<CallbackWrapper>)>,
) -> usize {
let mut wheel = self.wheel.lock();
wheel.postpone_batch_with_callbacks(updates.to_vec())
}
/// 核心 tick 循环
async fn tick_loop(wheel: Arc<Mutex<Wheel>>, tick_duration: Duration) {
let mut interval = tokio::time::interval(tick_duration);
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
loop {
interval.tick().await;
// 推进时间轮并获取到期任务
let expired_tasks = {
let mut wheel_guard = wheel.lock();
wheel_guard.advance()
};
// 执行到期任务
for task in expired_tasks {
let callback = task.get_callback();
// 移动task的所有权来获取completion_notifier
let notifier = task.completion_notifier;
// 只有注册过的任务才有 notifier
if let Some(notifier) = notifier {
// 在独立的 tokio 任务中执行回调,并在回调完成后发送通知
if let Some(callback) = callback {
tokio::spawn(async move {
// 执行回调
let future = callback.call();
future.await;
// 回调执行完成后发送通知
let _ = notifier.0.send(TaskCompletionReason::Expired);
});
} else {
// 如果没有回调,立即发送完成通知
let _ = notifier.0.send(TaskCompletionReason::Expired);
}
}
}
}
}
/// 停止定时器管理器
pub async fn shutdown(mut self) {
if let Some(handle) = self.tick_handle.take() {
handle.abort();
let _ = handle.await;
}
}
}
impl Drop for TimerWheel {
fn drop(&mut self) {
if let Some(handle) = self.tick_handle.take() {
handle.abort();
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::{AtomicU32, Ordering};
#[tokio::test]
async fn test_timer_creation() {
let _timer = TimerWheel::with_defaults();
}
#[tokio::test]
async fn test_schedule_once() {
use std::sync::Arc;
let timer = TimerWheel::with_defaults();
let counter = Arc::new(AtomicU32::new(0));
let counter_clone = Arc::clone(&counter);
let task = TimerWheel::create_task(
Duration::from_millis(50),
Some(CallbackWrapper::new(move || {
let counter = Arc::clone(&counter_clone);
async move {
counter.fetch_add(1, Ordering::SeqCst);
}
})),
);
let _handle = timer.register(task);
// 等待定时器触发
tokio::time::sleep(Duration::from_millis(100)).await;
assert_eq!(counter.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn test_cancel_timer() {
use std::sync::Arc;
let timer = TimerWheel::with_defaults();
let counter = Arc::new(AtomicU32::new(0));
let counter_clone = Arc::clone(&counter);
let task = TimerWheel::create_task(
Duration::from_millis(100),
Some(CallbackWrapper::new(move || {
let counter = Arc::clone(&counter_clone);
async move {
counter.fetch_add(1, Ordering::SeqCst);
}
})),
);
let handle = timer.register(task);
// 立即取消
let cancel_result = handle.cancel();
assert!(cancel_result);
// 等待足够长时间确保定时器不会触发
tokio::time::sleep(Duration::from_millis(200)).await;
assert_eq!(counter.load(Ordering::SeqCst), 0);
}
#[tokio::test]
async fn test_cancel_immediate() {
use std::sync::Arc;
let timer = TimerWheel::with_defaults();
let counter = Arc::new(AtomicU32::new(0));
let counter_clone = Arc::clone(&counter);
let task = TimerWheel::create_task(
Duration::from_millis(100),
Some(CallbackWrapper::new(move || {
let counter = Arc::clone(&counter_clone);
async move {
counter.fetch_add(1, Ordering::SeqCst);
}
})),
);
let handle = timer.register(task);
// 立即取消
let cancel_result = handle.cancel();
assert!(cancel_result);
// 等待足够长时间确保定时器不会触发
tokio::time::sleep(Duration::from_millis(200)).await;
assert_eq!(counter.load(Ordering::SeqCst), 0);
}
#[tokio::test]
async fn test_postpone_timer() {
use std::sync::Arc;
let timer = TimerWheel::with_defaults();
let counter = Arc::new(AtomicU32::new(0));
let counter_clone = Arc::clone(&counter);
let task = TimerWheel::create_task(
Duration::from_millis(50),
Some(CallbackWrapper::new(move || {
let counter = Arc::clone(&counter_clone);
async move {
counter.fetch_add(1, Ordering::SeqCst);
}
})),
);
let task_id = task.get_id();
let handle = timer.register(task);
// 推迟任务到 150ms
let postponed = timer.postpone(task_id, Duration::from_millis(150), None);
assert!(postponed);
// 等待原定时间 50ms,任务不应该触发
tokio::time::sleep(Duration::from_millis(70)).await;
assert_eq!(counter.load(Ordering::SeqCst), 0);
// 等待新的触发时间(从推迟开始算,还需要等待约 150ms)
let result = tokio::time::timeout(
Duration::from_millis(200),
handle.into_completion_receiver().0
).await;
assert!(result.is_ok());
// 等待回调执行
tokio::time::sleep(Duration::from_millis(20)).await;
assert_eq!(counter.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn test_postpone_with_callback() {
use std::sync::Arc;
let timer = TimerWheel::with_defaults();
let counter = Arc::new(AtomicU32::new(0));
let counter_clone1 = Arc::clone(&counter);
let counter_clone2 = Arc::clone(&counter);
// 创建任务,原始回调增加 1
let task = TimerWheel::create_task(
Duration::from_millis(50),
Some(CallbackWrapper::new(move || {
let counter = Arc::clone(&counter_clone1);
async move {
counter.fetch_add(1, Ordering::SeqCst);
}
})),
);
let task_id = task.get_id();
let handle = timer.register(task);
// 推迟任务并替换回调,新回调增加 10
let postponed = timer.postpone(
task_id,
Duration::from_millis(100),
Some(CallbackWrapper::new(move || {
let counter = Arc::clone(&counter_clone2);
async move {
counter.fetch_add(10, Ordering::SeqCst);
}
})),
);
assert!(postponed);
// 等待任务触发(推迟后需要等待100ms,加上余量)
let result = tokio::time::timeout(
Duration::from_millis(200),
handle.into_completion_receiver().0
).await;
assert!(result.is_ok());
// 等待回调执行
tokio::time::sleep(Duration::from_millis(20)).await;
// 验证新回调被执行(增加了 10 而不是 1)
assert_eq!(counter.load(Ordering::SeqCst), 10);
}
#[tokio::test]
async fn test_postpone_nonexistent_timer() {
let timer = TimerWheel::with_defaults();
// 尝试推迟不存在的任务
let fake_task = TimerWheel::create_task(Duration::from_millis(50), None);
let fake_task_id = fake_task.get_id();
// 不注册这个任务
let postponed = timer.postpone(fake_task_id, Duration::from_millis(100), None);
assert!(!postponed);
}
#[tokio::test]
async fn test_postpone_batch() {
use std::sync::Arc;
let timer = TimerWheel::with_defaults();
let counter = Arc::new(AtomicU32::new(0));
// 创建 3 个任务
let mut task_ids = Vec::new();
for _ in 0..3 {
let counter_clone = Arc::clone(&counter);
let task = TimerWheel::create_task(
Duration::from_millis(50),
Some(CallbackWrapper::new(move || {
let counter = Arc::clone(&counter_clone);
async move {
counter.fetch_add(1, Ordering::SeqCst);
}
})),
);
task_ids.push((task.get_id(), Duration::from_millis(150)));
timer.register(task);
}
// 批量推迟
let postponed = timer.postpone_batch(task_ids);
assert_eq!(postponed, 3);
// 等待原定时间 50ms,任务不应该触发
tokio::time::sleep(Duration::from_millis(70)).await;
assert_eq!(counter.load(Ordering::SeqCst), 0);
// 等待新的触发时间(从推迟开始算,还需要等待约 150ms)
tokio::time::sleep(Duration::from_millis(200)).await;
// 等待回调执行
tokio::time::sleep(Duration::from_millis(20)).await;
assert_eq!(counter.load(Ordering::SeqCst), 3);
}
#[tokio::test]
async fn test_postpone_batch_with_callbacks() {
use std::sync::Arc;
let timer = TimerWheel::with_defaults();
let counter = Arc::new(AtomicU32::new(0));
// 创建 3 个任务
let mut task_ids = Vec::new();
for _ in 0..3 {
let task = TimerWheel::create_task(
Duration::from_millis(50),
None
);
task_ids.push(task.get_id());
timer.register(task);
}
// 批量推迟并替换回调
let updates: Vec<_> = task_ids
.into_iter()
.map(|id| {
let counter_clone = Arc::clone(&counter);
(id, Duration::from_millis(150), Some(CallbackWrapper::new(move || {
let counter = Arc::clone(&counter_clone);
async move {
counter.fetch_add(1, Ordering::SeqCst);
}
})))
})
.collect();
let postponed = timer.postpone_batch_with_callbacks(updates);
assert_eq!(postponed, 3);
// 等待原定时间 50ms,任务不应该触发
tokio::time::sleep(Duration::from_millis(70)).await;
assert_eq!(counter.load(Ordering::SeqCst), 0);
// 等待新的触发时间(从推迟开始算,还需要等待约 150ms)
tokio::time::sleep(Duration::from_millis(200)).await;
// 等待回调执行
tokio::time::sleep(Duration::from_millis(20)).await;
assert_eq!(counter.load(Ordering::SeqCst), 3);
}
#[tokio::test]
async fn test_postpone_keeps_completion_receiver_valid() {
use std::sync::Arc;
let timer = TimerWheel::with_defaults();
let counter = Arc::new(AtomicU32::new(0));
let counter_clone = Arc::clone(&counter);
let task = TimerWheel::create_task(
Duration::from_millis(50),
Some(CallbackWrapper::new(move || {
let counter = Arc::clone(&counter_clone);
async move {
counter.fetch_add(1, Ordering::SeqCst);
}
})),
);
let task_id = task.get_id();
let handle = timer.register(task);
// 推迟任务
timer.postpone(task_id, Duration::from_millis(100), None);
// 验证原 completion_receiver 仍然有效(推迟后需要等待100ms,加上余量)
let result = tokio::time::timeout(
Duration::from_millis(200),
handle.into_completion_receiver().0
).await;
assert!(result.is_ok(), "Completion receiver should still work after postpone");
// 等待回调执行
tokio::time::sleep(Duration::from_millis(20)).await;
assert_eq!(counter.load(Ordering::SeqCst), 1);
}
}