Skip to content

Commit

Permalink
Finish FixBug
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyx committed Nov 15, 2018
2 parents d793cad + 63b8cbb commit 12b1f28
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 370 deletions.
82 changes: 25 additions & 57 deletions app_config.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package agollo

import (
"os"
"strconv"
"time"
"encoding/json"
"fmt"
"net/url"
"encoding/json"
"strings"
"time"
)

const appConfigFileName ="app.properties"

var (
refresh_interval = 5 *time.Minute //5m
refresh_interval_key = "apollo.refreshInterval" //

long_poll_interval = 2 *time.Second //2s
long_poll_connect_timeout = 1 * time.Minute //1m

Expand Down Expand Up @@ -61,6 +56,9 @@ func (this *AppConfig) getBackupConfigPath() string{

func (this *AppConfig) getHost() string{
if strings.HasPrefix(this.Ip,"http"){
if(!strings.HasSuffix(this.Ip,"/")){
return this.Ip+"/"
}
return this.Ip
}
return "http://"+this.Ip+"/"
Expand Down Expand Up @@ -125,32 +123,23 @@ type serverInfo struct {
}

func init() {
//init common
initCommon()

//init config
initFileConfig()

//init common
initCommon()
}

func initCommon() {

initRefreshInterval()
//init server ip list
go initServerIpList()
}

func initFileConfig() {
// default use application.properties
initConfig(nil)
}

func InitCustomConfig(loadAppConfig func()(*AppConfig,error)) {

initConfig(loadAppConfig)

//init all notification
initAllNotifications()

}

func initConfig(loadAppConfig func()(*AppConfig,error)) {
var err error
//init config file
Expand All @@ -170,6 +159,16 @@ func initConfig(loadAppConfig func()(*AppConfig,error)) {
}(appConfig)
}

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

initConfig(loadAppConfig)

//init all notification
initAllNotifications()

}

// set load app config's function
func getLoadAppConfig(loadAppConfig func()(*AppConfig,error)) (*AppConfig,error) {
if loadAppConfig!=nil{
Expand All @@ -181,6 +180,8 @@ func getLoadAppConfig(loadAppConfig func()(*AppConfig,error)) (*AppConfig,error)
//set timer for update ip list
//interval : 20m
func initServerIpList() {
syncServerIpList(nil)

t2 := time.NewTimer(refresh_ip_list_interval)
for {
select {
Expand All @@ -192,6 +193,8 @@ func initServerIpList() {
}

func syncServerIpListSuccessCallBack(responseBody []byte)(o interface{},err error){
logger.Debug("get all server info:",string(responseBody))

tmpServerInfo:=make([]*serverInfo,0)

err= json.Unmarshal(responseBody,&tmpServerInfo)
Expand Down Expand Up @@ -225,15 +228,7 @@ func syncServerIpList(newAppConfig *AppConfig) error{
panic("can not find apollo config!please confirm!")
}

var url string
if newAppConfig ==nil{
getServicesConfigUrl(appConfig)
}else{
url=newAppConfig.Ip
}
logger.Debug("url:",url)

_,err:=request(url,&ConnectConfig{
_,err:=request(getServicesConfigUrl(appConfig),&ConnectConfig{
},&CallBack{
SuccessCallBack:syncServerIpListSuccessCallBack,
})
Expand All @@ -249,19 +244,6 @@ func GetAppConfig(newAppConfig *AppConfig)*AppConfig {
return appConfig
}

func initRefreshInterval() error {
customizedRefreshInterval:=os.Getenv(refresh_interval_key)
if isNotEmpty(customizedRefreshInterval){
interval,err:=strconv.Atoi(customizedRefreshInterval)
if isNotNil(err) {
logger.Errorf("Config for apollo.refreshInterval is invalid:%s",customizedRefreshInterval)
return err
}
refresh_interval=time.Duration(interval)
}
return nil
}

func getConfigUrl(config *AppConfig) string{
return getConfigUrlByHost(config,config.getHost())
}
Expand Down Expand Up @@ -290,20 +272,6 @@ func getConfigUrlSuffix(config *AppConfig,newConfig *AppConfig) string{
getInternal())
}

func getNotifyUrl(notifications string,config *AppConfig) string{
return getNotifyUrlByHost(notifications,
config,
config.getHost())
}

func getNotifyUrlByHost(notifications string,config *AppConfig,host string) string{
return fmt.Sprintf("%snotifications/v2?appId=%s&cluster=%s&notifications=%s",
host,
url.QueryEscape(config.AppId),
url.QueryEscape(config.Cluster),
url.QueryEscape(notifications))
}

func getNotifyUrlSuffix(notifications string,config *AppConfig,newConfig *AppConfig) string{
if newConfig!=nil{
return ""
Expand Down
31 changes: 1 addition & 30 deletions app_config_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package agollo

import (
"github.com/zouyx/agollo/test"
"testing"
"os"
"strconv"
"time"
"github.com/zouyx/agollo/test"
)

func TestInit(t *testing.T) {
Expand Down Expand Up @@ -53,21 +51,6 @@ func TestStructInit(t *testing.T) {
initFileConfig()
}

func TestInitRefreshInterval_1(t *testing.T) {
os.Setenv(refresh_interval_key,"joe")

err:=initRefreshInterval()
test.NotNil(t,err)

interval:="3"
os.Setenv(refresh_interval_key,interval)
err=initRefreshInterval()
test.Nil(t,err)
i,_:=strconv.Atoi(interval)
test.Equal(t,time.Duration(i),refresh_interval)

}

func TestGetConfigUrl(t *testing.T) {
appConfig:=getTestAppConfig()
url:=getConfigUrl(appConfig)
Expand All @@ -80,18 +63,6 @@ func TestGetConfigUrlByHost(t *testing.T) {
test.StartWith(t,"http://baidu.com/configs/test/dev/application?releaseKey=&ip=",url)
}

func TestGetNotifyUrl(t *testing.T) {
appConfig:=getTestAppConfig()
url:=getNotifyUrl("notifys",appConfig)
test.Equal(t,"http://localhost:8888/notifications/v2?appId=test&cluster=dev&notifications=notifys",url)
}

func TestGetNotifyUrlByHost(t *testing.T) {
appConfig:=getTestAppConfig()
url:=getNotifyUrlByHost("notifys",appConfig,"http://baidu.com/")
test.Equal(t,"http://baidu.com/notifications/v2?appId=test&cluster=dev&notifications=notifys",url)
}

func TestGetServicesConfigUrl(t *testing.T) {
appConfig:=getTestAppConfig()
url:=getServicesConfigUrl(appConfig)
Expand Down
Loading

0 comments on commit 12b1f28

Please sign in to comment.