Clock
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")
}
)
myClock.AddJobWithInterval(time.Duration(100*time.Millisecond), jobFunc)
time.Sleep(1 * time.Second)
add repeat task that executes three times
func ExampleClock_AddJobRepeat() {
var (
myClock = NewClock()
)
fn := func() {
fmt.Println("schedule repeat")
}
_, inserted := myClock.AddJobRepeat(time.Duration(time.Millisecond*200), 3, fn)
if !inserted {
log.Println("failure")
}
time.Sleep(time.Second)
}
rm a task before its execution
func ExampleClock_RmJob(){
var (
myClock = NewClock()
count int
jobFunc = func() {
count++
fmt.Println("do ",count)
}
)
job, _ := myClock.AddJobRepeat(time.Second*1,2, jobFunc)
time.Sleep(time.Millisecond*500)
job.Cancel()
time.Sleep(3 * time.Second)
}
more examples