timewheel

package
v1.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
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) GetStatus

func (entry *Entry) GetStatus() string

获取任务状态

func (*Entry) IsSingleton

func (entry *Entry) IsSingleton() bool

IsSingleton checks and returns whether the job in singleton mode.

func (*Entry) Run

func (entry *Entry) Run()

Run runs the job.

func (*Entry) SetSingleton

func (entry *Entry) SetSingleton(enabled bool)

SetSingleton sets the job singleton mode.

func (*Entry) SetStatus

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

SetStatus custom sets the status for the job. 设置任务状态

func (*Entry) SetTimes

func (entry *Entry) SetTimes(times int)

SetTimes sets the limit running times for the job.

func (*Entry) Start

func (entry *Entry) Start()

Start starts the job.

func (*Entry) Status

func (entry *Entry) Status() int

获取任务状态

func (*Entry) Stop

func (entry *Entry) Stop()

Stop stops the job. 关闭当前任务

func (*Entry) String

func (entry *Entry) String() string

type JobFunc

type JobFunc = func(string, time.Time, time.Duration, interface{}) //作业功能

type Timer

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

Wheel is a slot wrapper for timing job install and uninstall.

func NewTimer

func NewTimer() *Timer

func NewTimerPlus

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

创建并返回Hierarchical Timing Wheel设计的计时器。slot - 时间轮槽数; interval - 间隔时间 level - 分层大小

func (*Timer) Add

func (t *Timer) Add(name string, interval time.Duration, job JobFunc, jobp interface{}) *Entry

添加定时任务

func (*Timer) AddNow

func (t *Timer) AddNow(name string, interval time.Duration, job JobFunc, jobp interface{}) *Entry

添加定时任务

func (*Timer) AddOnce

func (t *Timer) AddOnce(name string, interval time.Duration, job JobFunc, jobp interface{}) *Entry

添加只运行一次的定时任务,当运行一次数后该定时任务自动销毁;

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次数后该定时任务自动销毁;

func (*Timer) Close

func (t *Timer) Close()

func (*Timer) DelayAdd

func (t *Timer) DelayAdd(name string, delay time.Duration, interval time.Duration, job JobFunc, jobp interface{})

func (*Timer) DelayAddOnce

func (t *Timer) DelayAddOnce(name string, delay time.Duration, interval time.Duration, job JobFunc, jobp interface{})

func (*Timer) DelayAddSingleton

func (t *Timer) DelayAddSingleton(name string, delay time.Duration, interval time.Duration, job JobFunc, jobp interface{})

func (*Timer) DelayAddTimes

func (t *Timer) DelayAddTimes(name string, delay time.Duration, interval time.Duration, times int, job JobFunc, jobp interface{})

func (*Timer) Start

func (t *Timer) Start()

func (*Timer) Stop

func (t *Timer) Stop()

func (*Timer) String

func (t *Timer) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL