Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WithTimestampResolution option for RedisQueue #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions redis_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"strings"
"time"

"github.com/go-redis/redis/v8"
)
Expand All @@ -15,6 +16,8 @@ type redisQueue struct {
dequeueScript *redis.Script
ackScript *redis.Script
findScript *redis.Script

timestampResolution time.Duration
}

// RedisQueue implements Queue with other additional capabilities
Expand All @@ -26,8 +29,19 @@ type RedisQueue interface {
MetricsExporter
}

type RedisQueueOption func(*redisQueue)

// WithTimestampResolution allows to override resolution for timestamps
// used for setting job delay.
// Default resolution is second.
func WithTimestampResolution(resolution time.Duration) RedisQueueOption {
return func(q *redisQueue) {
q.timestampResolution = resolution
}
}

// NewRedisQueue creates a new queue stored in redis.
func NewRedisQueue(client redis.UniversalClient) RedisQueue {
func NewRedisQueue(client redis.UniversalClient, options ...RedisQueueOption) RedisQueue {
enqueueScript := redis.NewScript(`
local ns = ARGV[1]
local queue_id = ARGV[2]
Expand Down Expand Up @@ -130,13 +144,19 @@ func NewRedisQueue(client redis.UniversalClient) RedisQueue {
return ret
`)

return &redisQueue{
client: client,
enqueueScript: enqueueScript,
dequeueScript: dequeueScript,
ackScript: ackScript,
findScript: findScript,
q := &redisQueue{
client: client,
enqueueScript: enqueueScript,
dequeueScript: dequeueScript,
ackScript: ackScript,
findScript: findScript,
timestampResolution: time.Second,
}

for _, optin := range options {
optin(q)
}
return q
}

func (q *redisQueue) Enqueue(job *Job, opt *EnqueueOptions) error {
Expand All @@ -159,7 +179,7 @@ func (q *redisQueue) BulkEnqueue(jobs []*Job, opt *EnqueueOptions) error {
if err != nil {
return err
}
args[2+3*i] = job.EnqueuedAt.Unix()
args[2+3*i] = q.resolveTimestamp(job.EnqueuedAt)
args[2+3*i+1] = job.ID
args[2+3*i+2] = jobm
}
Expand All @@ -182,7 +202,7 @@ func (q *redisQueue) BulkDequeue(count int64, opt *DequeueOptions) ([]*Job, erro
res, err := q.dequeueScript.Run(context.Background(), q.client, nil,
opt.Namespace,
opt.QueueID,
opt.At.Unix(),
q.resolveTimestamp(opt.At),
opt.InvisibleSec,
count,
).Result()
Expand Down Expand Up @@ -281,3 +301,7 @@ func (q *redisQueue) GetQueueMetrics(opt *QueueMetricsOptions) (*QueueMetrics, e
ScheduledTotal: scheduledTotal,
}, nil
}

func (q *redisQueue) resolveTimestamp(t time.Time) int64 {
return t.UnixNano() / (int64(q.timestampResolution) / int64(time.Nanosecond))
}
44 changes: 44 additions & 0 deletions redis_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,47 @@ func TestRedisQueueGetQueueMetrics(t *testing.T) {
require.EqualValues(t, 0, m.ReadyTotal)
require.EqualValues(t, 1, m.ScheduledTotal)
}

func TestRedisWithTimestampResolution(t *testing.T) {
client := redistest.NewClient()
defer client.Close()
require.NoError(t, redistest.Reset(client))
q := NewRedisQueue(client, WithTimestampResolution(time.Millisecond))

type message struct {
Text string
}

job := NewJob()
err := job.MarshalPayload(message{Text: "hello"})
require.NoError(t, err)

job.EnqueuedAt = time.Now().
Add(300 * time.Millisecond).
Truncate(time.Millisecond)
err = q.Enqueue(job, &EnqueueOptions{
Namespace: "{ns1}",
QueueID: "q1",
})
require.NoError(t, err)

jobDequeued, err := q.Dequeue(&DequeueOptions{
Namespace: "{ns1}",
QueueID: "q1",
At: time.Now(),
InvisibleSec: 0,
})

require.ErrorIs(t, err, ErrEmptyQueue)
require.Nil(t, jobDequeued)

jobDequeued, err = q.Dequeue(&DequeueOptions{
Namespace: "{ns1}",
QueueID: "q1",
At: job.EnqueuedAt.Add(time.Millisecond),
InvisibleSec: 0,
})

require.NoError(t, err)
require.Equal(t, job, jobDequeued)
}