-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
145 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package delayqueue | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
// Publisher only publishes messages to delayqueue, it is a encapsulation of delayqueue | ||
type Publisher struct { | ||
inner *DelayQueue | ||
} | ||
|
||
// NewPublisher0 creates a new Publisher by a RedisCli instance | ||
func NewPublisher0(name string, cli RedisCli, opts ...interface{}) *Publisher { | ||
opts = append(opts, noCallbackOpt(1)) | ||
return &Publisher{ | ||
inner: NewQueue0(name, cli, nil, opts...), | ||
} | ||
} | ||
|
||
// NewPublisher creates a new Publisher by a *redis.Client | ||
func NewPublisher(name string, cli *redis.Client, opts ...interface{}) *Publisher { | ||
rc := &redisV9Wrapper{ | ||
inner: cli, | ||
} | ||
return NewPublisher0(name, rc, opts...) | ||
} | ||
|
||
// WithLogger customizes logger for queue | ||
func (p *Publisher) WithLogger(logger *log.Logger) *Publisher { | ||
p.inner.logger = logger | ||
return p | ||
} | ||
|
||
// SendScheduleMsg submits a message delivered at given time | ||
func (p *Publisher) SendScheduleMsg(payload string, t time.Time, opts ...interface{}) error { | ||
return p.inner.SendScheduleMsg(payload, t, opts...) | ||
} | ||
|
||
// SendDelayMsg submits a message delivered after given duration | ||
func (p *Publisher) SendDelayMsg(payload string, duration time.Duration, opts ...interface{}) error { | ||
return p.inner.SendDelayMsg(payload, duration, opts...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package delayqueue | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
func TestPublisher(t *testing.T) { | ||
redisCli := redis.NewClient(&redis.Options{ | ||
Addr: "127.0.0.1:6379", | ||
}) | ||
redisCli.FlushDB(context.Background()) | ||
size := 1000 | ||
retryCount := 3 | ||
deliveryCount := make(map[string]int) | ||
cb := func(s string) bool { | ||
deliveryCount[s]++ | ||
i, _ := strconv.ParseInt(s, 10, 64) | ||
return i%2 == 0 | ||
} | ||
logger := log.New(os.Stderr, "[DelayQueue]", log.LstdFlags) | ||
queue := NewQueue("test", redisCli, cb).WithLogger(logger) | ||
publisher := NewPublisher("test", redisCli).WithLogger(logger) | ||
|
||
for i := 0; i < size; i++ { | ||
err := publisher.SendDelayMsg(strconv.Itoa(i), 0, WithRetryCount(retryCount), WithMsgTTL(time.Hour)) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
for i := 0; i < 10*size; i++ { | ||
err := queue.consume() | ||
if err != nil { | ||
t.Errorf("consume error: %v", err) | ||
return | ||
} | ||
} | ||
for k, v := range deliveryCount { | ||
i, _ := strconv.ParseInt(k, 10, 64) | ||
if i%2 == 0 { | ||
if v != 1 { | ||
t.Errorf("expect 1 delivery, actual %d", v) | ||
} | ||
} else { | ||
if v != retryCount+1 { | ||
t.Errorf("expect %d delivery, actual %d", retryCount+1, v) | ||
} | ||
} | ||
} | ||
} |