timer

package
v0.33.9 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package timer 定时器,时间轮 推荐使用 timer-pool.go 中的 TimerWheelPool 而不是直接使用 TimerWheel

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler func(t time.Time)

Handler 任务执行回调函数

type Task

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

Task 表示一个将要执行的任务,存储于 Slot 中

type TimerWheel

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

TimerWheel 时间轮

func New

func New(interval time.Duration, slotNum int) *TimerWheel

New 创建一个时间轮

func (*TimerWheel) AddCron

func (tw *TimerWheel) AddCron(hour, minute, second int, times int, callback Handler) *Task

AddCron 添加一个指定时间执行的任务

示例:每天早上 5 点执行,AddCron(5, 0, 0, -1, true, func() {})

func (*TimerWheel) AddMonthCron

func (tw *TimerWheel) AddMonthCron(dayOfMonth, hour, minute, second int, times int, callback Handler) *Task

AddMonthCron 添加一个每月指定时间执行的任务 dayOfMonth: 1-31

示例:每月 1 号早上 5 点执行,AddMonthCron(1, 5, 0, 0, -1, true, func() {})

func (*TimerWheel) AddTask

func (tw *TimerWheel) AddTask(delay time.Duration, times int, callback Handler) *Task

AddTask 添加一个任务 delay 延迟时间 times 重复执行次数,-1 表示永久重复执行 callback 时间到之后的回调

func (*TimerWheel) AddWeekCron

func (tw *TimerWheel) AddWeekCron(week, hour, minute, second int, times int, callback Handler) *Task

AddWeekCron 添加一个每周指定时间执行的任务 week: 1-7 表示周一到周七

示例:每周一早上 5 点执行,AddWeekCron(1, 5, 0, 0, -1, true, func() {})

func (*TimerWheel) AddYearDayCron

func (tw *TimerWheel) AddYearDayCron(month, dayOfMonth, hour, minute, second int, times int, callback Handler) *Task

AddYearDayCron 添加一个具体日期执行的任务 month: 1-12 dayOfMonth: 1-31

示例:每年6月1日早上 5 点执行

func (*TimerWheel) Close

func (tw *TimerWheel) Close()

Close 停止并关闭时间轮

func (*TimerWheel) Start

func (tw *TimerWheel) Start()

Start 启动时间轮

type TimerWheelPool

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

TimerWheelPool 时间轮池 推荐使用,而不是直接使用 TimerWheel 可以将任务分发在多个时间轮中

func NewPool

func NewPool(poolsize int, interval time.Duration, slotNum int) *TimerWheelPool

NewPool 创建一个时间轮池

func (*TimerWheelPool) AddCron

func (twp *TimerWheelPool) AddCron(hour, minute, second int, times int, callback Handler) *Task

AddCron 添加一个指定时间执行的任务

示例:每天早上 5 点执行,AddCron(5, 0, 0, -1, true, func() {})

func (*TimerWheelPool) AddMonthCron

func (twp *TimerWheelPool) AddMonthCron(dayOfMonth, hour, minute, second int, times int, callback Handler) *Task

AddMonthCron 添加一个每月指定时间执行的任务 dayOfMonth: 1-31

示例:每月 1 号早上 5 点执行,AddMonthCron(1, 5, 0, 0, -1, true, func() {})

func (*TimerWheelPool) AddTask

func (twp *TimerWheelPool) AddTask(delay time.Duration, times int, callback Handler) *Task

AddTask 添加一个任务 delay 延迟时间 times 重复执行次数,-1 表示永久重复执行 callback 时间到之后的回调

func (*TimerWheelPool) AddWeekCron

func (twp *TimerWheelPool) AddWeekCron(week, hour, minute, second int, times int, callback Handler) *Task

AddWeekCron 添加一个每周指定时间执行的任务 week: 1-7 表示周一到周七

示例:每周一早上 5 点执行,AddWeekCron(1, 5, 0, 0, -1, true, func() {})

func (*TimerWheelPool) AddYearDayCron

func (twp *TimerWheelPool) AddYearDayCron(month, dayOfMonth, hour, minute, second int, times int, callback Handler) *Task

AddYearDayCron 添加一个具体日期执行的任务 month: 1-12 dayOfMonth: 1-31

示例:每年6月1日早上 5 点执行

func (*TimerWheelPool) Close

func (twp *TimerWheelPool) Close()

func (*TimerWheelPool) Start

func (twp *TimerWheelPool) Start()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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