🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

github.com/yaodongen/go-redis-delay-queue

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/yaodongen/go-redis-delay-queue

v1.0.1
Source
Go
Version published
Created
Source

golang redis delay queue

Usage

import delay "github.com/yaodongen/delay-queue"

Advantage

  • safe, fast and nice concurrency support
  • Benchmark with 8 thread in local can read 7079 ns/op, equals to 141262 reads / s. (go test -bench=. -run=none)

Sample

package main

import (
	"context"
	"github.com/go-redis/redis/v8"
	delay "github.com/yaodongen/delay-queue"
)

func main() {
	ctx := context.Background()
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
	})
	// producer
	err := delay.AddToQueue(ctx, rdb, "key", "123", 5, 86400)
	if err != nil {
		// your own logic
	}

	// consumer
	go func() {
		resCh, errCh := delay.GetFromQueue(ctx, rdb, "key")
		for res := range resCh {
			// your own logic
			_ = res
		}
		for err := range errCh {
			if err != context.Canceled && err != context.DeadlineExceeded {
				// your own logic
			}
		}
	}()

}

Redis Delay Queue Main Logic

Push

  • add the timePiece(sample: "1645614542") to sorted set
  • rpush the real data to timePiece

Pop

  • get a timePiece from sorted set which is before time.Now()
  • lpop the real data from timePiece

Redis 延迟队列原理

入队列

  • 把时间片(样例: "1645614542") 添加到 zset 中
  • 把需要存储的数据 rpush 到时间片中

出队列

  • 从 zset 中取出早于当前时间戳的一个时间片
  • 从时间片中 lpop 对应的数据

FAQs

Package last updated on 23 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts