Skip to content

Commit

Permalink
解决程序启动时无法连接配置中心,则后续即使配置中心启动或恢复正常也无法刷新配置的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
logo306142054 committed Jun 18, 2020
1 parent de9df20 commit d48c747
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions agollo.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ func New(configServerURL, appID string, opts ...Option) (Agollo, error) {
func (a *agollo) initNamespace(namespaces ...string) error { func (a *agollo) initNamespace(namespaces ...string) error {
var uninitializedNamespaces []string var uninitializedNamespaces []string
for _, namespace := range namespaces { for _, namespace := range namespaces {
_, found := a.initialized.LoadOrStore(namespace, true) //_, found := a.initialized.LoadOrStore(namespace, true)
_, found := a.initialized.Load(namespace)
if !found { if !found {
uninitializedNamespaces = append(uninitializedNamespaces, namespace) uninitializedNamespaces = append(uninitializedNamespaces, namespace)
} }
Expand All @@ -129,9 +130,9 @@ func (a *agollo) initNamespace(namespaces ...string) error {
for _, namespace := range uninitializedNamespaces { for _, namespace := range uninitializedNamespaces {
// (1)读取配置 (2)设置初始化notificationMap // (1)读取配置 (2)设置初始化notificationMap
status, _, err := a.reloadNamespace(namespace) status, _, err := a.reloadNamespace(namespace)
if err != nil { // if err != nil {
return err // return err
} // }


// 这里没法光凭靠error==nil来判断,即使http请求失败,如果开启 容错,会导致error丢失 // 这里没法光凭靠error==nil来判断,即使http请求失败,如果开启 容错,会导致error丢失
// 从而可能将一个不存在的namespace拿去调用getRemoteNotifications导致被hold // 从而可能将一个不存在的namespace拿去调用getRemoteNotifications导致被hold
Expand All @@ -146,6 +147,9 @@ func (a *agollo) initNamespace(namespaces ...string) error {
// 不能正常获取notificationID的设置为默认notificationID // 不能正常获取notificationID的设置为默认notificationID
a.notificationMap.Store(namespace, defaultNotificationID) a.notificationMap.Store(namespace, defaultNotificationID)
} }
if err == nil {
a.initialized.Store(namespace, true)
}
} }


if len(existsNamespaces) == 0 { if len(existsNamespaces) == 0 {
Expand Down Expand Up @@ -244,13 +248,16 @@ func (a *agollo) Get(key string, opts ...GetOption) string {
} }


func (a *agollo) GetNameSpace(namespace string) Configurations { func (a *agollo) GetNameSpace(namespace string) Configurations {
config, found := a.cache.LoadOrStore(namespace, Configurations{}) config, found := a.cache.Load(namespace)
//config, found := a.cache.LoadOrStore(namespace, Configurations{})
if !found && a.opts.AutoFetchOnCacheMiss { if !found && a.opts.AutoFetchOnCacheMiss {
err := a.initNamespace(namespace) err := a.initNamespace(namespace)
if err != nil { if err != nil {
a.log("Action", "InitNamespace", "Error", err) a.log("Action", "InitNamespace", "Error", err)
} }
return a.getNameSpace(namespace) return a.getNameSpace(namespace)
} else {
config, _ = a.cache.LoadOrStore(namespace, Configurations{})
} }


return config.(Configurations) return config.(Configurations)
Expand Down

0 comments on commit d48c747

Please sign in to comment.