forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_notifications.go
84 lines (68 loc) · 1.79 KB
/
redis_notifications.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
package main
import (
"encoding/json"
"github.com/Sirupsen/logrus"
"github.com/garyburd/redigo/redis"
"time"
)
type InterfaceNotification struct {
Type string
Message string
OrgID string
Payload interface{}
Timestamp time.Time
}
type RedisNotificationHandler struct {
CacheStore *RedisClusterStorageManager
}
const (
UIChanName string = "dashboard.ui.messages"
)
func (u *RedisNotificationHandler) Start() {
go u.StartUIPubSubConn()
// go func() {
// for {
// n := InterfaceNotification{
// Type: "debug",
// Message: "Test Debug",
// OrgID: "53ac07777cbb8c2d53000002",
// Timestamp: time.Now(),
// }
// u.Notify(n)
// time.Sleep(time.Second * 5)
// }
// }()
}
func (u *RedisNotificationHandler) Notify(n InterfaceNotification) error {
json_err, encErr := json.Marshal(n)
if encErr != nil {
return encErr
}
if u.CacheStore != nil {
u.CacheStore.Publish(UIChanName, string(json_err))
}
return nil
}
func (u *RedisNotificationHandler) StartUIPubSubConn() {
u.CacheStore = &RedisClusterStorageManager{KeyPrefix: "gateway-notifications:", HashKeys: false}
u.CacheStore.Connect()
// On message, synchronize
for {
err := u.CacheStore.StartPubSubHandler(UIChanName, u.HandleIncommingRedisMessage)
if err != nil {
log.WithFields(logrus.Fields{
"prefix": "log-notifications",
"err": err,
}).Error("Connection to Redis failed, reconnect in 10s")
time.Sleep(10 * time.Second)
log.WithFields(logrus.Fields{
"prefix": "log-notifications",
}).Warning("Reconnecting")
u.CacheStore.Connect()
u.CacheStore.StartPubSubHandler(UIChanName, u.HandleIncommingRedisMessage)
}
}
}
func (u *RedisNotificationHandler) HandleIncommingRedisMessage(message redis.Message) {
log.Debug("Inbound message received")
}