-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtimer_default.go
More file actions
57 lines (40 loc) · 1.38 KB
/
timer_default.go
File metadata and controls
57 lines (40 loc) · 1.38 KB
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
package timer
import (
"time"
"github.com/panjf2000/ants/v2"
)
var pool GoPool = wrapperAnts{}
var defaultTimer = NewTimer(WithGoPool(pool))
func init() {
defaultTimer.Start()
}
// Timer return the default timer.
func DefaultTimer() *Timer { return defaultTimer }
// TickMs return Basic time tick milliseconds.
func TickMs() int64 { return defaultTimer.TickMs() }
// WheelSize return the wheel size.
func WheelSize() int { return defaultTimer.WheelSize() }
// TaskCounter return the total number of tasks.
func TaskCounter() int64 { return defaultTimer.TaskCounter() }
// AfterFunc adds a function to the timer.
func AfterFunc(d time.Duration, f func()) (*Task, error) { return defaultTimer.AfterFunc(d, f) }
// AddTask adds a task to the timer.
func AddTask(task *Task) error { return defaultTimer.AddTask(task) }
// AddDerefTask adds a task from DerefTask to the timer.
func AddDerefTask(task DerefTask) error { return defaultTimer.AddDerefTask(task) }
// Started have started or not.
func Started() bool { return defaultTimer.Started() }
// Start the timer.
func Start() { defaultTimer.Start() }
// Stop the timer.
func Stop() { defaultTimer.Stop() }
type wrapperAnts struct{}
func (wrapperAnts) Go(f func()) {
Go(f)
}
// Go run a function in `ants` goroutine pool, if submit failed, fallback to use goroutine.
func Go(f func()) {
if err := ants.Submit(f); err != nil {
go f()
}
}