Skip to content

Commit

Permalink
feat(sentinel): Add support for Redis Sentinel (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
nttu-ysc committed Dec 4, 2023
1 parent bbafc78 commit d4c7d21
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
16 changes: 16 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type options struct {
channelName string
channelSize int
cluster bool
sentinel bool
masterName string
}

// WithAddr setup the addr of redis
Expand All @@ -43,6 +45,20 @@ func WithCluster(enable bool) Option {
}
}

// WithSentinel redis sentinel
func WithSentinel(enable bool) Option {
return func(w *options) {
w.sentinel = enable
}
}

// WithMasterName sentinel master name
func WithMasterName(masterName string) Option {
return func(w *options) {
w.masterName = masterName
}
}

// WithChannelSize redis channel size
func WithChannelSize(size int) Option {
return func(w *options) {
Expand Down
7 changes: 7 additions & 0 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ func NewWorker(opts ...Option) *Worker {
Addrs: strings.Split(w.opts.addr, ","),
Password: w.opts.password,
})
} else if w.opts.sentinel {
w.rdb = redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: w.opts.masterName,
SentinelAddrs: strings.Split(w.opts.addr, ","),
Password: w.opts.password,
DB: w.opts.db,
})
} else {
options := &redis.Options{
Addr: w.opts.addr,
Expand Down
31 changes: 31 additions & 0 deletions redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,37 @@ func TestRedisCluster(t *testing.T) {
// you will see the execute time > 1000ms
}

func TestRedisSentinel(t *testing.T) {
t.Helper()
m := &mockMessage{
Message: "foo",
}
hosts := []string{host + ":26379", host + ":26380"}

w := NewWorker(
WithAddr(strings.Join(hosts, ",")),
WithMasterName("mymaster"),
WithChannel("testSentinel"),
WithSentinel(true),
WithRunFunc(func(ctx context.Context, m core.QueuedMessage) error {
time.Sleep(500 * time.Millisecond)
return nil
}),
)
q := queue.NewPool(
5,
queue.WithWorker(w),
)
time.Sleep(100 * time.Millisecond)
assert.NoError(t, q.Queue(m))
assert.NoError(t, q.Queue(m))
assert.NoError(t, q.Queue(m))
assert.NoError(t, q.Queue(m))
time.Sleep(1000 * time.Millisecond)
q.Release()
// you will see the execute time > 1000ms
}

func TestEnqueueJobAfterShutdown(t *testing.T) {
m := mockMessage{
Message: "foo",
Expand Down

0 comments on commit d4c7d21

Please sign in to comment.