forked from apolloconfig/agollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
componet_notify.go
139 lines (106 loc) · 2.82 KB
/
componet_notify.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
package agollo
import (
"encoding/json"
"sync"
"time"
"github.com/cihub/seelog"
)
const (
default_notification_id = -1
)
var (
allNotifications *notificationsMap
)
type NotifyConfigComponent struct {
}
type apolloNotify struct {
NotificationId int64 `json:"notificationId"`
NamespaceName string `json:"namespaceName"`
}
func (this *NotifyConfigComponent) Start() {
//long poll for sync
for {
notifySyncConfigServices()
time.Sleep(time.Second/2)
}
}
func toApolloConfig(resBody []byte) ([]*apolloNotify, error) {
remoteConfig := make([]*apolloNotify, 0)
err := json.Unmarshal(resBody, &remoteConfig)
if err != nil {
seelog.Error("Unmarshal Msg Fail,Error:", err)
return nil, err
}
return remoteConfig, nil
}
func getRemoteConfigSuccessCallBack(responseBody []byte) (o interface{}, err error) {
return toApolloConfig(responseBody)
}
func getRemoteConfig() ([]*apolloNotify, error) {
appConfig := GetAppConfig()
if appConfig == nil {
panic("can not find apollo config!please confirm!")
}
urlSuffix := getNotifyUrlSuffix(allNotifications.getNotifies(), appConfig)
//seelog.Debugf("allNotifications.getNotifies():%s",allNotifications.getNotifies())
notifies, err := requestRecovery(appConfig, urlSuffix, getRemoteConfigSuccessCallBack)
if notifies == nil {
return nil, err
}
return notifies.([]*apolloNotify), err
}
func notifySyncConfigServices() error {
remoteConfigs, err := getRemoteConfig()
if err != nil || len(remoteConfigs) == 0 {
return err
}
updateAllNotifications(remoteConfigs)
//sync all config
SyncConfig()
return nil
}
func updateAllNotifications(remoteConfigs []*apolloNotify) {
for _, remoteConfig := range remoteConfigs {
if remoteConfig.NamespaceName == "" {
continue
}
allNotifications.setNotify(remoteConfig.NamespaceName, remoteConfig.NotificationId)
}
}
func initNotify() {
allNotifications = ¬ificationsMap{
notifications: make(map[string]int64, 1),
}
appConfig := GetAppConfig()
allNotifications.setNotify(appConfig.NamespaceName, default_notification_id)
}
type notification struct {
NamespaceName string `json:"namespaceName"`
NotificationId int64 `json:"notificationId"`
}
type notificationsMap struct {
notifications map[string]int64
sync.RWMutex
}
func (this *notificationsMap) setNotify(namespaceName string, notificationId int64) {
this.Lock()
defer this.Unlock()
this.notifications[namespaceName] = notificationId
}
func (this *notificationsMap) getNotifies() string {
this.RLock()
defer this.RUnlock()
notificationArr := make([]*notification, 0)
for namespaceName, notificationId := range this.notifications {
notificationArr = append(notificationArr,
¬ification{
NamespaceName: namespaceName,
NotificationId: notificationId,
})
}
j, err := json.Marshal(notificationArr)
if err != nil {
return ""
}
return string(j)
}