godelayqueue

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2021 License: MIT Imports: 10 Imported by: 0

README

go-delayqueue

A delay queue implemented in go language

  • 时间轮算法实现的延迟队列,支持细粒度延迟任务执行,用户自定义任务实现,无上下文依赖,可扩展性高。
  • 延迟队列中的任务,支持持久化存储,持久对象可自行实现,默认提供redis持久实现;支持程序中断后的任务恢复,可靠性更高。

详细说明参考如下文章:

使用时间轮算法实现延迟队列 (一)

使用时间轮算法实现延迟队列 (二)

Documentation

Index

Constants

View Source
const (
	//每个task存储到redis当中的key前缀,便于与其他数据的区分度
	TASK_KEY_PREFIX = "delaytk_"
)
View Source
const (
	//轮的时间长度,目前设置为一个小时,也就是时间轮每循环一次需要1小时;默认时间轮上面的每走一步的最小粒度为1秒。
	WHEEL_SIZE = 3600
)

Variables

This section is empty.

Functions

func GetDelayQueue

func GetDelayQueue(serviceBuilder BuildExecutor) *delayQueue

单列方法,使用默认的redis持久方案

func GetDelayQueueWithPersis

func GetDelayQueueWithPersis(serviceBuilder BuildExecutor, persistence Persistence) *delayQueue

单列方法,使用外部传入的持久方案

func GetEvnWithDefaultVal

func GetEvnWithDefaultVal(key string, defaultVal string) string

Types

type BuildExecutor

type BuildExecutor func(taskType string) Executor

定义一个获取实现具体任务对象的工厂方法,该方法需要在业务项目中定义并实现

type Executor

type Executor interface {
	DoDelayTask(contents string) error
}

executor interface, the business instance need to implement it

type Persistence

type Persistence interface {
	Save(task *Task) error
	GetList() []*Task
	Delete(taskId string) error
}

type Task

type Task struct {
	//任务ID便于持久化
	Id string
	//任务在时间轮上的循环次数,等于0时,执行该任务
	CycleCount int
	//任务在时间轮上的位置
	WheelPosition int
	//任务类型,用于工厂方法判断该使用哪个实现对象
	TaskType string
	//任务方法参数
	TaskParams string

	Next *Task
}

任务采用链表结构

Jump to

Keyboard shortcuts

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