concurrent

package
v0.0.28 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

* * 时间轮调度器 * 依赖模块 scheduled_task.go time_wheel.go

Index

Constants

View Source
const (
	//默认缓冲触发函数队列大小
	MaxChanBuff = 2048
	//默认最大误差时间
	MaxTimeDelay = 100
)
View Source
const (
	HourName     = "HOUR"
	HourInterval = 60 * 60 * 1e3 //ms为精度
	HourScales   = 12

	MinuteName     = "MINUTE"
	MinuteInterval = 60 * 1e3
	MinuteScales   = 60

	SecondName     = "SECOND"
	SecondInterval = 1e3
	SecondScales   = 60

	TimersMaxCap = 2048 //每个时间轮刻度挂载定时器的最大个数
)

Variables

This section is empty.

Functions

func UnixMilli

func UnixMilli() int64

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

Types

type Runnable

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

func NewRunnable

func NewRunnable(run func(v ...interface{}), args []interface{}) *Runnable

创建一个延迟调用函数

func (*Runnable) Run

func (r *Runnable) Run()

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

func (*Runnable) String

func (r *Runnable) String() string

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

type ScheduledPool

type ScheduledPool struct {

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

调度池

func NewAutoExecuteScheduledPool

func NewAutoExecuteScheduledPool() *ScheduledPool

时间轮定时器 自动调度

func NewScheduledPool

func NewScheduledPool() *ScheduledPool

返回一个定时器调度器

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

func (*ScheduledPool) CancelTimer

func (p *ScheduledPool) CancelTimer(id uint32)

删除timer

func (*ScheduledPool) CreateTimerAfter

func (p *ScheduledPool) CreateTimerAfter(df *Runnable, duration time.Duration) (uint32, error)

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

func (*ScheduledPool) CreateTimerAt

func (p *ScheduledPool) CreateTimerAt(runnable *Runnable, unixNano int64) (uint32, error)

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

func (*ScheduledPool) GetTriggerChan

func (p *ScheduledPool) GetTriggerChan() chan *Runnable

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

func (*ScheduledPool) Start

func (p *ScheduledPool) Start()

非阻塞的方式启动timerSchedule,只取出,不执行任务

type ScheduledTask

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

时间任务

func NewScheduledTask

func NewScheduledTask(runnable *Runnable, unixNano int64) *ScheduledTask
   创建一个定时器,在指定的时间触发 定时器方法
	runnable: Runnable类型的延迟调用函数类型
	unixNano: unix计算机从1970-1-1至今经历的纳秒数

func NewScheduledTaskAfter

func NewScheduledTaskAfter(runnable *Runnable, duration time.Duration) *ScheduledTask

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

func (*ScheduledTask) Run

func (s *ScheduledTask) Run()

启动定时器,用一个go承载

type TimeWheel

type TimeWheel struct {

	//互斥锁(继承RWMutex的 RWLock,UnLock 等方法)
	sync.RWMutex
	// contains filtered or unexported fields
}
  tips:
	一个网络服务程序时需要管理大量客户端连接的,
	其中每个客户端连接都需要管理它的 timeout 时间。
	通常连接的超时管理一般设置为30~60秒不等,并不需要太精确的时间控制。
	另外由于服务端管理着多达数万到数十万不等的连接数,
	因此我们没法为每个连接使用一个Timer,那样太消耗资源不现实。

	用时间轮的方式来管理和维护大量的timer调度,会解决上面的问题。

func NewTimeWheel

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

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

func (*TimeWheel) AddScheduledTask

func (tw *TimeWheel) AddScheduledTask(id uint32, scheduledTask *ScheduledTask) error

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

func (*TimeWheel) AddTimeWheel

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

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

func (*TimeWheel) GetScheduledTaskWithIn

func (tw *TimeWheel) GetScheduledTaskWithIn(duration time.Duration) map[uint32]*ScheduledTask

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

func (*TimeWheel) RemoveScheduledTask

func (tw *TimeWheel) RemoveScheduledTask(id uint32)

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

func (*TimeWheel) Run

func (tw *TimeWheel) Run()

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

Jump to

Keyboard shortcuts

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