Documentation ¶
Overview ¶
Package gtimer implements Hierarchical Timing Wheel for interval/delayed jobs running and management.
任务定时器, 高性能的分层时间轮任务管理模块,用于管理间隔/延迟运行任务。 与gcron模块的区别是,时间轮模块只管理间隔执行任务,并且更注重执行效率(纳秒级别)。 需要注意执行时间间隔的准确性问题: https://github.com/golang/go/issues/14410
Index ¶
- Constants
- func DelayAdd(delay time.Duration, interval time.Duration, job JobFunc)
- func DelayAddEntry(delay time.Duration, interval time.Duration, job JobFunc, singleton bool, ...)
- func DelayAddOnce(delay time.Duration, interval time.Duration, job JobFunc)
- func DelayAddSingleton(delay time.Duration, interval time.Duration, job JobFunc)
- func DelayAddTimes(delay time.Duration, interval time.Duration, times int, job JobFunc)
- func Exit()
- func SetInterval(interval time.Duration, job JobFunc)
- func SetTimeout(delay time.Duration, job JobFunc)
- type Entry
- func Add(interval time.Duration, job JobFunc) *Entry
- func AddEntry(interval time.Duration, job JobFunc, singleton bool, times int, status int) *Entry
- func AddOnce(interval time.Duration, job JobFunc) *Entry
- func AddSingleton(interval time.Duration, job JobFunc) *Entry
- func AddTimes(interval time.Duration, times int, job JobFunc) *Entry
- func (entry *Entry) Close()
- func (entry *Entry) IsSingleton() bool
- func (entry *Entry) Run()
- func (entry *Entry) SetSingleton(enabled bool)
- func (entry *Entry) SetStatus(status int) int
- func (entry *Entry) SetTimes(times int)
- func (entry *Entry) Start()
- func (entry *Entry) Status() int
- func (entry *Entry) Stop()
- type JobFunc
- type Timer
- func (t *Timer) Add(interval time.Duration, job JobFunc) *Entry
- func (t *Timer) AddEntry(interval time.Duration, job JobFunc, singleton bool, times int, status int) *Entry
- func (t *Timer) AddOnce(interval time.Duration, job JobFunc) *Entry
- func (t *Timer) AddSingleton(interval time.Duration, job JobFunc) *Entry
- func (t *Timer) AddTimes(interval time.Duration, times int, job JobFunc) *Entry
- func (t *Timer) Close()
- func (t *Timer) DelayAdd(delay time.Duration, interval time.Duration, job JobFunc)
- func (t *Timer) DelayAddEntry(delay time.Duration, interval time.Duration, job JobFunc, singleton bool, ...)
- func (t *Timer) DelayAddOnce(delay time.Duration, interval time.Duration, job JobFunc)
- func (t *Timer) DelayAddSingleton(delay time.Duration, interval time.Duration, job JobFunc)
- func (t *Timer) DelayAddTimes(delay time.Duration, interval time.Duration, times int, job JobFunc)
- func (t *Timer) Start()
- func (t *Timer) Stop()
Examples ¶
Constants ¶
View Source
const ( STATUS_READY = 0 STATUS_RUNNING = 1 STATUS_STOPPED = 2 STATUS_CLOSED = -1 )
Variables ¶
This section is empty.
Functions ¶
func DelayAddEntry ¶
func DelayAddEntry(delay time.Duration, interval time.Duration, job JobFunc, singleton bool, times int, status int)
延迟添加循环任务, 支持完整的参数。
func DelayAddOnce ¶
延迟添加只运行一次的循环任务,delay参数单位为秒
func DelayAddSingleton ¶
延迟添加单例循环任务,delay参数单位为秒
func DelayAddTimes ¶
延迟添加运行指定次数的循环任务,delay参数单位为秒
func SetInterval ¶
类似与js中的SetInterval,每隔一段时间执行指定回调函数。
Types ¶
type Entry ¶
type Entry struct {
// contains filtered or unexported fields
}
循环任务项
func Add ¶
添加执行方法。
Example ¶
package main import ( "fmt" "github.com/gogf/gf/g/os/gtimer" "time" ) func main() { now := time.Now() interval := 1400 * time.Millisecond gtimer.Add(interval, func() { fmt.Println(time.Now(), time.Duration(time.Now().UnixNano()-now.UnixNano())) now = time.Now() }) select {} }
Output:
type Timer ¶
type Timer struct {
// contains filtered or unexported fields
}
定时器/分层时间轮
func (*Timer) AddEntry ¶
func (t *Timer) AddEntry(interval time.Duration, job JobFunc, singleton bool, times int, status int) *Entry
添加定时任务
func (*Timer) AddSingleton ¶
添加单例运行循环任务
func (*Timer) DelayAddEntry ¶
func (t *Timer) DelayAddEntry(delay time.Duration, interval time.Duration, job JobFunc, singleton bool, times int, status int)
延迟添加循环任务, 支持完整的参数。
func (*Timer) DelayAddOnce ¶
延迟添加只运行一次的循环任务
func (*Timer) DelayAddSingleton ¶
延迟添加单例循环任务
func (*Timer) DelayAddTimes ¶
延迟添加只运行一次的循环任务
Click to show internal directories.
Click to hide internal directories.