Skip to content

Commit

Permalink
fix: 如果获取不到config,尝试远端同步获取一次,获取到的内容需要更新缓存 (#301)
Browse files Browse the repository at this point in the history
* fix: 如果获取不到config,尝试远端同步获取一次,获取到的内容需要更新缓存

* fix: 优化 getConfigAndInit 逻辑,UpdateApolloConfig 的流程中已经包含了 cache store 过程

* fix: UT supplement

* fix: UT fix

---------

Co-authored-by: dongsheng.qi <[email protected]>
  • Loading branch information
qdsordinarydream and dongsheng.qi committed Mar 7, 2024
1 parent 9a6597d commit 9a87bea
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 18 deletions.
34 changes: 17 additions & 17 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func init() {

var syncApolloConfig = remote.CreateSyncApolloConfig()

//Client apollo 客户端接口
// Client apollo 客户端接口
type Client interface {
GetConfig(namespace string) *storage.Config
GetConfigAndInit(namespace string) *storage.Config
Expand Down Expand Up @@ -147,12 +147,12 @@ func StartWithConfig(loadAppConfig func() (*config.AppConfig, error)) (Client, e
return c, nil
}

//GetConfig 根据namespace获取apollo配置
// GetConfig 根据namespace获取apollo配置
func (c *internalClient) GetConfig(namespace string) *storage.Config {
return c.GetConfigAndInit(namespace)
}

//GetConfigAndInit 根据namespace获取apollo配置
// GetConfigAndInit 根据namespace获取apollo配置
func (c *internalClient) GetConfigAndInit(namespace string) *storage.Config {
if namespace == "" {
return nil
Expand All @@ -161,19 +161,19 @@ func (c *internalClient) GetConfigAndInit(namespace string) *storage.Config {
config := c.cache.GetConfig(namespace)

if config == nil {
//init cache
storage.CreateNamespaceConfig(namespace)

//sync config
syncApolloConfig.SyncWithNamespace(namespace, c.getAppConfig)
apolloConfig := syncApolloConfig.SyncWithNamespace(namespace, c.getAppConfig)
if apolloConfig != nil {
c.cache.UpdateApolloConfig(apolloConfig, c.getAppConfig)
}
}

config = c.cache.GetConfig(namespace)

return config
}

//GetConfigCache 根据namespace获取apollo配置的缓存
// GetConfigCache 根据namespace获取apollo配置的缓存
func (c *internalClient) GetConfigCache(namespace string) agcache.CacheInterface {
config := c.GetConfigAndInit(namespace)
if config == nil {
Expand All @@ -183,7 +183,7 @@ func (c *internalClient) GetConfigCache(namespace string) agcache.CacheInterface
return config.GetCache()
}

//GetDefaultConfigCache 获取默认缓存
// GetDefaultConfigCache 获取默认缓存
func (c *internalClient) GetDefaultConfigCache() agcache.CacheInterface {
config := c.GetConfigAndInit(storage.GetDefaultNamespace())
if config != nil {
Expand All @@ -192,42 +192,42 @@ func (c *internalClient) GetDefaultConfigCache() agcache.CacheInterface {
return nil
}

//GetApolloConfigCache 获取默认namespace的apollo配置
// GetApolloConfigCache 获取默认namespace的apollo配置
func (c *internalClient) GetApolloConfigCache() agcache.CacheInterface {
return c.GetDefaultConfigCache()
}

//GetValue 获取配置
// GetValue 获取配置
func (c *internalClient) GetValue(key string) string {
return c.GetConfig(storage.GetDefaultNamespace()).GetValue(key)
}

//GetStringValue 获取string配置值
// GetStringValue 获取string配置值
func (c *internalClient) GetStringValue(key string, defaultValue string) string {
return c.GetConfig(storage.GetDefaultNamespace()).GetStringValue(key, defaultValue)
}

//GetIntValue 获取int配置值
// GetIntValue 获取int配置值
func (c *internalClient) GetIntValue(key string, defaultValue int) int {
return c.GetConfig(storage.GetDefaultNamespace()).GetIntValue(key, defaultValue)
}

//GetFloatValue 获取float配置值
// GetFloatValue 获取float配置值
func (c *internalClient) GetFloatValue(key string, defaultValue float64) float64 {
return c.GetConfig(storage.GetDefaultNamespace()).GetFloatValue(key, defaultValue)
}

//GetBoolValue 获取bool 配置值
// GetBoolValue 获取bool 配置值
func (c *internalClient) GetBoolValue(key string, defaultValue bool) bool {
return c.GetConfig(storage.GetDefaultNamespace()).GetBoolValue(key, defaultValue)
}

//GetStringSliceValue 获取[]string 配置值
// GetStringSliceValue 获取[]string 配置值
func (c *internalClient) GetStringSliceValue(key string, defaultValue []string) []string {
return c.GetConfig(storage.GetDefaultNamespace()).GetStringSliceValue(key, separator, defaultValue)
}

//GetIntSliceValue 获取[]int 配置值
// GetIntSliceValue 获取[]int 配置值
func (c *internalClient) GetIntSliceValue(key string, defaultValue []int) []int {
return c.GetConfig(storage.GetDefaultNamespace()).GetIntSliceValue(key, separator, defaultValue)
}
Expand Down
40 changes: 39 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"

"github.com/agiledragon/gomonkey/v2"

"github.com/apolloconfig/agollo/v4/agcache/memory"
"github.com/apolloconfig/agollo/v4/component/remote"
"github.com/apolloconfig/agollo/v4/env/config"
"github.com/apolloconfig/agollo/v4/env/server"

Expand All @@ -37,7 +41,7 @@ import (

const testDefaultNamespace = "application"

//init param
// init param
func init() {
extension.SetCacheFactory(&memory.DefaultCacheFactory{})
}
Expand Down Expand Up @@ -352,3 +356,37 @@ func TestUseEventDispatch(t *testing.T) {
l := cache.GetChangeListeners()
Assert(t, l.Len(), Equal(1))
}

func TestGetConfigAndInitValNotNil(t *testing.T) {
var apc *remote.AbsApolloConfig
patch := gomonkey.ApplyMethod(reflect.TypeOf(apc), "SyncWithNamespace", func(_ *remote.AbsApolloConfig, namespace string, appConfigFunc func() config.AppConfig) *config.ApolloConfig {
return &config.ApolloConfig{
ApolloConnConfig: config.ApolloConnConfig{
AppID: "testID",
NamespaceName: "testNotFound",
},
Configurations: map[string]interface{}{"testKey": "testValue"},
}
})
defer patch.Reset()

client := createMockApolloConfig(120)
cf := client.GetConfig("testNotFound")
Assert(t, cf, NotNilVal())
// cache should be updated
Assert(t, client.cache.GetConfig("testNotFound"), NotNilVal())
Assert(t, client.cache.GetConfig("testNotFound").GetValue("testKey"), Equal("testValue"))
}

func TestGetConfigAndInitValNil(t *testing.T) {
var apc *remote.AbsApolloConfig
patch := gomonkey.ApplyMethod(reflect.TypeOf(apc), "SyncWithNamespace", func(_ *remote.AbsApolloConfig, namespace string, appConfigFunc func() config.AppConfig) *config.ApolloConfig {
return nil
})
defer patch.Reset()

client := createMockApolloConfig(120)
cf := client.GetConfig("testNotFound")
Assert(t, cf, NilVal())
Assert(t, client.cache.GetConfig("testNotFound"), NilVal())
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/apolloconfig/agollo/v4

require (
github.com/agiledragon/gomonkey/v2 v2.11.0 // indirect
github.com/spf13/viper v1.8.1
github.com/tevid/gohamcrest v1.1.1
)
Expand Down

0 comments on commit 9a87bea

Please sign in to comment.