redqueue

package
v1.66.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package redqueue 提供基于 Redis 的可靠任务队列: 即时任务走 List,延迟任务走 ZSet(score=执行时间),多实例可并行消费。 与进程内 taskqueue 互补:进程内队列重启丢失,redqueue 持久化且支持多实例; 无需部署 MQ 的轻量场景下可作为可靠延迟队列使用。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Queue

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

Queue 是基于 Redis 的可靠任务队列。

func NewQueue

func NewQueue(client *redis.Client, keyPrefix string) *Queue

NewQueue 创建队列;client 为 go-redis 客户端(可用 cache.RedisCache.Client())。 keyPrefix 用于多队列隔离(如 "order-tasks")。

func (*Queue) Consume

func (q *Queue) Consume(ctx context.Context, handler func(ctx context.Context, payload []byte) error) error

Consume 阻塞消费任务:延迟任务到期后原子搬入即时队列,再阻塞取任务执行。 handler 返回错误时任务重新入队(最多无限重试;业务可自行判断后返回 nil 放弃)。 ctx 取消时优雅退出(处理中的任务完成后返回)。

func (*Queue) Pending

func (q *Queue) Pending(ctx context.Context) (int64, error)

Pending 返回待处理任务总数(即时队列 + 延迟队列)。

func (*Queue) Submit

func (q *Queue) Submit(ctx context.Context, payload []byte, delay time.Duration) error

Submit 提交任务:delay<=0 走即时队列,否则走延迟队列(执行时间 = now+delay)。

Jump to

Keyboard shortcuts

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