Skip to content

Commit

Permalink
Finish v4.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyx committed Jan 23, 2022
2 parents 9b72755 + 9a239db commit 6893d9f
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 391 deletions.
12 changes: 7 additions & 5 deletions client.go
Expand Up @@ -19,6 +19,7 @@ package agollo

import (
"container/list"
"errors"
"strconv"

"github.com/apolloconfig/agollo/v4/agcache"
Expand Down Expand Up @@ -90,7 +91,6 @@ func (c *internalClient) getAppConfig() config.AppConfig {
}

func create() *internalClient {

appConfig := env.InitFileConfig()
return &internalClient{
appConfig: appConfig,
Expand Down Expand Up @@ -122,10 +122,12 @@ func StartWithConfig(loadAppConfig func() (*config.AppConfig, error)) (Client, e

//first sync
configs := syncApolloConfig.Sync(c.getAppConfig)
if len(configs) > 0 {
for _, apolloConfig := range configs {
c.cache.UpdateApolloConfig(apolloConfig, c.getAppConfig)
}
if len(configs) == 0 && appConfig != nil && appConfig.MustStart {
return nil, errors.New("start failed cause no config was read")
}

for _, apolloConfig := range configs {
c.cache.UpdateApolloConfig(apolloConfig, c.getAppConfig)
}

log.Debug("init notifySyncConfigServices finished")
Expand Down
18 changes: 10 additions & 8 deletions env/config/config.go
Expand Up @@ -41,14 +41,16 @@ type File interface {

//AppConfig 配置文件
type AppConfig struct {
AppID string `json:"appId"`
Cluster string `json:"cluster"`
NamespaceName string `json:"namespaceName"`
IP string `json:"ip"`
IsBackupConfig bool `default:"true" json:"isBackupConfig"`
BackupConfigPath string `json:"backupConfigPath"`
Secret string `json:"secret"`
SyncServerTimeout int `json:"syncServerTimeout"`
AppID string `json:"appId"`
Cluster string `json:"cluster"`
NamespaceName string `json:"namespaceName"`
IP string `json:"ip"`
IsBackupConfig bool `default:"true" json:"isBackupConfig"`
BackupConfigPath string `json:"backupConfigPath"`
Secret string `json:"secret"`
SyncServerTimeout int `json:"syncServerTimeout"`
// MustStart 可用于控制第一次同步必须成功
MustStart bool `default:"false"`
notificationsMap *notificationsMap
currentConnApolloConfig *CurrentApolloConfig
}
Expand Down
3 changes: 2 additions & 1 deletion env/file/json/json.go
Expand Up @@ -60,10 +60,11 @@ func (fileHandler *FileHandler) createDir(configPath string) error {
defer configFileDirMapLock.Unlock()
if !configFileDirMap[configPath] {
err := os.Mkdir(configPath, os.ModePerm)
if err != nil {
if err != nil && !os.IsExist(err) {
log.Errorf("Create backup dir:%s fail,error:&s", configPath, err)
return err
}
configFileDirMap[configPath] = true
}
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion env/file/json/json_test.go
Expand Up @@ -33,6 +33,8 @@ func TestCreateDir(t *testing.T) {
f := &FileHandler{}
err := f.createDir(configPath)
Assert(t, err, NilVal())
err = f.createDir(configPath)
Assert(t, err, NilVal())
err = os.Mkdir(configPath, os.ModePerm)
Assert(t, os.IsExist(err), Equal(true))

Expand All @@ -44,7 +46,7 @@ func TestCreateDir(t *testing.T) {

func TestJSONFileHandler_WriteConfigDirFile(t *testing.T) {
extension.SetFileHandler(&FileHandler{})
configPath := "conf"
configPath := "json-conf"
jsonStr := `{
"appId": "100004458",
"cluster": "default",
Expand Down
2 changes: 1 addition & 1 deletion env/file/json/raw_test.go
Expand Up @@ -27,7 +27,7 @@ import (

func TestRawHandler_WriteConfigDirFile(t *testing.T) {
extension.SetFileHandler(&rawFileHandler{})
configPath := "conf"
configPath := "raw-conf"
jsonStr := `{
"appId": "100004458",
"cluster": "default",
Expand Down
376 changes: 2 additions & 374 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion protocol/http/request.go
Expand Up @@ -171,7 +171,7 @@ func Request(requestURL string, connectionConfig *env.ConnectConfig, callBack *C
}
return nil, nil
default:
log.Errorf("Connect Apollo Server Fail,url:%s,StatusCode:%s", requestURL, res.StatusCode)
log.Errorf("Connect Apollo Server Fail,url:%s,StatusCode:%d", requestURL, res.StatusCode)
// if error then sleep
time.Sleep(onErrorRetryInterval)
continue
Expand Down
34 changes: 34 additions & 0 deletions start_test.go
Expand Up @@ -241,3 +241,37 @@ func TestSetSignature(t *testing.T) {

Assert(t, t2, Equal(extension.GetHTTPAuth()))
}

func TestErrorStartWithConfigMustReadFromRemote(t *testing.T) {
server := runErrorResponse()
newAppConfig := getTestAppConfig()
newAppConfig.IP = server.URL
newAppConfig.MustStart = true

client, err := StartWithConfig(func() (*config.AppConfig, error) {
return newAppConfig, nil
})

Assert(t, client, Equal(nil))
Assert(t, err, NotNilVal())
}

func TestStartWithConfigMustReadFromRemote(t *testing.T) {
c := appConfig
handlerMap := make(map[string]func(http.ResponseWriter, *http.Request), 1)
handlerMap["application"] = onlyNormalConfigResponse
server := runMockConfigFilesServer(handlerMap, nil, c)
c.IP = server.URL
c.MustStart = true

client, err := StartWithConfig(func() (*config.AppConfig, error) {
return c, nil
})
Assert(t, err, Equal(nil))

value := client.GetValue("key1")
Assert(t, "value1", Equal(value))
handler := extension.GetFileHandler()
Assert(t, handler, NotNilVal())

}

0 comments on commit 6893d9f

Please sign in to comment.