Documentation ¶
Index ¶
- Constants
- type Entry
- func (entry *Entry) Close()
- func (entry *Entry) GetStatus() string
- 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()
- func (entry *Entry) String() string
- type JobFunc
- type Timer
- func (t *Timer) Add(name string, interval time.Duration, job JobFunc, jobp interface{}) *Entry
- func (t *Timer) AddNow(name string, interval time.Duration, job JobFunc, jobp interface{}) *Entry
- func (t *Timer) AddOnce(name string, interval time.Duration, job JobFunc, jobp interface{}) *Entry
- func (t *Timer) AddSingleton(name string, interval time.Duration, job JobFunc, jobp interface{}) *Entry
- func (t *Timer) AddTimes(name string, interval time.Duration, times int, job JobFunc, jobp interface{}) *Entry
- func (t *Timer) Close()
- func (t *Timer) DelayAdd(name string, delay time.Duration, interval time.Duration, job JobFunc, ...)
- func (t *Timer) DelayAddOnce(name string, delay time.Duration, interval time.Duration, job JobFunc, ...)
- func (t *Timer) DelayAddSingleton(name string, delay time.Duration, interval time.Duration, job JobFunc, ...)
- func (t *Timer) DelayAddTimes(name string, delay time.Duration, interval time.Duration, times int, ...)
- func (t *Timer) Start()
- func (t *Timer) Stop()
- func (t *Timer) String() string
Constants ¶
const ( STATUS_READY = 0 STATUS_RUNNING = 1 STATUS_STOPPED = 2 STATUS_CLOSED = -1 PANIC_EXIT = "exit" DEFAULT_TIMES = math.MaxInt32 )
注意事项:
任何的定时任务都是有误差的,在时间轮刻度比较大,或者并发量大,负载较高的系统中尤其明显,具体请参考:https://github.com/golang/go/issues/14410 间隔不会考虑任务的执行时间。例如,如果一项工作需要3分钟才能执行完成,并且计划每隔5分钟运行一次,那么每次任务之间只有2分钟的空闲时间。 需要注意的是单例模式运行的定时任务,任务的执行时间会影响该任务下一次执行的开始时间。例如:一个每秒执行的任务,运行耗时为1秒,那么在第1秒开始运行后,下一次任务将会在第3秒开始执行。
简要说明:
New 方法用于创建自定义的任务定时器对象:
- slot 参数用于指定每个时间轮的槽数;
- interval 参数用于指定定时器的最小tick时间间隔;
- level 为非必需参数,用于自定义分层时间轮的层数,默认为6;
Add 方法用于添加定时任务,其中:
- interval 参数用于指定方法的执行的时间间隔;
- job 参数为需要执行的任务方法(方法地址);
AddEntry 方法添加定时任务,支持更多参数的控制; AddSingleton 方法用于添加单例定时任务,即同时只能有一个该任务正在运行; AddOnce 方法用于添加只运行一次的定时任务,当运行一次数后该定时任务自动销毁; AddTimes 方法用于添加运行指定次数的定时任务,当运行times次数后该定时任务自动销毁; Search 方法用于根据名称进行定时任务搜索(返回定时任务*Entry对象指针); Start 方法用于启动定时任务(Add后自动启动定时任务); Stop 方法用于停止定时任务; Close 方法用于关闭定时器;
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Entry ¶
type Entry struct {
// contains filtered or unexported fields
}
Entry is the timing job entry to wheel.
func (*Entry) Close ¶
func (entry *Entry) Close()
Close closes the job, and then it will be removed from the timer.
func (*Entry) IsSingleton ¶
IsSingleton checks and returns whether the job in singleton mode.
func (*Entry) SetSingleton ¶
SetSingleton sets the job singleton mode.
type Timer ¶
type Timer struct {
// contains filtered or unexported fields
}
Wheel is a slot wrapper for timing job install and uninstall.
func NewTimerPlus ¶
创建并返回Hierarchical Timing Wheel设计的计时器。slot - 时间轮槽数; interval - 间隔时间 level - 分层大小
func (*Timer) AddSingleton ¶
func (t *Timer) AddSingleton(name string, interval time.Duration, job JobFunc, jobp interface{}) *Entry
添加单例定时任务,即同时只能有一个该任务正在运行;
func (*Timer) AddTimes ¶
func (t *Timer) AddTimes(name string, interval time.Duration, times int, job JobFunc, jobp interface{}) *Entry
添加运行指定次数的定时任务,当运行times次数后该定时任务自动销毁;