/
gtimer_timer.go
150 lines (133 loc) · 4.54 KB
/
gtimer_timer.go
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
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gtimer
import (
"github.com/gogf/gf/container/gtype"
"time"
)
func New(options ...TimerOptions) *Timer {
t := &Timer{
queue: newPriorityQueue(),
status: gtype.NewInt(StatusRunning),
ticks: gtype.NewInt64(),
}
if len(options) > 0 {
t.options = options[0]
} else {
t.options = DefaultOptions()
}
go t.loop()
return t
}
// Add adds a timing job to the timer, which runs in interval of <interval>.
func (t *Timer) Add(interval time.Duration, job JobFunc) *Entry {
return t.createEntry(interval, job, false, -1, StatusReady)
}
// AddEntry adds a timing job to the timer with detailed parameters.
//
// The parameter <interval> specifies the running interval of the job.
//
// The parameter <singleton> specifies whether the job running in singleton mode.
// There's only one of the same job is allowed running when its a singleton mode job.
//
// The parameter <times> specifies limit for the job running times, which means the job
// exits if its run times exceeds the <times>.
//
// The parameter <status> specifies the job status when it's firstly added to the timer.
func (t *Timer) AddEntry(interval time.Duration, job JobFunc, singleton bool, times int, status int) *Entry {
return t.createEntry(interval, job, singleton, times, status)
}
// AddSingleton is a convenience function for add singleton mode job.
func (t *Timer) AddSingleton(interval time.Duration, job JobFunc) *Entry {
return t.createEntry(interval, job, true, -1, StatusReady)
}
// AddOnce is a convenience function for adding a job which only runs once and then exits.
func (t *Timer) AddOnce(interval time.Duration, job JobFunc) *Entry {
return t.createEntry(interval, job, true, 1, StatusReady)
}
// AddTimes is a convenience function for adding a job which is limited running times.
func (t *Timer) AddTimes(interval time.Duration, times int, job JobFunc) *Entry {
return t.createEntry(interval, job, true, times, StatusReady)
}
// DelayAdd adds a timing job after delay of <interval> duration.
// Also see Add.
func (t *Timer) DelayAdd(delay time.Duration, interval time.Duration, job JobFunc) {
t.AddOnce(delay, func() {
t.Add(interval, job)
})
}
// DelayAddEntry adds a timing job after delay of <interval> duration.
// Also see AddEntry.
func (t *Timer) DelayAddEntry(delay time.Duration, interval time.Duration, job JobFunc, singleton bool, times int, status int) {
t.AddOnce(delay, func() {
t.AddEntry(interval, job, singleton, times, status)
})
}
// DelayAddSingleton adds a timing job after delay of <interval> duration.
// Also see AddSingleton.
func (t *Timer) DelayAddSingleton(delay time.Duration, interval time.Duration, job JobFunc) {
t.AddOnce(delay, func() {
t.AddSingleton(interval, job)
})
}
// DelayAddOnce adds a timing job after delay of <interval> duration.
// Also see AddOnce.
func (t *Timer) DelayAddOnce(delay time.Duration, interval time.Duration, job JobFunc) {
t.AddOnce(delay, func() {
t.AddOnce(interval, job)
})
}
// DelayAddTimes adds a timing job after delay of <interval> duration.
// Also see AddTimes.
func (t *Timer) DelayAddTimes(delay time.Duration, interval time.Duration, times int, job JobFunc) {
t.AddOnce(delay, func() {
t.AddTimes(interval, times, job)
})
}
// Start starts the timer.
func (t *Timer) Start() {
t.status.Set(StatusRunning)
}
// Stop stops the timer.
func (t *Timer) Stop() {
t.status.Set(StatusStopped)
}
// Close closes the timer.
func (t *Timer) Close() {
t.status.Set(StatusClosed)
}
// createEntry creates and adds a timing job to the timer.
func (t *Timer) createEntry(interval time.Duration, job JobFunc, singleton bool, times int, status int) *Entry {
var (
infinite = false
)
if times <= 0 {
infinite = true
}
var (
intervalTicksOfJob = int64(interval / t.options.Interval)
)
if intervalTicksOfJob == 0 {
// If the given interval is lesser than the one of the wheel,
// then sets it to one tick, which means it will be run in one interval.
intervalTicksOfJob = 1
}
var (
nextTicks = t.ticks.Val() + intervalTicksOfJob
entry = &Entry{
job: job,
timer: t,
ticks: intervalTicksOfJob,
times: gtype.NewInt(times),
status: gtype.NewInt(status),
singleton: gtype.NewBool(singleton),
nextTicks: gtype.NewInt64(nextTicks),
infinite: gtype.NewBool(infinite),
}
)
t.queue.Push(entry, nextTicks)
return entry
}