liTimer

package
v0.0.0-...-f53a7f7 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

* * @Author: Aceld * @Date: 2019/4/30 11:57 * @Mail: danbing.at@gmail.com

* * @Author: Aceld * @Date: 2019/4/30 17:42 * @Mail: danbing.at@gmail.com

* * @Author: Aceld * @Date: 2019/5/8 17:43 * @Mail: danbing.at@gmail.com * * 时间轮调度器 * 依赖模块,delayfunc.go timer.go timewheel.go

* * @Author: Aceld * @Date: 2019/4/30 11:57 * @Mail: danbing.at@gmail.com

Index

Constants

View Source
const (
	HOUR_NAME     = "HOUR"
	HOUR_INTERVAL = 60 * 60 * 1e3 //ms为精度
	HOUR_SCALES   = 12

	MINUTE_NAME     = "MINUTE"
	MINUTE_INTERVAL = 60 * 1e3
	MINUTE_SCALES   = 60

	SECOND_NAME     = "SECOND"
	SECOND_INTERVAL = 1e3
	SECOND_SCALES   = 60

	TIMERS_MAX_CAP = 2048 //每个时间轮刻度挂载定时器的最大个数
)
View Source
const (
	//默认缓冲触发函数队列大小
	MAX_CHAN_BUFF = 2048
	//默认最大误差时间
	MAX_TIME_DELAY = 100
)

Variables

This section is empty.

Functions

func UnixMilli

func UnixMilli() int64

返回1970-1-1至今经历的毫秒数

Types

type DelayFunc

type DelayFunc struct {
	// contains filtered or unexported fields
}
   定义一个延迟调用函数
	延迟调用函数就是 时间定时器超时的时候,触发的事先注册好的
	回调函数

func NewDelayFunc

func NewDelayFunc(f func(v ...interface{}), args []interface{}) *DelayFunc

创建一个延迟调用函数

func (*DelayFunc) Call

func (df *DelayFunc) Call()

执行延迟函数---如果执行失败,抛出异常

func (*DelayFunc) String

func (df *DelayFunc) String() string

打印当前延迟函数的信息,用于日志记录

type TimeWheel

type TimeWheel struct {

	//互斥锁(继承RWMutex的 RWLock,UnLock 等方法)
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewTimeWheel

func NewTimeWheel(name string, interval int64, scales int, maxCap int) *TimeWheel

创建一个时间轮 name:时间轮的名称 interval:每个刻度之间的duration时间间隔 scales:当前时间轮的轮盘一共多少个刻度(如我们正常的时钟就是12个刻度) maxCap: 每个刻度所最大保存的Timer定时器个数

func (*TimeWheel) AddTimeWheel

func (tw *TimeWheel) AddTimeWheel(next *TimeWheel)

给一个时间轮添加下层时间轮 比如给小时时间轮添加分钟时间轮,给分钟时间轮添加秒时间轮

func (*TimeWheel) AddTimer

func (tw *TimeWheel) AddTimer(tid uint32, t *Timer) error

添加一个timer到一个时间轮中(非时间轮自转情况)

func (*TimeWheel) GetTimerWithIn

func (tw *TimeWheel) GetTimerWithIn(duration time.Duration) map[uint32]Timer

获取定时器在一段时间间隔内的Timer

func (*TimeWheel) RemoveTimer

func (tw *TimeWheel) RemoveTimer(tid uint32)

删除一个定时器,根据定时器的id

func (*TimeWheel) Run

func (tw *TimeWheel) Run()

非阻塞的方式让时间轮转起来

type Timer

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

定时器实现

func NewTimerAfter

func NewTimerAfter(duration time.Duration, f func(v ...interface{}), args []interface{}) *Timer

创建一个定时器,在当前时间延迟duration之后触发 定时器方法

func NewTimerAt

func NewTimerAt(unixNano int64, f func(v ...interface{}), args []interface{}) *Timer
   创建一个定时器,在指定的时间触发 定时器方法
	df: DelayFunc类型的延迟调用函数类型
	unixNano: unix计算机从1970-1-1至今经历的纳秒数

func NewTimerInterval

func NewTimerInterval(duration time.Duration, times int, f func(v ...interface{}), args []interface{}) *Timer

func (*Timer) Run

func (t *Timer) Run()

启动定时器,用一个go承载,启动就无法停止

type TimerScheduler

type TimerScheduler struct {

	//互斥锁
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewAutoExecTimerScheduler

func NewAutoExecTimerScheduler() *TimerScheduler

时间轮定时器 自动调度

func NewTimerScheduler

func NewTimerScheduler() *TimerScheduler

返回一个定时器调度器

主要创建分层定时器,并做关联,并依次启动

func (*TimerScheduler) CancelTimer

func (this *TimerScheduler) CancelTimer(tid uint32)

删除timer

func (*TimerScheduler) CreateTimerAt

func (this *TimerScheduler) CreateTimerAt(unixNano int64, f func(v ...interface{}), args []interface{}) (uint32, error)

创建一个定点Timer 并将Timer添加到分层时间轮中, 返回Timer的tid

func (*TimerScheduler) GetTriggerChan

func (this *TimerScheduler) GetTriggerChan() chan *DelayFunc

获取计时结束的延迟执行函数通道

func (*TimerScheduler) NewTimerAfter

func (this *TimerScheduler) NewTimerAfter(duration time.Duration, f func(v ...interface{}), args []interface{}) (uint32, error)

创建一个延迟Timer 并将Timer添加到分层时间轮中, 返回Timer的tid

func (*TimerScheduler) NewTimerInterval

func (this *TimerScheduler) NewTimerInterval(duration time.Duration, times int, f func(v ...interface{}), args []interface{}) (uint32, error)

func (*TimerScheduler) Start

func (this *TimerScheduler) Start()

非阻塞的方式启动timerSchedule

Jump to

Keyboard shortcuts

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