-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
87 lines (66 loc) · 1.79 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
package broker
import (
"context"
"github.com/redis/go-redis/v9"
"github.com/tiptophelmet/nomess-core/v5/errs"
"github.com/tiptophelmet/nomess-core/v5/logger"
)
func InitRedisBroker() *RedisBroker {
return &RedisBroker{subscriptions: make(map[string]*redis.PubSub)}
}
type RedisBroker struct {
client *redis.Client
subscriptions map[string]*redis.PubSub
}
func (r *RedisBroker) Connect(url string) error {
options, err := redis.ParseURL(url)
if err != nil {
logger.Error("failed to connect to redis (pubsub): %v", err.Error())
return errs.ErrPubSubBrokerConnectionFailed
}
r.client = redis.NewClient(options)
return nil
}
func (r *RedisBroker) Publish(topic string, message []byte) error {
ctx, ctxCancel := context.WithCancel(context.Background())
defer ctxCancel()
intCmd := r.client.Publish(ctx, topic, message)
return intCmd.Err()
}
func (r *RedisBroker) Subscribe(topic string, handler func(message []byte)) error {
ctx, ctxCancel := context.WithCancel(context.Background())
defer ctxCancel()
sub := r.client.Subscribe(ctx, topic)
_, err := sub.Receive(ctx)
if err != nil {
return err
}
r.subscriptions[topic] = sub
ch := sub.Channel()
for msg := range ch {
handler([]byte(msg.Payload))
}
return nil
}
func (r *RedisBroker) Unsubscribe(topic string) error {
ctx, ctxCancel := context.WithCancel(context.Background())
defer ctxCancel()
sub, found := r.subscriptions[topic]
if found {
delete(r.subscriptions, topic)
return sub.Unsubscribe(ctx)
}
return nil
}
func (r *RedisBroker) IsConnected() bool {
ctx, ctxCancel := context.WithCancel(context.Background())
defer ctxCancel()
statusCmd := r.client.Ping(ctx)
if err := statusCmd.Err(); err != nil {
return false
}
return true
}
func (r *RedisBroker) Close() error {
return r.client.Close()
}