dtask

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2022 License: MIT Imports: 7 Imported by: 0

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

Constants

View Source
const (
	// Debug debug switch
	Debug = false
)

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 Default

func Default() *Clock

Default return singal default clock

func NewClock

func NewClock() *Clock

NewClock Create a task queue controller

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

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

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.

func (*Clock) Count

func (jl *Clock) Count() uint64

Count 已经执行的任务数。对于重复任务,会计算多次

func (*Clock) Reset

func (jl *Clock) Reset() *Clock

重置Clock的内部状态

func (*Clock) Stop

func (jl *Clock) Stop()

Stop stop clock , and cancel all waiting jobs

func (*Clock) StopGraceful

func (jl *Clock) StopGraceful()

StopGracefull stop clock ,and do once every waiting job including Once\Reapeat Note:对于任务队列中,即使安排执行多次或者不限次数的,也仅仅执行一次。

func (*Clock) UpdateJobTimeout

func (jl *Clock) UpdateJobTimeout(job Job, actionTime time.Duration) (updated bool)

UpdateJobTimeout update a timed task with time duration after now

@job:			job identifier
@actionTime:	new job schedule time,must be greater than 0

func (*Clock) WaitJobs

func (jl *Clock) WaitJobs() uint64

WaitJobs get how much jobs waiting for call

type Job

type Job interface {
	C() <-chan Job //C Get a Chan,which can get message if Job is executed
	Count() uint64 //计数器,表示已执行(或触发)的次数
	Max() uint64   //允许执行的最大次数
	Cancel()       //撤销加载的任务,不再定时执行
	// contains filtered or unexported methods
}

This code is based on the following resources: source code: https://github.com/alex023/clock.git

Job External access interface for timed tasks

type Task

type Task struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewTask

func NewTask() *Task

func (*Task) Add

func (t *Task) Add(key string, job Job)

func (*Task) AddJobRepeat

func (t *Task) AddJobRepeat(interval time.Duration, actionMax uint64, jobFunc func()) (jobScheduled Job, inserted bool)

func (*Task) Get

func (t *Task) Get(key string) Job

func (*Task) Stop

func (t *Task) Stop()

Directories

Path Synopsis
cmd
client command
pkg
rbtree
Package rbtree implements operations on Red-Black tree.
Package rbtree implements operations on Red-Black tree.

Jump to

Keyboard shortcuts

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