timewheel

package module
v0.0.0-...-319c01e Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2022 License: MIT Imports: 2 Imported by: 0

README

TimeWheel

Golang实现的简单时间轮算法

原理

https://cloud.tencent.com/developer/article/1815722

安装

go get -u github.com/MrWater233/timewheel

使用

package main

import (
	"github.com/MrWater233/timewheel"
	"fmt"
	"time"
)

func main() {
	// 创建时间轮
	// 参数一:转动触发时间
	// 参数二:时间轮槽数
	// 参数三:回调函数
	tw := timewheel.New(time.Second, 3600, func(i interface{}) {
		// 回调事件
		fmt.Println(time.Now().Format("2006-01-02 03:04:05"))
	})

	// 启动时间轮
	tw.Start()
	fmt.Println(time.Now().Format("2006-01-02 03:04:05"))

	// 添加定时任务
	// 参数一:延迟时间
	// 参数二:标识,用于删除定时任务
	// 参数三:传给回调函数的参数
	tw.AddTask(5*time.Second, "key1", "hello")

	// 删除定时任务,参数为定时任务标识
	tw.RemoveTask("key1")

	// 停止时间轮
	tw.Stop()

	select {}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job func(interface{})

Job 任务回调函数

type Task

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

type TimeWheel

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

TimeWheel 时间轮

func New

func New(interval time.Duration, slotNum int, job Job) *TimeWheel

New 创建时间轮

func (*TimeWheel) AddTask

func (tw *TimeWheel) AddTask(delay time.Duration, key interface{}, data interface{})

AddTask 添加定时任务

func (*TimeWheel) RemoveTask

func (tw *TimeWheel) RemoveTask(key interface{})

RemoveTask 移除定时任务

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