gf: github.com/gogf/gf/g/os/gtimer Index | Examples | Files

package gtimer

import "github.com/gogf/gf/g/os/gtimer"

Package gtimer implements Hierarchical Timing Wheel for interval/delayed jobs running and management.

任务定时器, 高性能的分层时间轮任务管理模块,用于管理间隔/延迟运行任务。 与gcron模块的区别是,时间轮模块只管理间隔执行任务,并且更注重执行效率(纳秒级别)。 需要注意执行时间间隔的准确性问题: https://github.com/golang/go/issues/14410

Index

Examples

Package Files

gtimer.go gtimer_entry.go gtimer_loop.go gtimer_timer.go

Constants

const (
    STATUS_READY   = 0
    STATUS_RUNNING = 1
    STATUS_STOPPED = 2
    STATUS_CLOSED  = -1
)

func DelayAdd Uses

func DelayAdd(delay time.Duration, interval time.Duration, job JobFunc)

延迟添加循环任务。

func DelayAddEntry Uses

func DelayAddEntry(delay time.Duration, interval time.Duration, job JobFunc, singleton bool, times int, status int)

延迟添加循环任务, 支持完整的参数。

func DelayAddOnce Uses

func DelayAddOnce(delay time.Duration, interval time.Duration, job JobFunc)

延迟添加只运行一次的循环任务,delay参数单位为秒

func DelayAddSingleton Uses

func DelayAddSingleton(delay time.Duration, interval time.Duration, job JobFunc)

延迟添加单例循环任务,delay参数单位为秒

func DelayAddTimes Uses

func DelayAddTimes(delay time.Duration, interval time.Duration, times int, job JobFunc)

延迟添加运行指定次数的循环任务,delay参数单位为秒

func Exit Uses

func Exit()

在Job方法中调用,停止并删除当前运行的任务。

func SetInterval Uses

func SetInterval(interval time.Duration, job JobFunc)

类似与js中的SetInterval,每隔一段时间执行指定回调函数。

func SetTimeout Uses

func SetTimeout(delay time.Duration, job JobFunc)

类似与js中的SetTimeout,一段时间后执行回调函数。

type Entry Uses

type Entry struct {
    // contains filtered or unexported fields
}

循环任务项

func Add Uses

func Add(interval time.Duration, job JobFunc) *Entry

添加执行方法。

Code:

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 {}

func AddEntry Uses

func AddEntry(interval time.Duration, job JobFunc, singleton bool, times int, status int) *Entry

添加执行方法,更多参数控制。

func AddOnce Uses

func AddOnce(interval time.Duration, job JobFunc) *Entry

添加只运行一次的循环任务。

func AddSingleton Uses

func AddSingleton(interval time.Duration, job JobFunc) *Entry

添加单例运行循环任务。

func AddTimes Uses

func AddTimes(interval time.Duration, times int, job JobFunc) *Entry

添加运行指定次数的循环任务。

func (*Entry) Close Uses

func (entry *Entry) Close()

关闭当前任务

func (*Entry) IsSingleton Uses

func (entry *Entry) IsSingleton() bool

是否单例运行

func (*Entry) Run Uses

func (entry *Entry) Run()

执行任务

func (*Entry) SetSingleton Uses

func (entry *Entry) SetSingleton(enabled bool)

设置单例运行

func (*Entry) SetStatus Uses

func (entry *Entry) SetStatus(status int) int

设置任务状态

func (*Entry) SetTimes Uses

func (entry *Entry) SetTimes(times int)

设置任务的运行次数

func (*Entry) Start Uses

func (entry *Entry) Start()

启动当前任务

func (*Entry) Status Uses

func (entry *Entry) Status() int

获取任务状态

func (*Entry) Stop Uses

func (entry *Entry) Stop()

停止当前任务

type JobFunc Uses

type JobFunc = func()

任务执行方法

type Timer Uses

type Timer struct {
    // contains filtered or unexported fields
}

定时器/分层时间轮

func New Uses

func New(slot int, interval time.Duration, level ...int) *Timer

创建分层时间轮

func (*Timer) Add Uses

func (t *Timer) Add(interval time.Duration, job JobFunc) *Entry

添加循环任务

func (*Timer) AddEntry Uses

func (t *Timer) AddEntry(interval time.Duration, job JobFunc, singleton bool, times int, status int) *Entry

添加定时任务

func (*Timer) AddOnce Uses

func (t *Timer) AddOnce(interval time.Duration, job JobFunc) *Entry

添加只运行一次的循环任务

func (*Timer) AddSingleton Uses

func (t *Timer) AddSingleton(interval time.Duration, job JobFunc) *Entry

添加单例运行循环任务

func (*Timer) AddTimes Uses

func (t *Timer) AddTimes(interval time.Duration, times int, job JobFunc) *Entry

添加运行指定次数的循环任务。

func (*Timer) Close Uses

func (t *Timer) Close()

关闭定时器

func (*Timer) DelayAdd Uses

func (t *Timer) DelayAdd(delay time.Duration, interval time.Duration, job JobFunc)

延迟添加循环任务。

func (*Timer) DelayAddEntry Uses

func (t *Timer) DelayAddEntry(delay time.Duration, interval time.Duration, job JobFunc, singleton bool, times int, status int)

延迟添加循环任务, 支持完整的参数。

func (*Timer) DelayAddOnce Uses

func (t *Timer) DelayAddOnce(delay time.Duration, interval time.Duration, job JobFunc)

延迟添加只运行一次的循环任务

func (*Timer) DelayAddSingleton Uses

func (t *Timer) DelayAddSingleton(delay time.Duration, interval time.Duration, job JobFunc)

延迟添加单例循环任务

func (*Timer) DelayAddTimes Uses

func (t *Timer) DelayAddTimes(delay time.Duration, interval time.Duration, times int, job JobFunc)

延迟添加只运行一次的循环任务

func (*Timer) Start Uses

func (t *Timer) Start()

启动定时器

func (*Timer) Stop Uses

func (t *Timer) Stop()

定制定时器

Package gtimer imports 5 packages (graph) and is imported by 8 packages. Updated 2019-06-24. Refresh now. Tools for package owners.