timer

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: MIT Imports: 9 Imported by: 0

README

timer

Go codecov

timer是高性能定时器库

feature

  • 支持一次性定时器
  • 支持周期性定时器
  • 支持多种数据结构后端,最小堆,5级时间轮

一次性定时器

import (
    "github.com/gyb1314/timer"
    "log"
)

func main() {
        tm := timer.NewTimer()

        tm.AfterFunc(1*time.Second, func() {
                log.Printf("after\n")
        })

        tm.AfterFunc(10*time.Second, func() {
                log.Printf("after\n")
        })
        tm.Run()
}

周期性定时器

import (
    "github.com/gyb1314/timer"
    "log"
)

func main() {
        tm := timer.NewTimer()

        tm.ScheduleFunc(1*time.Second, func() {
                log.Printf("schedule\n")
        })

        tm.Run()
}

自定义周期性定时器

实现时间翻倍定时的例子

type curstomTest struct {
	count int
}
// 只要实现Next接口就行
func (c *curstomTest) Next(now time.Time) (rv time.Time) {
	rv = now.Add(time.Duration(c.count) * time.Millisecond * 10)
	c.count++
	return
}

func main() {
        tm := timer.NewTimer(timer.WithMinHeap())
        node := tm.CustomFunc(&curstomTest{count: 1}, func() {
                log.Printf("%v\n", time.Now())
        })
        tm.Run()
}

取消某一个定时器

import (
	"log"
	"time"

	"github.com/gyb1314/timer"
)

func main() {

	tm := timer.NewTimer()

	// 只会打印2 time.Second
	tm.AfterFunc(2*time.Second, func() {
		log.Printf("2 time.Second")
	})

	// tk3 会被 tk3.Stop()函数调用取消掉
	tk3 := tm.AfterFunc(3*time.Second, func() {
		log.Printf("3 time.Second")
	})

	tk3.Stop() //取消tk3

	tm.Run()
}

选择不同的的数据结构

import (
    "github.com/gyb1314/timer"
    "log"
)

func main() {
        tm := timer.NewTimer(timer.WithMinHeap())// 选择最小堆,默认时间轮
}

benchmark

github.com/gyb1314/timer 性能最高

goos: linux
goarch: amd64
pkg: benchmark
Benchmark_antlabs_Timer_AddTimer/N-1m-16        	 9177537	       124 ns/op
Benchmark_antlabs_Timer_AddTimer/N-5m-16        	10152950	       128 ns/op
Benchmark_antlabs_Timer_AddTimer/N-10m-16       	 9955639	       127 ns/op
Benchmark_RussellLuo_Timingwheel_AddTimer/N-1m-16         	 5316916	       222 ns/op
Benchmark_RussellLuo_Timingwheel_AddTimer/N-5m-16         	 5848843	       218 ns/op
Benchmark_RussellLuo_Timingwheel_AddTimer/N-10m-16        	 5872621	       231 ns/op
Benchmark_ouqiang_Timewheel/N-1m-16                       	  720667	      1622 ns/op
Benchmark_ouqiang_Timewheel/N-5m-16                       	  807018	      1573 ns/op
Benchmark_ouqiang_Timewheel/N-10m-16                      	  666183	      1557 ns/op
Benchmark_Stdlib_AddTimer/N-1m-16                         	 8031864	       144 ns/op
Benchmark_Stdlib_AddTimer/N-5m-16                         	 8437442	       151 ns/op
Benchmark_Stdlib_AddTimer/N-10m-16                        	 8080659	       167 ns/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Next

type Next interface {
	Next(time.Time) time.Time
}

type Option

type Option func(c *option)

func WithMinHeap

func WithMinHeap() Option

func WithRbtree

func WithRbtree() Option

TODO

func WithSkipList

func WithSkipList() Option

TODO

func WithTimeWheel

func WithTimeWheel() Option

type Time

type Time struct {
	sync.Mutex
	// contains filtered or unexported fields
}

先使用sync.Mutex实现功能 后面使用cas优化

func (*Time) Stop

func (t *Time) Stop()

一个timeNode节点有4个状态 1.存在于初始化链表中 2.被移动到tmp链表 3.1 和 3.2是if else的状态

3.1被移动到new链表
3.2直接执行

1和3.1状态是没有问题的 2和3.2状态会是没有锁保护下的操作,会有数据竞争

type TimeNoder

type TimeNoder interface {
	Stop()
}

停止单个定时器

type Timer

type Timer interface {
	// 一次性定时器
	AfterFunc(expire time.Duration, callback func()) TimeNoder

	// 周期性定时器
	ScheduleFunc(expire time.Duration, callback func()) TimeNoder

	// 自定义下次的时间
	CustomFunc(n Next, callback func()) TimeNoder

	// 运行
	Run()

	// 停止所有定时器
	Stop()
}

定时器接口

func NewTimer

func NewTimer(opt ...Option) Timer

定时器构造函数

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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