forked from apolloconfig/agollo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.go
More file actions
148 lines (126 loc) · 3.93 KB
/
sync.go
File metadata and controls
148 lines (126 loc) · 3.93 KB
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package serverlist
import (
"encoding/json"
"sync"
"time"
"github.com/apolloconfig/agollo/v4/env/server"
"github.com/apolloconfig/agollo/v4/component"
"github.com/apolloconfig/agollo/v4/component/log"
"github.com/apolloconfig/agollo/v4/env"
"github.com/apolloconfig/agollo/v4/env/config"
"github.com/apolloconfig/agollo/v4/protocol/http"
)
const (
//refresh ip list
refreshIPListInterval = 20 * time.Minute //20m
)
func init() {
}
// InitSyncServerIPList 初始化同步服务器信息列表
// Deprecated 该方式启动的SyncServerIPListComponent无法关闭,强烈建议使用NewSyncServerIPListComponent
func InitSyncServerIPList(appConfig func() config.AppConfig) {
go component.StartRefreshConfig(&SyncServerIPListComponent{appConfig: appConfig})
}
func NewSyncServerIPListComponent(appConfig func() config.AppConfig) *SyncServerIPListComponent {
return &SyncServerIPListComponent{
appConfig: appConfig,
stopCh: make(chan struct{}),
}
}
// SyncServerIPListComponent set timer for update ip list
// interval : 20m
type SyncServerIPListComponent struct {
appConfig func() config.AppConfig
stopCh chan struct{}
stopOnce sync.Once
}
// Start 启动同步服务器列表
func (s *SyncServerIPListComponent) Start() {
SyncServerIPList(s.appConfig)
log.Debug("syncServerIpListComponent started")
t2 := time.NewTimer(refreshIPListInterval)
defer t2.Stop()
for {
select {
case <-s.stopCh:
log.Debug("syncServerIpListComponent stopped")
return
case <-t2.C:
SyncServerIPList(s.appConfig)
t2.Reset(refreshIPListInterval)
}
}
}
func (s *SyncServerIPListComponent) Stop() {
s.stopOnce.Do(func() {
if s.stopCh != nil {
close(s.stopCh)
}
})
}
// SyncServerIPList sync ip list from server
// then
// 1.update agcache
// 2.store in disk
func SyncServerIPList(appConfigFunc func() config.AppConfig) (map[string]*config.ServerInfo, error) {
if appConfigFunc == nil {
panic("can not find apollo config!please confirm!")
}
appConfig := appConfigFunc()
c := &env.ConnectConfig{
AppID: appConfig.AppID,
Secret: appConfig.Secret,
}
if appConfig.SyncServerTimeout > 0 {
c.Timeout = time.Duration(appConfig.SyncServerTimeout) * time.Second
}
serverMap, err := http.Request(appConfig.GetServicesConfigURL(), c, &http.CallBack{
SuccessCallBack: SyncServerIPListSuccessCallBack,
AppConfigFunc: appConfigFunc,
})
if serverMap == nil {
return nil, err
}
m := serverMap.(map[string]*config.ServerInfo)
server.SetServers(appConfig.GetHost(), m)
return m, err
}
// SyncServerIPListSuccessCallBack 同步服务器列表成功后的回调
func SyncServerIPListSuccessCallBack(responseBody []byte, callback http.CallBack) (o interface{}, err error) {
log.Debug("get all server info:", string(responseBody))
tmpServerInfo := make([]*config.ServerInfo, 0)
err = json.Unmarshal(responseBody, &tmpServerInfo)
if err != nil {
log.Errorf("Unmarshal json Fail, error: %v", err)
return
}
if len(tmpServerInfo) == 0 {
log.Info("get no real server!")
return
}
m := make(map[string]*config.ServerInfo)
for _, server := range tmpServerInfo {
if server == nil {
continue
}
m[server.HomepageURL] = server
}
o = m
return
}