GithubHelp home page GithubHelp logo

lingling1420q / clock Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alex023/clock

0.0 1.0 0.0 80 KB

A low consumption, low latency support for frequent updates of large capcity timing manage

License: Apache License 2.0

Go 100.00%

clock's Introduction

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

clock's People

Contributors

alex023 avatar robertxiaohui avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.