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

Upgrade to go-redis/v8 #17

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
17 changes: 9 additions & 8 deletions consumer.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package redisqueue

import (
"context"
"net"
"os"
"sync"
"time"

"github.com/go-redis/redis/v7"
"github.com/go-redis/redis/v8"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -58,7 +59,7 @@ type ConsumerOptions struct {
RedisClient redis.UniversalClient
// RedisOptions allows you to configure the underlying Redis connection.
// More info here:
// https://pkg.go.dev/github.com/go-redis/redis/v7?tab=doc#Options.
// https://pkg.go.dev/github.com/go-redis/redis/v8?tab=doc#Options.
//
// This field is used if RedisClient field is nil.
RedisOptions *RedisOptions
Expand Down Expand Up @@ -190,7 +191,7 @@ func (c *Consumer) Run() {

for stream, consumer := range c.consumers {
c.streams = append(c.streams, stream)
err := c.redis.XGroupCreateMkStream(stream, c.options.GroupName, consumer.id).Err()
err := c.redis.XGroupCreateMkStream(context.TODO(), stream, c.options.GroupName, consumer.id).Err()
// ignoring the BUSYGROUP error makes this a noop
if err != nil && err.Error() != "BUSYGROUP Consumer Group name already exists" {
c.Errors <- errors.Wrap(err, "error creating consumer group")
Expand Down Expand Up @@ -256,7 +257,7 @@ func (c *Consumer) reclaim() {
end := "+"

for {
res, err := c.redis.XPendingExt(&redis.XPendingExtArgs{
res, err := c.redis.XPendingExt(context.TODO(), &redis.XPendingExtArgs{
Stream: stream,
Group: c.options.GroupName,
Start: start,
Expand All @@ -276,7 +277,7 @@ func (c *Consumer) reclaim() {

for _, r := range res {
if r.Idle >= c.options.VisibilityTimeout {
claimres, err := c.redis.XClaim(&redis.XClaimArgs{
claimres, err := c.redis.XClaim(context.TODO(), &redis.XClaimArgs{
Stream: stream,
Group: c.options.GroupName,
Consumer: c.options.Name,
Expand All @@ -297,7 +298,7 @@ func (c *Consumer) reclaim() {
// exists, the only way we can get it out of the
// pending state is to acknowledge it.
if err == redis.Nil {
err = c.redis.XAck(stream, c.options.GroupName, r.ID).Err()
err = c.redis.XAck(context.TODO(), stream, c.options.GroupName, r.ID).Err()
if err != nil {
c.Errors <- errors.Wrapf(err, "error acknowledging after failed claim for %q stream and %q message", stream, r.ID)
continue
Expand Down Expand Up @@ -335,7 +336,7 @@ func (c *Consumer) poll() {
}
return
default:
res, err := c.redis.XReadGroup(&redis.XReadGroupArgs{
res, err := c.redis.XReadGroup(context.TODO(), &redis.XReadGroupArgs{
Group: c.options.GroupName,
Consumer: c.options.Name,
Streams: c.streams,
Expand Down Expand Up @@ -389,7 +390,7 @@ func (c *Consumer) work() {
c.Errors <- errors.Wrapf(err, "error calling ConsumerFunc for %q stream and %q message", msg.Stream, msg.ID)
continue
}
err = c.redis.XAck(msg.Stream, c.options.GroupName, msg.ID).Err()
err = c.redis.XAck(context.TODO(), msg.Stream, c.options.GroupName, msg.ID).Err()
if err != nil {
c.Errors <- errors.Wrapf(err, "error acknowledging after success for %q stream and %q message", msg.Stream, msg.ID)
continue
Expand Down
39 changes: 20 additions & 19 deletions consumer_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package redisqueue

import (
"context"
"os"
"testing"
"time"

"github.com/go-redis/redis/v7"
"github.com/go-redis/redis/v8"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -156,8 +157,8 @@ func TestRun(t *testing.T) {
require.NoError(tt, err)

// create consumer group
c.redis.XGroupDestroy(tt.Name(), c.options.GroupName)
c.redis.XGroupCreateMkStream(tt.Name(), c.options.GroupName, "$")
c.redis.XGroupDestroy(context.TODO(), tt.Name(), c.options.GroupName)
c.redis.XGroupCreateMkStream(context.TODO(), tt.Name(), c.options.GroupName, "$")

// enqueue a message
err = p.Enqueue(&Message{
Expand Down Expand Up @@ -200,8 +201,8 @@ func TestRun(t *testing.T) {
require.NoError(tt, err)

// create consumer group
c.redis.XGroupDestroy(tt.Name(), c.options.GroupName)
c.redis.XGroupCreateMkStream(tt.Name(), c.options.GroupName, "$")
c.redis.XGroupDestroy(context.TODO(), tt.Name(), c.options.GroupName)
c.redis.XGroupCreateMkStream(context.TODO(), tt.Name(), c.options.GroupName, "$")

// enqueue a message
msg := &Message{
Expand All @@ -220,7 +221,7 @@ func TestRun(t *testing.T) {
})

// read the message but don't acknowledge it
res, err := c.redis.XReadGroup(&redis.XReadGroupArgs{
res, err := c.redis.XReadGroup(context.TODO(), &redis.XReadGroupArgs{
Group: c.options.GroupName,
Consumer: "failed_consumer",
Streams: []string{tt.Name(), ">"},
Expand Down Expand Up @@ -263,8 +264,8 @@ func TestRun(t *testing.T) {
require.NoError(tt, err)

// create consumer group
c.redis.XGroupDestroy(tt.Name(), c.options.GroupName)
c.redis.XGroupCreateMkStream(tt.Name(), c.options.GroupName, "$")
c.redis.XGroupDestroy(context.TODO(), tt.Name(), c.options.GroupName)
c.redis.XGroupCreateMkStream(context.TODO(), tt.Name(), c.options.GroupName, "$")

// enqueue a message
msg1 := &Message{
Expand All @@ -287,7 +288,7 @@ func TestRun(t *testing.T) {
})

// read the message but don't acknowledge it
res, err := c.redis.XReadGroup(&redis.XReadGroupArgs{
res, err := c.redis.XReadGroup(context.TODO(), &redis.XReadGroupArgs{
Group: c.options.GroupName,
Consumer: "failed_consumer",
Streams: []string{tt.Name(), ">"},
Expand All @@ -312,7 +313,7 @@ func TestRun(t *testing.T) {
c.Run()

// check if the pending message is still there
pendingRes, err := c.redis.XPendingExt(&redis.XPendingExtArgs{
pendingRes, err := c.redis.XPendingExt(context.TODO(), &redis.XPendingExtArgs{
Stream: tt.Name(),
Group: c.options.GroupName,
Start: "-",
Expand Down Expand Up @@ -343,8 +344,8 @@ func TestRun(t *testing.T) {
require.NoError(tt, err)

// create consumer group
c.redis.XGroupDestroy(tt.Name(), c.options.GroupName)
c.redis.XGroupCreateMkStream(tt.Name(), c.options.GroupName, "$")
c.redis.XGroupDestroy(context.TODO(), tt.Name(), c.options.GroupName)
c.redis.XGroupCreateMkStream(context.TODO(), tt.Name(), c.options.GroupName, "$")

// enqueue a message
msg := &Message{
Expand All @@ -361,7 +362,7 @@ func TestRun(t *testing.T) {
})

// read the message but don't acknowledge it
res, err := c.redis.XReadGroup(&redis.XReadGroupArgs{
res, err := c.redis.XReadGroup(context.TODO(), &redis.XReadGroupArgs{
Group: c.options.GroupName,
Consumer: "failed_consumer",
Streams: []string{tt.Name(), ">"},
Expand All @@ -373,7 +374,7 @@ func TestRun(t *testing.T) {
require.Equal(tt, msg.ID, res[0].Messages[0].ID)

// delete the message
err = c.redis.XDel(tt.Name(), msg.ID).Err()
err = c.redis.XDel(context.TODO(), tt.Name(), msg.ID).Err()
require.NoError(tt, err)

// watch for consumer errors
Expand All @@ -392,7 +393,7 @@ func TestRun(t *testing.T) {
c.Run()

// check that there are no pending messages
pendingRes, err := c.redis.XPendingExt(&redis.XPendingExtArgs{
pendingRes, err := c.redis.XPendingExt(context.TODO(), &redis.XPendingExtArgs{
Stream: tt.Name(),
Group: c.options.GroupName,
Start: "-",
Expand All @@ -418,8 +419,8 @@ func TestRun(t *testing.T) {
require.NoError(tt, err)

// create consumer group
c.redis.XGroupDestroy(tt.Name(), c.options.GroupName)
c.redis.XGroupCreateMkStream(tt.Name(), c.options.GroupName, "$")
c.redis.XGroupDestroy(context.TODO(), tt.Name(), c.options.GroupName)
c.redis.XGroupCreateMkStream(context.TODO(), tt.Name(), c.options.GroupName, "$")

// enqueue a message
err = p.Enqueue(&Message{
Expand Down Expand Up @@ -462,8 +463,8 @@ func TestRun(t *testing.T) {
require.NoError(tt, err)

// create consumer group
c.redis.XGroupDestroy(tt.Name(), c.options.GroupName)
c.redis.XGroupCreateMkStream(tt.Name(), c.options.GroupName, "$")
c.redis.XGroupDestroy(context.TODO(), tt.Name(), c.options.GroupName)
c.redis.XGroupCreateMkStream(context.TODO(), tt.Name(), c.options.GroupName, "$")

// enqueue a message
err = p.Enqueue(&Message{
Expand Down
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/git-chglog/git-chglog v0.0.0-20190611050339-63a4e637021f
github.com/go-redis/redis/v7 v7.3.0
github.com/golang/protobuf v1.3.3 // indirect
github.com/go-redis/redis/v8 v8.5.0
github.com/imdario/mergo v0.3.7 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/goveralls v0.0.2
github.com/pborman/uuid v1.2.0 // indirect
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.5.1
github.com/stretchr/testify v1.6.1
github.com/tsuyoshiwada/go-gitcmd v0.0.0-20180205145712-5f1f5f9475df // indirect
github.com/urfave/cli v1.20.0 // indirect
golang.org/x/tools v0.0.0-20190706070813-72ffa07ba3db // indirect
gopkg.in/AlecAivazis/survey.v1 v1.8.5 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/kyokomi/emoji.v1 v1.5.1 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)
Loading