Skip to content

Commit

Permalink
Finish v4.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyx committed Mar 26, 2022
2 parents 6893d9f + 60ed861 commit f89da36
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 129 deletions.
68 changes: 7 additions & 61 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ package agollo
import (
"container/list"
"errors"
"strconv"

"github.com/apolloconfig/agollo/v4/agcache"
"github.com/apolloconfig/agollo/v4/agcache/memory"
"github.com/apolloconfig/agollo/v4/cluster/roundrobin"
Expand Down Expand Up @@ -195,89 +193,37 @@ func (c *internalClient) GetApolloConfigCache() agcache.CacheInterface {

//GetValue 获取配置
func (c *internalClient) GetValue(key string) string {
value := c.getConfigValue(key)
if value == nil {
return utils.Empty
}

return value.(string)
return c.GetConfig(storage.GetDefaultNamespace()).GetValue(key)
}

//GetStringValue 获取string配置值
func (c *internalClient) GetStringValue(key string, defaultValue string) string {
value := c.GetValue(key)
if value == utils.Empty {
return defaultValue
}

return value
return c.GetConfig(storage.GetDefaultNamespace()).GetStringValue(key, defaultValue)
}

//GetIntValue 获取int配置值
func (c *internalClient) GetIntValue(key string, defaultValue int) int {
value := c.GetValue(key)

i, err := strconv.Atoi(value)
if err != nil {
log.Debug("convert to int fail!error:", err)
return defaultValue
}

return i
return c.GetConfig(storage.GetDefaultNamespace()).GetIntValue(key, defaultValue)
}

//GetFloatValue 获取float配置值
func (c *internalClient) GetFloatValue(key string, defaultValue float64) float64 {
value := c.GetValue(key)

i, err := strconv.ParseFloat(value, 64)
if err != nil {
log.Debug("convert to float fail!error:", err)
return defaultValue
}

return i
return c.GetConfig(storage.GetDefaultNamespace()).GetFloatValue(key, defaultValue)
}

//GetBoolValue 获取bool 配置值
func (c *internalClient) GetBoolValue(key string, defaultValue bool) bool {
value := c.GetValue(key)

b, err := strconv.ParseBool(value)
if err != nil {
log.Debug("convert to bool fail!error:", err)
return defaultValue
}

return b
return c.GetConfig(storage.GetDefaultNamespace()).GetBoolValue(key, defaultValue)
}

//GetStringSliceValue 获取[]string 配置值
func (c *internalClient) GetStringSliceValue(key string, defaultValue []string) []string {
value := c.getConfigValue(key)

if value == nil {
return defaultValue
}
s, ok := value.([]string)
if !ok {
return defaultValue
}
return s
return c.GetConfig(storage.GetDefaultNamespace()).GetStringSliceValue(key, defaultValue)
}

//GetIntSliceValue 获取[]int 配置值
func (c *internalClient) GetIntSliceValue(key string, defaultValue []int) []int {
value := c.getConfigValue(key)

if value == nil {
return defaultValue
}
s, ok := value.([]int)
if !ok {
return defaultValue
}
return s
return c.GetConfig(storage.GetDefaultNamespace()).GetIntSliceValue(key, defaultValue)
}

func (c *internalClient) getConfigValue(key string) interface{} {
Expand Down
10 changes: 5 additions & 5 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ func createMockApolloConfig(expireTime int) *internalClient {
//string
configs["string"] = "value"
//int
configs["int"] = "1"
configs["int"] = 1
//float
configs["float"] = "190.3"
configs["float"] = 190.3
//bool
configs["bool"] = "true"
configs["bool"] = true
//string slice
configs["stringSlice"] = []string{"1", "2"}

Expand Down Expand Up @@ -159,7 +159,7 @@ func TestGetFloatValue(t *testing.T) {
//error type
v = client.GetFloatValue("int", defaultValue)

Assert(t, float64(1), Equal(v))
Assert(t, defaultValue, Equal(v))
}

func TestGetBoolValue(t *testing.T) {
Expand Down Expand Up @@ -315,7 +315,7 @@ func TestConfig_GetFloatValue(t *testing.T) {
//error type
v = config.GetFloatValue("int", defaultValue)

Assert(t, float64(1), Equal(v))
Assert(t, defaultValue, Equal(v))
}

func TestConfig_GetIntValue(t *testing.T) {
Expand Down
16 changes: 8 additions & 8 deletions component/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,42 +50,42 @@ type LoggerInterface interface {

//Debugf debug 格式化
func Debugf(format string, params ...interface{}) {
Logger.Debugf(format, params)
Logger.Debugf(format, params...)
}

//Infof 打印info
func Infof(format string, params ...interface{}) {
Logger.Infof(format, params)
Logger.Infof(format, params...)
}

//Warnf warn格式化
func Warnf(format string, params ...interface{}) {
Logger.Warnf(format, params)
Logger.Warnf(format, params...)
}

//Errorf error格式化
func Errorf(format string, params ...interface{}) {
Logger.Errorf(format, params)
Logger.Errorf(format, params...)
}

//Debug 打印debug
func Debug(v ...interface{}) {
Logger.Debug(v)
Logger.Debug(v...)
}

//Info 打印Info
func Info(v ...interface{}) {
Logger.Info(v)
Logger.Info(v...)
}

//Warn 打印Warn
func Warn(v ...interface{}) {
Logger.Warn(v)
Logger.Warn(v...)
}

//Error 打印Error
func Error(v ...interface{}) {
Logger.Error(v)
Logger.Error(v...)
}

//DefaultLogger 默认日志实现
Expand Down
37 changes: 19 additions & 18 deletions protocol/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,21 @@ var (
)

func getDefaultTransport(insecureSkipVerify bool) *http.Transport {
if defaultTransport == nil {
once.Do(func() {
defaultTransport = &http.Transport{
MaxIdleConns: defaultMaxConnsPerHost,
MaxIdleConnsPerHost: defaultMaxConnsPerHost,
DialContext: (&net.Dialer{
KeepAlive: defaultKeepAliveSecond,
Timeout: defaultTimeoutBySecond,
}).DialContext,
}
if insecureSkipVerify {
defaultTransport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: insecureSkipVerify,
}
once.Do(func() {
defaultTransport = &http.Transport{
MaxIdleConns: defaultMaxConnsPerHost,
MaxIdleConnsPerHost: defaultMaxConnsPerHost,
DialContext: (&net.Dialer{
KeepAlive: defaultKeepAliveSecond,
Timeout: defaultTimeoutBySecond,
}).DialContext,
}
if insecureSkipVerify {
defaultTransport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: insecureSkipVerify,
}
})
}

}
})
return defaultTransport
}

Expand Down Expand Up @@ -135,6 +132,10 @@ func Request(requestURL string, connectionConfig *env.ConnectConfig, callBack *C
if len(headers) > 0 {
req.Header = headers
}
host := req.Header.Get("Host")
if len(host) > 0 {
req.Host = host
}
}

res, err := client.Do(req)
Expand All @@ -154,7 +155,7 @@ func Request(requestURL string, connectionConfig *env.ConnectConfig, callBack *C
case http.StatusOK:
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Errorf("Connect Apollo Server Fail,url:%s,Error:", requestURL, err)
log.Errorf("Connect Apollo Server Fail,url : %s ,Error: %s ", requestURL, err)
// if error then sleep
time.Sleep(onErrorRetryInterval)
continue
Expand Down
78 changes: 51 additions & 27 deletions storage/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"container/list"
"fmt"
"reflect"
"strconv"
"sync"
"sync/atomic"

Expand Down Expand Up @@ -140,7 +139,12 @@ func (c *Config) GetValue(key string) string {
return utils.Empty
}

return value.(string)
v, ok := value.(string)
if !ok {
log.Debug("convert to string fail ! source type:%T", value)
return utils.Empty
}
return v
}

//GetStringValue 获取配置值(string),获取不到则取默认值
Expand All @@ -154,68 +158,88 @@ func (c *Config) GetStringValue(key string, defaultValue string) string {
}

//GetStringSliceValue 获取配置值([]string)
func (c *Config) GetStringSliceValue(key string) []string {
func (c *Config) GetStringSliceValue(key string, defaultValue []string) []string {
value := c.getConfigValue(key)
if value == nil {
return []string{}
return defaultValue
}
return value.([]string)
v, ok := value.([]string)
if !ok {
log.Debug("convert to []string fail ! source type:%T", value)
return defaultValue
}
return v
}

//GetIntSliceValue 获取配置值([]int)
func (c *Config) GetIntSliceValue(key string) []int {
func (c *Config) GetIntSliceValue(key string, defaultValue []int) []int {
value := c.getConfigValue(key)
if value == nil {
return []int{}
return defaultValue
}
return value.([]int)
v, ok := value.([]int)
if !ok {
log.Debug("convert to []int fail ! source type:%T", value)
return defaultValue
}
return v
}

//GetSliceValue 获取配置值([]interface)
func (c *Config) GetSliceValue(key string) []interface{} {
func (c *Config) GetSliceValue(key string, defaultValue []interface{}) []interface{} {
value := c.getConfigValue(key)
if value == nil {
return []interface{}{}
return defaultValue
}
v, ok := value.([]interface{})
if !ok {
log.Debug("convert to []interface{} fail ! source type:%T", value)
return defaultValue
}
return value.([]interface{})
return v
}

//GetIntValue 获取配置值(int),获取不到则取默认值
func (c *Config) GetIntValue(key string, defaultValue int) int {
value := c.GetValue(key)
value := c.getConfigValue(key)

i, err := strconv.Atoi(value)
if err != nil {
log.Debug("convert to int fail!error:", err)
if value == nil {
return defaultValue
}
v, ok := value.(int)
if !ok {
log.Debug("convert to int fail ! source type:%T", value)
return defaultValue
}
return i
return v
}

//GetFloatValue 获取配置值(float),获取不到则取默认值
func (c *Config) GetFloatValue(key string, defaultValue float64) float64 {
value := c.GetValue(key)
value := c.getConfigValue(key)

i, err := strconv.ParseFloat(value, 64)
if err != nil {
log.Debug("convert to float fail!error:", err)
if value == nil {
return defaultValue
}

return i
v, ok := value.(float64)
if !ok {
log.Debug("convert to float64 fail ! source type:%T", value)
return defaultValue
}
return v
}

//GetBoolValue 获取配置值(bool),获取不到则取默认值
func (c *Config) GetBoolValue(key string, defaultValue bool) bool {
value := c.GetValue(key)
value := c.getConfigValue(key)

b, err := strconv.ParseBool(value)
if err != nil {
log.Debug("convert to bool fail!error:", err)
v, ok := value.(bool)
if !ok {
log.Debug("convert to bool fail ! source type:%T", value)
return defaultValue
}

return b
return v
}

//UpdateApolloConfig 根据config server返回的内容更新内存
Expand Down
Loading

0 comments on commit f89da36

Please sign in to comment.