clock

package module
v0.0.0-...-c265f1b Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2019 License: Apache-2.0 Imports: 7 Imported by: 17

README

Clock

License Go Report Card GoDoc Build Status

Brief

Timing task manager based on red black tree in memory

Feature

  • support task function call, and event notifications
  • support task that executes once or several times
  • support task cancel which added
  • fault isolation
  • 100k/s operation

last indicator may not be available on the cloud server,see more test code

Example

add a task that executes once

   var (
		myClock = NewClock()
		jobFunc  = func() {
			fmt.Println("schedule once")
		}
	)
	//add a task that executes once,interval 100 millisecond
	myClock.AddJobWithInterval(time.Duration(100*time.Millisecond), jobFunc)

	//wait a second,watching 
	time.Sleep(1 * time.Second)

	//Output:
	//
	//schedule once

add repeat task that executes three times

func ExampleClock_AddJobRepeat() {
	var (
   		myClock = NewClock()
   	)
   	//define a repeat task 
   	fn := func() {
   		fmt.Println("schedule repeat")
   	}
   	//add in clock,execute three times,interval 200 millisecond
   	_, inserted := myClock.AddJobRepeat(time.Duration(time.Millisecond*200), 3, fn)
   	if !inserted {
   		log.Println("failure")
   	}
    	//wait a second,watching 
   	time.Sleep(time.Second)
   	//Output:
   	//
   	//schedule repeat
   	//schedule repeat
   	//schedule repeat
}

rm a task before its execution

func ExampleClock_RmJob(){
   var (
   	myClock = NewClock()
   	count int
   	jobFunc = func() {
   		count++
   		fmt.Println("do ",count)
   	}
   )
   //创建任务,间隔1秒,执行两次
   job, _ := myClock.AddJobRepeat(time.Second*1,2, jobFunc)

   //任务执行前,撤销任务
   time.Sleep(time.Millisecond*500)
   job.Cancel()

   //等待3秒,正常情况下,事件不会再执行
   time.Sleep(3 * time.Second)

   //Output:
   //
   //
}

more examples

event notify
TTL Session

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

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

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

Job External access interface for timed tasks

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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