Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/alex023/clock

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/alex023/clock

  • v0.0.0-20191208111215-c265f1b2ab18
  • Source
  • Go
  • Socket score

Version published
Created
Source

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

FAQs

Package last updated on 08 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc