Documentation ¶
Overview ¶
Package clock is a low consumption, low latency support for frequent updates of large capacity timing manager:
1、能够添加一次性、重复性任务,并能在其执行前撤销或频繁更改。 2、支持同一时间点,多个任务提醒。 3、适用于中等密度,大跨度的单次、多次定时任务。 4、支持10万次/秒的定时任务执行、提醒、撤销或添加操作,平均延迟10微秒内 5、支持注册任务的函数调用,及事件通知。
基本处理逻辑:
1、重复性任务,流程是: a、注册重复任务 b、时间抵达时,控制器调用注册函数,并发送通知 c、如果次数达到限制,则撤销;否则,控制器更新该任务的下次执行时间点 d、控制器等待下一个最近需要执行的任务 2、一次性任务,可以是服务运行时,当前时间点之后的任意事件,流程是: a、注册一次性任务 b、时间抵达时,控制器调用注册函数,并发送通知 c、控制器释放该任务 d、控制器等待下一个最近需要执行的任务
使用方式,参见示例代码。
Index ¶
- type Clock
- func (jl *Clock) AddJobRepeat(interval time.Duration, actionMax uint64, jobFunc func()) (jobScheduled Job, inserted bool)
- func (jl *Clock) AddJobWithDeadtime(actionTime time.Time, jobFunc func()) (jobScheduled Job, inserted bool)
- func (jl *Clock) AddJobWithInterval(actionInterval time.Duration, jobFunc func()) (jobScheduled Job, inserted bool)
- func (jl *Clock) Count() uint64
- func (jl *Clock) Reset() *Clock
- func (jl *Clock) Stop()
- func (jl *Clock) StopGraceful()
- func (jl *Clock) UpdateJobTimeout(job Job, actionTime time.Duration) (updated bool)
- func (jl *Clock) WaitJobs() uint64
- type Job
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Clock ¶
type Clock struct {
// contains filtered or unexported fields
}
Clock is jobs schedule
func (*Clock) AddJobRepeat ¶
func (jl *Clock) AddJobRepeat(interval time.Duration, actionMax uint64, jobFunc func()) (jobScheduled Job, inserted bool)
AddJobRepeat add a repeat task with interval duration
@interval: The interval between two actions of the job @actionMax: The number of job execution @jobFunc: Callback function,not nil return @jobScheduled : A reference to a task that has been scheduled. @inserted : return false ,if interval is not Positiveor jobFunc is nil
Note: when jobTimes==0,the job will be executed without limitation。If you no longer use, be sure to call the DelJob method to release
Example ¶
ExampleClock_AddJobRepeat, show how to add repeat job。
var ( myClock = NewClock() ) fn := func() { fmt.Println("schedule repeat") } //create a task that executes three times,interval 50 millisecond _, inserted := myClock.AddJobRepeat(time.Duration(time.Millisecond*50), 3, fn) if !inserted { log.Println("failure") } //wait a second,watching time.Sleep(time.Second)
Output: schedule repeat schedule repeat schedule repeat
func (*Clock) AddJobWithDeadtime ¶
func (jl *Clock) AddJobWithDeadtime(actionTime time.Time, jobFunc func()) (jobScheduled Job, inserted bool)
AddJobWithDeadtime insert a timed task with time point after now
@actionTime: Execution start time. must after now @jobFunc: Callback function,not nil return @jobScheduled : A reference to a task that has been scheduled. @inserted : return false ,if actionTime before time.Now or jobFunc is nil
Example ¶
ExampleClock_AddJobWithDeadtime,show how to add a job with a point time
var ( myClock = Default() jobFunc = func() { fmt.Println("schedule once") } actionTime = time.Now().Add(time.Millisecond * 500) ) //创建一次性任务,定时500ms job, _ := myClock.AddJobWithDeadtime(actionTime, jobFunc) //任务执行前,撤销任务 time.Sleep(time.Millisecond * 300) job.Cancel() //等待2秒,正常情况下,事件不会再执行 time.Sleep(2 * time.Second)
Output:
func (*Clock) AddJobWithInterval ¶
func (jl *Clock) AddJobWithInterval(actionInterval time.Duration, jobFunc func()) (jobScheduled Job, inserted bool)
AddJobWithInterval insert a timed task with time duration after now
@actionTime: Duration after now @jobFunc: Callback function,not nil return @jobScheduled: A reference to a task that has been scheduled.
Example ¶
ExampleClock_AddJobWithInterval,show how to add a job just do once with interval
var ( jobClock = NewClock() jobFunc = func() { fmt.Println("schedule once") } ) //add a task that executes once,interval 100 millisecond jobClock.AddJobWithInterval(time.Duration(100*time.Millisecond), jobFunc) //wait a second,watching time.Sleep(1 * time.Second)
Output: schedule once
func (*Clock) StopGraceful ¶
func (jl *Clock) StopGraceful()
StopGracefull stop clock ,and do once every waiting job including Once\Reapeat Note:对于任务队列中,即使安排执行多次或者不限次数的,也仅仅执行一次。
func (*Clock) UpdateJobTimeout ¶
UpdateJobTimeout update a timed task with time duration after now
@job: job identifier @actionTime: new job schedule time,must be greater than 0