-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathtimer.go
More file actions
53 lines (41 loc) · 861 Bytes
/
timer.go
File metadata and controls
53 lines (41 loc) · 861 Bytes
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
// Copyright 2020-2024 guonaihong, antlabs. All rights reserved.
//
// mit license
package timer
import "time"
type Next interface {
Next(time.Time) time.Time
}
// 定时器接口
type Timer interface {
// 一次性定时器
AfterFunc(expire time.Duration, callback func()) TimeNoder
// 周期性定时器
ScheduleFunc(expire time.Duration, callback func()) TimeNoder
// 自定义下次的时间
CustomFunc(n Next, callback func()) TimeNoder
// 运行
Run()
// 停止所有定时器
Stop()
}
// 停止单个定时器
type TimeNoder interface {
Stop() bool
// 重置时间器
Reset(expire time.Duration) bool
}
// 定时器构造函数
func NewTimer(opt ...Option) Timer {
var o option
for _, cb := range opt {
cb(&o)
}
if o.timeWheel {
return newTimeWheel()
}
if o.minHeap {
return newMinHeap()
}
return newTimeWheel()
}