Skip to content

Commit

Permalink
Merge pull request #91 from qiluge/master
Browse files Browse the repository at this point in the history
optimize usage of allNotifications
  • Loading branch information
zouyx authored Dec 21, 2019
2 parents 13cb20a + 64260aa commit dcbebfe
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 32 deletions.
1 change: 1 addition & 0 deletions app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func initConfig(loadAppConfig func() (*AppConfig, error)) {
return
}

initAllNotifications()
initApolloConfigCache(appConfig.NamespaceName)
}

Expand Down
22 changes: 10 additions & 12 deletions componet_notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package agollo

import (
"encoding/json"
"fmt"
"sync"
"time"
)
Expand Down Expand Up @@ -73,19 +74,13 @@ func (n *notificationsMap) getNotifies(namespace string) string {
}

func initAllNotifications() {
appConfig := GetAppConfig(nil)

if appConfig == nil {
allNotifications = &notificationsMap{
notifications: make(map[string]int64, 0),
}
return
}
initNamespaceNotifications(appConfig.NamespaceName)
}

func initNamespaceNotifications(namespace string) {
if namespace == empty {
return
}
namespaces := splitNamespaces(namespace,
namespaces := splitNamespaces(appConfig.NamespaceName,
func(namespace string) {})

allNotifications = &notificationsMap{
Expand All @@ -112,8 +107,11 @@ func notifySyncConfigServices() error {

remoteConfigs, err := notifyRemoteConfig(nil, empty)

if err != nil || len(remoteConfigs) == 0 {
return err
if err != nil {
return fmt.Errorf("notifySyncConfigServices: %s", err)
}
if len(remoteConfigs) == 0 {
return fmt.Errorf("notifySyncConfigServices: empty remote config")
}

updateAllNotifications(remoteConfigs)
Expand Down
16 changes: 4 additions & 12 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,6 @@ func initDefaultConfig() {
initConfigCache(cacheFactory)
}

//initNamespaceConfig 根据namespace创建缓存
func initNamespaceConfig(namespace string) {

createNamespaceConfig(cacheFactory, namespace)

initNamespaceNotifications(namespace)
}

func initConfigCache(cacheFactory *agcache.DefaultCacheFactory) {
if appConfig == nil {
logger.Warn("Config is nil,can not init agollo.")
Expand All @@ -74,7 +66,7 @@ func initConfigCache(cacheFactory *agcache.DefaultCacheFactory) {

func createNamespaceConfig(cacheFactory *agcache.DefaultCacheFactory, namespace string) {
splitNamespaces(namespace, func(namespace string) {
if _, ok := apolloConfigCache.Load(namespace);ok {
if _, ok := apolloConfigCache.Load(namespace); ok {
return
}
c := &Config{
Expand All @@ -83,7 +75,7 @@ func createNamespaceConfig(cacheFactory *agcache.DefaultCacheFactory, namespace
}
c.isInit.Store(false)
c.waitInit.Add(1)
apolloConfigCache.Store(namespace,c)
apolloConfigCache.Store(namespace, c)
})
}

Expand Down Expand Up @@ -198,12 +190,12 @@ func GetConfigAndInit(namespace string) *Config {
config, ok := apolloConfigCache.Load(namespace)

if !ok {
initNamespaceConfig(namespace)
createNamespaceConfig(cacheFactory, namespace)

notifySimpleSyncConfigServices(namespace)
}

if config, ok = apolloConfigCache.Load(namespace);!ok{
if config, ok = apolloConfigCache.Load(namespace); !ok {
return nil
}

Expand Down
12 changes: 4 additions & 8 deletions start.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,15 @@ func init() {
//init config
initFileConfig()

initCommon()
}

func initCommon() {
initDefaultConfig()

initAllNotifications()
}

//InitCustomConfig init config by custom
func InitCustomConfig(loadAppConfig func() (*AppConfig, error)) {

initConfig(loadAppConfig)

initCommon()
initDefaultConfig()
}

//start apollo
Expand Down Expand Up @@ -60,7 +54,9 @@ func startAgollo() error {
//init server ip list
go initServerIpList()
//first sync
go notifySyncConfigServices()
if err := notifySyncConfigServices(); err != nil {
return err
}
logger.Debug("init notifySyncConfigServices finished")

//start long poll sync config
Expand Down

0 comments on commit dcbebfe

Please sign in to comment.