Coder Social home page Coder Social logo

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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

clock's Issues

timer crash

@alex023

clock version:0.7

panic: send on closed channel

goroutine 27 [running]:
gitlab/service/timer.(*jobItem).action(0xc4202f10a0, 0xc4201aaa01)
/home/gofile/src/service/timer/job.go:57 +0x94
gitlab/service/timer.(*Clock).schedule(0xc420234510)
/home/gofile/src/service/timer/clock.go:132 +0x205
created by gitlab/service/timer.(*Clock).start
/home/gofile/src/service/timer/clock.go:85 +0xff

timer crash

panic: send on closed channel

goroutine 39 [running]:
gitlab.**/timer.(*jobItem).action(0xc424d59960, 0xc4201ae801)
/home/gofile/src/service/timer/job.go:57 +0x99
/home/gofile/src/service/timer.(*Clock).schedule(0xc420237e30)
/home/gofile/src/service/timer/clock.go:129 +0x205
created by /home/gofile/src/service/timer.(*Clock).start
/home/gofile/src/service/timer/clock.go:85 +0x111

I used the old version, the specific version number can not remember
thanks!!!

服务器重启问题

你好,我现在是将所有任务放在一个单例当中,但是当服务器挂了或者重启后,单例中任务都会消失,这种情况应该怎么处理呢?

timer crash

panic: runtime error: index out of range [recovered]
panic: runtime error: index out of range

goroutine 2078 [running]:
gitlab.**/service/timer.(*jobItem).call.func1()
/home/gofile/src/service/timer/job.go:65 +0x52
panic(0xc306c0, 0x11eefb0)
/usr/local/go/src/runtime/panic.go:489 +0x2cf
thanks!!!

help

C:\Go\src\github.com\alex023\clock>go test
--- FAIL: TestClock_Create (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recover
ed]
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x4012ab]

goroutine 5 [running]:
testing.tRunner.func1(0x11978090)
C:/Go/src/testing/testing.go:711 +0x283
panic(0x506e20, 0x5beb18)
C:/Go/src/runtime/panic.go:491 +0x1d0
sync/atomic.LoadUint64(0x1195e0a4, 0x1195e090, 0x5d41e0)
C:/Go/src/sync/atomic/asm_386.s:159 +0xb
github.com/alex023/clock.(*Clock).WaitJobs(0x1195e090, 0x1195e090, 0xfa7b4a21)
C:/Go/src/github.com/alex023/clock/clock.go:307 +0x26
github.com/alex023/clock.TestClock_Create(0x11978090)
C:/Go/src/github.com/alex023/clock/clock_test.go:33 +0x33
testing.tRunner(0x11978090, 0x52b240)
C:/Go/src/testing/testing.go:746 +0xa6
created by testing.(*T).Run
C:/Go/src/testing/testing.go:789 +0x251
exit status 2
FAIL github.com/alex023/clock 0.046s

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.