Documentation ¶
Overview ¶
* * 时间轮调度器 * 依赖模块 scheduled_task.go time_wheel.go
Index ¶
- Constants
- func UnixMilli() int64
- type Runnable
- type ScheduledPool
- func (p *ScheduledPool) CancelTimer(id uint32)
- func (p *ScheduledPool) CreateTimerAfter(df *Runnable, duration time.Duration) (uint32, error)
- func (p *ScheduledPool) CreateTimerAt(runnable *Runnable, unixNano int64) (uint32, error)
- func (p *ScheduledPool) GetTriggerChan() chan *Runnable
- func (p *ScheduledPool) Start()
- type ScheduledTask
- type TimeWheel
- func (tw *TimeWheel) AddScheduledTask(id uint32, scheduledTask *ScheduledTask) error
- func (tw *TimeWheel) AddTimeWheel(next *TimeWheel)
- func (tw *TimeWheel) GetScheduledTaskWithIn(duration time.Duration) map[uint32]*ScheduledTask
- func (tw *TimeWheel) RemoveScheduledTask(id uint32)
- func (tw *TimeWheel) Run()
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 ¶
Types ¶
type Runnable ¶
type Runnable struct {
// contains filtered or unexported fields
}
定义一个延迟调用函数 延迟调用函数就是 时间定时器超时的时候,触发的事先注册好的 回调函数
func NewRunnable ¶
func NewRunnable(run func(v ...interface{}), args []interface{}) *Runnable
创建一个延迟调用函数
type ScheduledPool ¶
调度池
func (*ScheduledPool) CreateTimerAfter ¶
创建一个延迟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
获取计时结束的延迟执行函数通道
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之后触发 定时器方法
type TimeWheel ¶
type TimeWheel struct { //互斥锁(继承RWMutex的 RWLock,UnLock 等方法) sync.RWMutex // contains filtered or unexported fields }
tips: 一个网络服务程序时需要管理大量客户端连接的, 其中每个客户端连接都需要管理它的 timeout 时间。 通常连接的超时管理一般设置为30~60秒不等,并不需要太精确的时间控制。 另外由于服务端管理着多达数万到数十万不等的连接数, 因此我们没法为每个连接使用一个Timer,那样太消耗资源不现实。 用时间轮的方式来管理和维护大量的timer调度,会解决上面的问题。
func NewTimeWheel ¶
创建一个时间轮 name:时间轮的名称 interval:每个刻度之间的duration时间间隔 scales:当前时间轮的轮盘一共多少个刻度(如我们正常的时钟就是12个刻度) maxCap: 每个刻度所最大保存的Timer定时器个数
func (*TimeWheel) AddScheduledTask ¶
func (tw *TimeWheel) AddScheduledTask(id uint32, scheduledTask *ScheduledTask) error
添加一个ScheduledTask到一个时间轮中(非时间轮自转情况)
func (*TimeWheel) AddTimeWheel ¶
给一个时间轮添加下层时间轮 比如给小时时间轮添加分钟时间轮,给分钟时间轮添加秒时间轮
func (*TimeWheel) GetScheduledTaskWithIn ¶
func (tw *TimeWheel) GetScheduledTaskWithIn(duration time.Duration) map[uint32]*ScheduledTask
获取定时器在一段时间间隔内的Timer
func (*TimeWheel) RemoveScheduledTask ¶
删除一个定时器,根据定时器的id
Click to show internal directories.
Click to hide internal directories.