Skip to content

Commit

Permalink
Merge pull request #279 from piglig/refactor/code-optimisation
Browse files Browse the repository at this point in the history
refactor: code optimisation
  • Loading branch information
mergify[bot] authored Jul 26, 2023
2 parents 6fc8ec3 + 99119a3 commit 0e5d4ca
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 39 deletions.
16 changes: 8 additions & 8 deletions env/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Info struct {
nextTryConnTime int64
}

//GetServersLen 获取服务器数组
// GetServersLen 获取服务器数组
func GetServers(configIp string) map[string]*config.ServerInfo {
serverLock.Lock()
defer serverLock.Unlock()
Expand All @@ -53,7 +53,7 @@ func GetServers(configIp string) map[string]*config.ServerInfo {
return ipMap[configIp].serverMap
}

//GetServersLen 获取服务器数组长度
// GetServersLen 获取服务器数组长度
func GetServersLen(configIp string) int {
serverLock.Lock()
defer serverLock.Unlock()
Expand All @@ -72,7 +72,7 @@ func SetServers(configIp string, serverMap map[string]*config.ServerInfo) {
}
}

//SetDownNode 设置失效节点
// SetDownNode 设置失效节点
func SetDownNode(configService string, serverHost string) {
serverLock.Lock()
defer serverLock.Unlock()
Expand All @@ -99,15 +99,15 @@ func SetDownNode(configService string, serverHost string) {

for k, server := range s.serverMap {
// if some node has down then select next node
if strings.Index(k, serverHost) > -1 {
if strings.Contains(k, serverHost) {
server.IsDown = true
}
}
}

//IsConnectDirectly is connect by ip directly
//false : yes
//true : no
// IsConnectDirectly is connect by ip directly
// false : yes
// true : no
func IsConnectDirectly(configIp string) bool {
serverLock.Lock()
defer serverLock.Unlock()
Expand All @@ -122,7 +122,7 @@ func IsConnectDirectly(configIp string) bool {
return false
}

//SetNextTryConnTime if this connect is fail will set this time
// SetNextTryConnTime if this connect is fail will set this time
func SetNextTryConnTime(configIp string, nextPeriod int64) {
serverLock.Lock()
defer serverLock.Unlock()
Expand Down
8 changes: 3 additions & 5 deletions extension/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@ import (

"github.com/apolloconfig/agollo/v4/env/config"

"github.com/apolloconfig/agollo/v4/env/file"
. "github.com/tevid/gohamcrest"
)

type TestFileHandler struct {
}

//WriteConfigFile 写入配置文件
// WriteConfigFile 写入配置文件
func (r *TestFileHandler) WriteConfigFile(config *config.ApolloConfig, configPath string) error {
return nil
}

//GetConfigFile 获得配置文件路径
// GetConfigFile 获得配置文件路径
func (r *TestFileHandler) GetConfigFile(configDir string, appID string, namespace string) string {
return ""
}
Expand All @@ -48,6 +47,5 @@ func TestSetFileHandler(t *testing.T) {

fileHandler := GetFileHandler()

b := fileHandler.(file.FileHandler)
Assert(t, b, NotNilVal())
Assert(t, fileHandler, NotNilVal())
}
9 changes: 3 additions & 6 deletions storage/event_dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Listener interface {
Event(event *Event)
}

//Dispatcher is the observer
// Dispatcher is the observer
type Dispatcher struct {
listeners map[string][]Listener
}
Expand Down Expand Up @@ -90,10 +90,7 @@ func (d *Dispatcher) RegisterListener(listenerObject Listener, keys ...string) e

func invalidKey(key string) bool {
_, err := regexp.Compile(key)
if err != nil {
return true
}
return false
return err != nil
}

// UnRegisterListener 用于为某些key注释Listener
Expand Down Expand Up @@ -123,7 +120,7 @@ func (d *Dispatcher) UnRegisterListener(listenerObj Listener, keys ...string) er
return nil
}

//OnChange 实现Apollo的ChangeEvent处理
// OnChange 实现Apollo的ChangeEvent处理
func (d *Dispatcher) OnChange(changeEvent *ChangeEvent) {
if changeEvent == nil {
return
Expand Down
38 changes: 18 additions & 20 deletions storage/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (c *Cache) GetConfig(namespace string) *Config {
return config.(*Config)
}

// CreateNamespaceConfig 根据namespace初始化agollo内润配置
// CreateNamespaceConfig 根据namespace初始化agollo内容配置
func CreateNamespaceConfig(namespace string) *Cache {
// config from apollo
var apolloConfigCache sync.Map
Expand Down Expand Up @@ -508,7 +508,7 @@ func (c *Cache) UpdateApolloConfigCache(configurations map[string]interface{}, e
c.waitInit.Done()
}(config)

if (configurations == nil || len(configurations) == 0) && config.cache.EntryCount() == 0 {
if (len(configurations) == 0) && config.cache.EntryCount() == 0 {
return nil
}

Expand All @@ -521,27 +521,25 @@ func (c *Cache) UpdateApolloConfigCache(configurations map[string]interface{}, e

changes := make(map[string]*ConfigChange)

if configurations != nil {
// update new
// keys
for key, value := range configurations {
// key state insert or update
// insert
if !mp[key] {
changes[key] = createAddConfigChange(value)
} else {
// update
oldValue, _ := config.cache.Get(key)
if !reflect.DeepEqual(oldValue, value) {
changes[key] = createModifyConfigChange(oldValue, value)
}
// update new
// keys
for key, value := range configurations {
// key state insert or update
// insert
if !mp[key] {
changes[key] = createAddConfigChange(value)
} else {
// update
oldValue, _ := config.cache.Get(key)
if !reflect.DeepEqual(oldValue, value) {
changes[key] = createModifyConfigChange(oldValue, value)
}
}

if err := config.cache.Set(key, value, expireTime); err != nil {
log.Errorf("set key %s to cache error %s", key, err)
}
delete(mp, key)
if err := config.cache.Set(key, value, expireTime); err != nil {
log.Errorf("set key %s to cache error %s", key, err)
}
delete(mp, key)
}

// remove del keys
Expand Down

0 comments on commit 0e5d4ca

Please sign in to comment.