From 7e5c6ae812b60cb292d1817cd082c657a5ce4261 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Catt=C4=AB=20Cr=C5=ABd=C4=93l=C4=93s?= <17695588+wzy9607@users.noreply.github.com> Date: Thu, 14 Dec 2023 22:28:59 +0800 Subject: [PATCH] update: simplify the conversion from int to time.Duration as suggested by the golang offical doc. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://pkg.go.dev/time#pkg-constants Signed-off-by: Cattī Crūdēlēs <17695588+wzy9607@users.noreply.github.com> --- component/remote/abs.go | 8 +------- component/serverlist/sync.go | 17 ++++++----------- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/component/remote/abs.go b/component/remote/abs.go index 801a417..1952a80 100644 --- a/component/remote/abs.go +++ b/component/remote/abs.go @@ -18,7 +18,6 @@ package remote import ( - "strconv" "time" "github.com/apolloconfig/agollo/v4/component/log" @@ -47,12 +46,7 @@ func (a *AbsApolloConfig) SyncWithNamespace(namespace string, appConfigFunc func IsRetry: true, } if appConfig.SyncServerTimeout > 0 { - duration, err := time.ParseDuration(strconv.Itoa(appConfig.SyncServerTimeout) + "s") - if err != nil { - log.Errorf("parse sync server timeout %s fail, error:%v", appConfig.SyncServerTimeout, err) - return nil - } - c.Timeout = duration + c.Timeout = time.Duration(appConfig.SyncServerTimeout) * time.Second } callback := a.remoteApollo.CallBack(namespace) diff --git a/component/serverlist/sync.go b/component/serverlist/sync.go index 9bb0c40..a554239 100644 --- a/component/serverlist/sync.go +++ b/component/serverlist/sync.go @@ -19,7 +19,6 @@ package serverlist import ( "encoding/json" - "strconv" "time" "github.com/apolloconfig/agollo/v4/env/server" @@ -66,10 +65,10 @@ func (s *SyncServerIPListComponent) Start() { } } -//SyncServerIPList sync ip list from server -//then -//1.update agcache -//2.store in disk +// SyncServerIPList sync ip list from server +// then +// 1.update agcache +// 2.store in disk func SyncServerIPList(appConfigFunc func() config.AppConfig) (map[string]*config.ServerInfo, error) { if appConfigFunc == nil { panic("can not find apollo config!please confirm!") @@ -80,12 +79,8 @@ func SyncServerIPList(appConfigFunc func() config.AppConfig) (map[string]*config AppID: appConfig.AppID, Secret: appConfig.Secret, } - if appConfigFunc().SyncServerTimeout > 0 { - duration, err := time.ParseDuration(strconv.Itoa(appConfigFunc().SyncServerTimeout) + "s") - if err != nil { - return nil, err - } - c.Timeout = duration + if appConfig.SyncServerTimeout > 0 { + c.Timeout = time.Duration(appConfig.SyncServerTimeout) * time.Second } serverMap, err := http.Request(appConfig.GetServicesConfigURL(), c, &http.CallBack{ SuccessCallBack: SyncServerIPListSuccessCallBack,