File tree Expand file tree Collapse file tree 3 files changed +54
-0
lines changed Expand file tree Collapse file tree 3 files changed +54
-0
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,8 @@ type options struct {
20
20
channelName string
21
21
channelSize int
22
22
cluster bool
23
+ sentinel bool
24
+ masterName string
23
25
}
24
26
25
27
// WithAddr setup the addr of redis
@@ -43,6 +45,20 @@ func WithCluster(enable bool) Option {
43
45
}
44
46
}
45
47
48
+ // WithSentinel redis sentinel
49
+ func WithSentinel (enable bool ) Option {
50
+ return func (w * options ) {
51
+ w .sentinel = enable
52
+ }
53
+ }
54
+
55
+ // WithMasterName sentinel master name
56
+ func WithMasterName (masterName string ) Option {
57
+ return func (w * options ) {
58
+ w .masterName = masterName
59
+ }
60
+ }
61
+
46
62
// WithChannelSize redis channel size
47
63
func WithChannelSize (size int ) Option {
48
64
return func (w * options ) {
Original file line number Diff line number Diff line change @@ -49,6 +49,13 @@ func NewWorker(opts ...Option) *Worker {
49
49
Addrs : strings .Split (w .opts .addr , "," ),
50
50
Password : w .opts .password ,
51
51
})
52
+ } else if w .opts .sentinel {
53
+ w .rdb = redis .NewFailoverClient (& redis.FailoverOptions {
54
+ MasterName : w .opts .masterName ,
55
+ SentinelAddrs : strings .Split (w .opts .addr , "," ),
56
+ Password : w .opts .password ,
57
+ DB : w .opts .db ,
58
+ })
52
59
} else {
53
60
options := & redis.Options {
54
61
Addr : w .opts .addr ,
Original file line number Diff line number Diff line change @@ -130,6 +130,37 @@ func TestRedisCluster(t *testing.T) {
130
130
// you will see the execute time > 1000ms
131
131
}
132
132
133
+ func TestRedisSentinel (t * testing.T ) {
134
+ t .Helper ()
135
+ m := & mockMessage {
136
+ Message : "foo" ,
137
+ }
138
+ hosts := []string {host + ":26379" , host + ":26380" }
139
+
140
+ w := NewWorker (
141
+ WithAddr (strings .Join (hosts , "," )),
142
+ WithMasterName ("mymaster" ),
143
+ WithChannel ("testSentinel" ),
144
+ WithSentinel (true ),
145
+ WithRunFunc (func (ctx context.Context , m core.QueuedMessage ) error {
146
+ time .Sleep (500 * time .Millisecond )
147
+ return nil
148
+ }),
149
+ )
150
+ q := queue .NewPool (
151
+ 5 ,
152
+ queue .WithWorker (w ),
153
+ )
154
+ time .Sleep (100 * time .Millisecond )
155
+ assert .NoError (t , q .Queue (m ))
156
+ assert .NoError (t , q .Queue (m ))
157
+ assert .NoError (t , q .Queue (m ))
158
+ assert .NoError (t , q .Queue (m ))
159
+ time .Sleep (1000 * time .Millisecond )
160
+ q .Release ()
161
+ // you will see the execute time > 1000ms
162
+ }
163
+
133
164
func TestEnqueueJobAfterShutdown (t * testing.T ) {
134
165
m := mockMessage {
135
166
Message : "foo" ,
You can’t perform that action at this time.
0 commit comments