timewheel

package
v1.13.8 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2023 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 3 more Imports: 3 Imported by: 1

README

timewheel

为心跳上报定制实现的、线程安全的多层时间轮,简化功能以追求更好的性能。

基于链表和ticker实现。 只有插入操作:插入任务后就必须执行。

测试/压测记录

覆盖率: 90%+

  1. 功能测试:正常

  2. 压力测试: 16c 16g机器

    开启5w个协程并发往同一个slot加10w个任务,单个操作用时280ns

    goos: windows
    goarch: amd64
    pkg: github.com/polarismesh/polaris-server/common/timewheel
    BenchmarkAddTask1-8   	  100000	       280 ns/op	     103 B/op	       2 allocs/op
    PASS
    

    开启16w个协程并发往同一个slot加500w个任务,单个操作用时376ns

    goos: windows
    goarch: amd64
    pkg: github.com/polarismesh/polaris-server/common/timewheel
    BenchmarkAddTask1-8   	 5000000	       376 ns/op	      97 B/op	       2 allocs/op
    PASS
    

    对比nosix/timewheel:

    goos: linux
    goarch: amd64
    BenchmarkAddTask1-8   	  100000	      2021 ns/op	     721 B/op	       3 allocs/op
    --- BENCH: BenchmarkAddTask1-8
        timewheel_test.go:40: N:100000, use time:255.514068ms
        timewheel_test.go:40: N:100000, use time:324.360456ms
        timewheel_test.go:40: N:100000, use time:402.377702ms
        timewheel_test.go:40: N:100000, use time:419.118132ms
        timewheel_test.go:40: N:100000, use time:195.719517ms
        timewheel_test.go:40: N:100000, use time:215.3815ms
        timewheel_test.go:40: N:100000, use time:176.733241ms
        timewheel_test.go:40: N:100000, use time:188.846803ms
        timewheel_test.go:40: N:100000, use time:164.038559ms
        timewheel_test.go:40: N:100000, use time:200.684048ms
    

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Callback

type Callback func(interface{})

Callback 时间轮回调函数定义

type Option

type Option func(*options)

func WithWaitTaskOnClose

func WithWaitTaskOnClose(waitTaskOnClose bool) Option

type Task

type Task struct {
	// contains filtered or unexported fields
}

Task 时间轮任务结构体

type TimeWheel

type TimeWheel struct {
	// contains filtered or unexported fields
}

TimeWheel 时间轮结构体

func New

func New(interval time.Duration, slotNum int, name string, opts ...Option) *TimeWheel

New 初始化时间轮

func (*TimeWheel) AddTask

func (tw *TimeWheel) AddTask(delayMilli uint32, data interface{}, cb Callback)

AddTask 新增时间轮任务

func (*TimeWheel) Start

func (tw *TimeWheel) Start()

Start 启动时间轮

func (*TimeWheel) Stop

func (tw *TimeWheel) Stop()

Stop 停止时间轮

Jump to

Keyboard shortcuts

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