forked from apolloconfig/agollo
/
componet_notify.go
183 lines (142 loc) · 3.84 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
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
181
182
183
package agollo
import (
"encoding/json"
"sync"
"time"
)
const (
default_notification_id = -1
)
var (
allNotifications *notificationsMap
)
type notification struct {
NamespaceName string `json:"namespaceName"`
NotificationId int64 `json:"notificationId"`
}
type notificationsMap struct {
notifications map[string]int64
sync.RWMutex
}
type apolloNotify struct {
NotificationId int64 `json:"notificationId"`
NamespaceName string `json:"namespaceName"`
}
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)
}
func init() {
initAllNotifications()
}
func initAllNotifications() {
appConfig := GetAppConfig(nil)
if appConfig != nil {
allNotifications = ¬ificationsMap{
notifications: make(map[string]int64, 1),
}
allNotifications.setNotify(appConfig.NamespaceName, default_notification_id)
}
}
type NotifyConfigComponent struct {
}
func (this *NotifyConfigComponent) Start() {
t2 := time.NewTimer(long_poll_interval)
//long poll for sync
for {
select {
case <-t2.C:
notifySyncConfigServices()
t2.Reset(long_poll_interval)
}
}
}
func notifySyncConfigServices() error {
remoteConfigs, err := notifyRemoteConfig(nil)
if err != nil || len(remoteConfigs) == 0 {
return err
}
updateAllNotifications(remoteConfigs)
//sync all config
autoSyncConfigServices(nil)
return nil
}
func toApolloConfig(resBody []byte) ([]*apolloNotify, error) {
remoteConfig := make([]*apolloNotify, 0)
err := json.Unmarshal(resBody, &remoteConfig)
if err != nil {
logger.Error("Unmarshal Msg Fail,Error:", err)
return nil, err
}
return remoteConfig, nil
}
func notifyRemoteConfig(newAppConfig *AppConfig) ([]*apolloNotify, error) {
appConfig := GetAppConfig(newAppConfig)
if appConfig == nil {
panic("can not find apollo config!please confirm!")
}
urlSuffix := getNotifyUrlSuffix(allNotifications.getNotifies(), appConfig, newAppConfig)
//seelog.Debugf("allNotifications.getNotifies():%s",allNotifications.getNotifies())
notifies, err := requestRecovery(appConfig, &ConnectConfig{
Uri: urlSuffix,
Timeout: nofity_connect_timeout,
}, &CallBack{
SuccessCallBack: func(responseBody []byte) (interface{}, error) {
return toApolloConfig(responseBody)
},
NotModifyCallBack: touchApolloConfigCache,
})
if notifies == nil {
return nil, err
}
return notifies.([]*apolloNotify), err
}
func updateAllNotifications(remoteConfigs []*apolloNotify) {
for _, remoteConfig := range remoteConfigs {
if remoteConfig.NamespaceName == "" {
continue
}
allNotifications.setNotify(remoteConfig.NamespaceName, remoteConfig.NotificationId)
}
}
func autoSyncConfigServicesSuccessCallBack(responseBody []byte) (o interface{}, err error) {
apolloConfig, err := createApolloConfigWithJson(responseBody)
if err != nil {
logger.Error("Unmarshal Msg Fail,Error:", err)
return nil, err
}
updateApolloConfig(apolloConfig, true)
return nil, nil
}
func autoSyncConfigServices(newAppConfig *AppConfig) error {
appConfig := GetAppConfig(newAppConfig)
if appConfig == nil {
panic("can not find apollo config!please confirm!")
}
urlSuffix := getConfigUrlSuffix(appConfig, newAppConfig)
_, err := requestRecovery(appConfig, &ConnectConfig{
Uri: urlSuffix,
}, &CallBack{
SuccessCallBack: autoSyncConfigServicesSuccessCallBack,
NotModifyCallBack: touchApolloConfigCache,
})
return err
}