Skip to content

Commit

Permalink
Finish FixCacheNoRefresh
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyx committed Aug 31, 2017
2 parents 5ba51eb + 9aaa293 commit a11eac4
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 14 deletions.
6 changes: 4 additions & 2 deletions app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
refresh_interval = 5 *time.Minute //5m
refresh_interval_key = "apollo.refreshInterval" //

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

connect_timeout = 1 * time.Second //1s
Expand Down Expand Up @@ -193,7 +193,9 @@ func syncServerIpList() error{
url:=getServicesConfigUrl(appConfig)
seelog.Debug("url:",url)

_,err:=request(url,syncServerIpListSuccessCallBack)
_,err:=request(url,&CallBack{
SuccessCallBack:syncServerIpListSuccessCallBack,
})


return err
Expand Down
4 changes: 3 additions & 1 deletion componet_notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ func getRemoteConfig() ([]*apolloNotify,error) {

//seelog.Debugf("allNotifications.getNotifies():%s",allNotifications.getNotifies())

notifies ,err:=requestRecovery(appConfig,urlSuffix,getRemoteConfigSuccessCallBack)
notifies ,err:=requestRecovery(appConfig,urlSuffix,&CallBack{
SuccessCallBack:getRemoteConfigSuccessCallBack,
})

if notifies==nil{
return nil,err
Expand Down
5 changes: 4 additions & 1 deletion componet_timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ func autoSyncConfigServices() error {

urlSuffix:=getConfigUrlSuffix(appConfig)

_,err:=requestRecovery(appConfig,urlSuffix,autoSyncConfigServicesSuccessCallBack)
_,err:=requestRecovery(appConfig,urlSuffix,&CallBack{
SuccessCallBack:autoSyncConfigServicesSuccessCallBack,
NotModifyCallBack:touchApolloConfigCache,
})

return err
}
32 changes: 31 additions & 1 deletion componet_timer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
//}

func TestAutoSyncConfigServices(t *testing.T) {
runMockConfigServer(normalConfigResponse)
go runMockConfigServer(normalConfigResponse)
defer closeMockConfigServer()

time.Sleep(1*time.Second)
Expand All @@ -37,6 +37,36 @@ func TestAutoSyncConfigServices(t *testing.T) {
//test.Equal(t,"value2",config.Configurations["key2"])
}

//test if not modify
func TestAutoSyncConfigServicesNotModify(t *testing.T) {
go runMockConfigServer(notModifyConfigResponse)
defer closeMockConfigServer()

apolloConfig,err:=createApolloConfigWithJson([]byte(configResponseStr))
updateApolloConfig(apolloConfig)

time.Sleep(10*time.Second)
checkCacheLeft(t,configCacheExpireTime-10)

appConfig.NextTryConnTime=0

err=autoSyncConfigServices()

test.Nil(t,err)

config:=GetCurrentApolloConfig()

test.Equal(t,"100004458",config.AppId)
test.Equal(t,"default",config.Cluster)
test.Equal(t,"application",config.NamespaceName)
test.Equal(t,"20170430092936-dee2d58e74515ff3",config.ReleaseKey)

checkCacheLeft(t,configCacheExpireTime)

//test.Equal(t,"value1",config.Configurations["key1"])
//test.Equal(t,"value2",config.Configurations["key2"])
}



func TestAutoSyncConfigServicesError(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions config_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func normalConfigResponse(rw http.ResponseWriter, req *http.Request) {
}
}

func notModifyConfigResponse(rw http.ResponseWriter, req *http.Request) {
time.Sleep(800 * time.Microsecond)
rw.WriteHeader(http.StatusNotModified)
}

//Error response
//will hold 5s and keep response 404
func errorConfigResponse(rw http.ResponseWriter, req *http.Request) {
Expand Down
18 changes: 17 additions & 1 deletion repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const (
apolloConfigCacheSize=50*1024*1024

//1 minute
configCacheExpireTime=60
configCacheExpireTime=120
)
var (
currentConnApolloConfig *ApolloConnConfig=&ApolloConnConfig{}
Expand Down Expand Up @@ -48,6 +48,22 @@ func updateApolloConfigCache(configurations map[string]string,expireTime int) {
}
}

func touchApolloConfigCache() error{
updateApolloConfigCacheTime(configCacheExpireTime)
return nil
}

func updateApolloConfigCacheTime(expireTime int) {
it := apolloConfigCache.NewIterator()
for i := int64(0); i < apolloConfigCache.EntryCount(); i++ {
entry := it.Next()
if entry==nil{
break
}
apolloConfigCache.Set([]byte(entry.Key),[]byte(entry.Value),expireTime)
}
}

func GetApolloConfigCache() *freecache.Cache {
return apolloConfigCache
}
Expand Down
20 changes: 20 additions & 0 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ func createMockApolloConfig(expireTime int)map[string]string{
return configs
}

func TestTouchApolloConfigCache(t *testing.T) {
createMockApolloConfig(10)

time.Sleep(5*time.Second)
checkCacheLeft(t,5)

updateApolloConfigCacheTime(10)

checkCacheLeft(t,10)
}

func checkCacheLeft(t *testing.T,excepted uint32) {
it := apolloConfigCache.NewIterator()
for i := int64(0); i < apolloConfigCache.EntryCount(); i++ {
entry := it.Next()
left,_:=apolloConfigCache.TTL(entry.Key)
test.Equal(t,true,left==uint32(excepted))
}
}

func TestUpdateApolloConfigNull(t *testing.T) {
time.Sleep(1*time.Second)
var currentConfig *ApolloConnConfig
Expand Down
25 changes: 18 additions & 7 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import (
"fmt"
)

func request(url string,successCallBack func([]byte)(interface{},error)) (interface{},error){
type CallBack struct {
SuccessCallBack func([]byte)(interface{},error)
NotModifyCallBack func()error
}

func request(url string,callBack *CallBack) (interface{},error){
client := &http.Client{
Timeout:connect_timeout,
}
Expand Down Expand Up @@ -40,12 +45,18 @@ func request(url string,successCallBack func([]byte)(interface{},error)) (interf
continue
}

return successCallBack(responseBody)

if callBack!=nil&&callBack.SuccessCallBack!=nil {
return callBack.SuccessCallBack(responseBody)
}else{
return nil,nil
}
case http.StatusNotModified:
seelog.Warn("Config Not Modified:", err)
return nil, nil

if callBack!=nil&&callBack.NotModifyCallBack!=nil {
return nil,callBack.NotModifyCallBack()
}else{
return nil,nil
}
default:
seelog.Error("Connect Apollo Server Fail,Error:",err)
if res!=nil{
Expand All @@ -67,7 +78,7 @@ func request(url string,successCallBack func([]byte)(interface{},error)) (interf

func requestRecovery(appConfig *AppConfig,
urlSuffix string,
successCallBack func([]byte)(interface{},error))(interface{},error) {
callBack *CallBack)(interface{},error) {
format:="%s%s"
var err error
var response interface{}
Expand All @@ -79,7 +90,7 @@ func requestRecovery(appConfig *AppConfig,
}

requestUrl:=fmt.Sprintf(format,host,urlSuffix)
response,err=request(requestUrl,successCallBack)
response,err=request(requestUrl,callBack)
if err==nil{
return response,err
}
Expand Down
4 changes: 3 additions & 1 deletion request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ func TestRequestRecovery(t *testing.T) {
appConfig:=GetAppConfig()
urlSuffix:=getConfigUrlSuffix(appConfig)

o,err:=requestRecovery(appConfig,urlSuffix,autoSyncConfigServicesSuccessCallBack)
o,err:=requestRecovery(appConfig,urlSuffix,&CallBack{
SuccessCallBack:autoSyncConfigServicesSuccessCallBack,
})

test.Nil(t,err)
test.Nil(t,o)
Expand Down

0 comments on commit a11eac4

Please sign in to comment.