-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredis.go
180 lines (153 loc) · 3.75 KB
/
redis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package redisdb
import (
"context"
"encoding/json"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/golang-queue/queue"
"github.com/golang-queue/queue/core"
"github.com/golang-queue/queue/job"
"github.com/redis/go-redis/v9"
"github.com/yassinebenaid/godump"
)
var _ core.Worker = (*Worker)(nil)
// Worker for Redis
type Worker struct {
// redis config
rdb redis.Cmdable
pubsub *redis.PubSub
channel <-chan *redis.Message
stopFlag int32
stopOnce sync.Once
stop chan struct{}
opts options
}
// NewWorker creates a new Worker instance with the provided options.
// It initializes a Redis client based on the options and establishes a connection to the Redis server.
// The Worker is responsible for subscribing to a Redis channel and receiving messages from it.
// It returns the created Worker instance.
func NewWorker(opts ...Option) *Worker {
var err error
w := &Worker{
opts: newOptions(opts...),
stop: make(chan struct{}),
}
if w.opts.debug {
_ = godump.Dump(w.opts)
}
options := &redis.Options{
Addr: w.opts.addr,
Username: w.opts.username,
Password: w.opts.password,
DB: w.opts.db,
TLSConfig: w.opts.tls,
}
w.rdb = redis.NewClient(options)
if w.opts.connectionString != "" {
options, err := redis.ParseURL(w.opts.connectionString)
if err != nil {
w.opts.logger.Fatal(err)
}
w.rdb = redis.NewClient(options)
}
if w.opts.cluster {
w.rdb = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: strings.Split(w.opts.addr, ","),
Username: w.opts.username,
Password: w.opts.password,
TLSConfig: w.opts.tls,
})
}
if w.opts.sentinel {
w.rdb = redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: w.opts.masterName,
SentinelAddrs: strings.Split(w.opts.addr, ","),
Username: w.opts.username,
Password: w.opts.password,
DB: w.opts.db,
TLSConfig: w.opts.tls,
})
}
_, err = w.rdb.Ping(context.Background()).Result()
if err != nil {
w.opts.logger.Fatal(err)
}
ctx := context.Background()
switch v := w.rdb.(type) {
case *redis.Client:
w.pubsub = v.Subscribe(ctx, w.opts.channelName)
case *redis.ClusterClient:
w.pubsub = v.Subscribe(ctx, w.opts.channelName)
}
var ropts []redis.ChannelOption
if w.opts.channelSize > 1 {
ropts = append(ropts, redis.WithChannelSize(w.opts.channelSize))
}
w.channel = w.pubsub.Channel(ropts...)
// make sure the connection is successful
if err := w.pubsub.Ping(ctx); err != nil {
w.opts.logger.Fatal(err)
}
return w
}
// Run to execute new task
func (w *Worker) Run(ctx context.Context, task core.TaskMessage) error {
return w.opts.runFunc(ctx, task)
}
// Shutdown worker
func (w *Worker) Shutdown() error {
if !atomic.CompareAndSwapInt32(&w.stopFlag, 0, 1) {
return queue.ErrQueueShutdown
}
w.stopOnce.Do(func() {
w.pubsub.Close()
switch v := w.rdb.(type) {
case *redis.Client:
v.Close()
case *redis.ClusterClient:
v.Close()
}
close(w.stop)
})
return nil
}
// Queue send notification to queue
func (w *Worker) Queue(job core.TaskMessage) error {
if atomic.LoadInt32(&w.stopFlag) == 1 {
return queue.ErrQueueShutdown
}
ctx := context.Background()
// Publish a message.
err := w.rdb.Publish(ctx, w.opts.channelName, job.Bytes()).Err()
if err != nil {
return err
}
return nil
}
// Request a new task
func (w *Worker) Request() (core.TaskMessage, error) {
clock := 0
loop:
for {
select {
case task, ok := <-w.channel:
if !ok {
return nil, queue.ErrQueueHasBeenClosed
}
var data job.Message
err := json.Unmarshal([]byte(task.Payload), &data)
if err != nil {
return nil, err
}
return &data, nil
case <-time.After(1 * time.Second):
if clock == 5 {
break loop
}
clock += 1
}
}
return nil, queue.ErrNoTaskInQueue
}