Skip to content

Update deps #22

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

Open
wants to merge 3 commits 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
23 changes: 12 additions & 11 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 @@ -98,15 +99,15 @@ var defaultConsumerOptions = &ConsumerOptions{
// to the hostname, GroupName to "redisqueue", VisibilityTimeout to 60 seconds,
// BufferSize to 100, and Concurrency to 10. In most production environments,
// you'll want to use NewConsumerWithOptions.
func NewConsumer() (*Consumer, error) {
return NewConsumerWithOptions(defaultConsumerOptions)
func NewConsumer(ctx context.Context) (*Consumer, error) {
return NewConsumerWithOptions(ctx, defaultConsumerOptions)
}

// NewConsumerWithOptions creates a Consumer with custom ConsumerOptions. If
// Name is left empty, it defaults to the hostname; if GroupName is left empty,
// it defaults to "redisqueue"; if BlockingTimeout is 0, it defaults to 5
// seconds; if ReclaimInterval is 0, it defaults to 1 second.
func NewConsumerWithOptions(options *ConsumerOptions) (*Consumer, error) {
func NewConsumerWithOptions(ctx context.Context, options *ConsumerOptions) (*Consumer, error) {
hostname, _ := os.Hostname()

if options.Name == "" {
Expand All @@ -130,7 +131,7 @@ func NewConsumerWithOptions(options *ConsumerOptions) (*Consumer, error) {
r = newRedisClient(options.RedisOptions)
}

if err := redisPreflightChecks(r); err != nil {
if err := redisPreflightChecks(ctx, r); err != nil {
return nil, err
}

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.Background(), 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.Background(), &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.Background(), &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.Background(), 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.Background(), &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.Background(), 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
Loading