From 56ea5a1284c85dbe74fb51e05f591804ad5bd4db Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Sat, 11 Apr 2020 18:47:55 +0800 Subject: [PATCH] refactor: all Automatic token refresh logic optimization --- clients/client_factory.go | 16 ++++++++-------- common/security/security_proxy.go | 8 +++++++- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/clients/client_factory.go b/clients/client_factory.go index 59578e16..45bad9fa 100644 --- a/clients/client_factory.go +++ b/clients/client_factory.go @@ -15,16 +15,16 @@ func CreateConfigClient(properties map[string]interface{}) (iClient config_clien nacosClient, errSetConfig := setConfig(properties) if errSetConfig != nil { err = errSetConfig - return nil, err + return } nacosClient.SetHttpAgent(&http_agent.HttpAgent{}) config, errNew := config_client.NewConfigClient(nacosClient) if errNew != nil { err = errNew - return nil, err + return } iClient = &config - return iClient, nil + return } // 创建服务发现相关的客户端 @@ -32,16 +32,16 @@ func CreateNamingClient(properties map[string]interface{}) (iClient naming_clien nacosClient, errSetConfig := setConfig(properties) if errSetConfig != nil { err = errSetConfig - return nil, err + return } nacosClient.SetHttpAgent(&http_agent.HttpAgent{}) naming, errNew := naming_client.NewNamingClient(nacosClient) if errNew != nil { err = errNew - return nil, err + return } iClient = &naming - return iClient, nil + return } func setConfig(properties map[string]interface{}) (iClient nacos_client.INacosClient, err error) { @@ -72,12 +72,12 @@ func setConfig(properties map[string]interface{}) (iClient nacos_client.INacosCl clientConfig, _ := client.GetClientConfig() if len(clientConfig.Endpoint) <= 0 { err = errors.New("server configs not found in properties") - return nil, err + return } client.SetServerConfig([]constant.ServerConfig{}) } iClient = &client - return iClient, nil + return } diff --git a/common/security/security_proxy.go b/common/security/security_proxy.go index aa603d95..aaf0373d 100644 --- a/common/security/security_proxy.go +++ b/common/security/security_proxy.go @@ -27,7 +27,6 @@ type AuthClient struct { } func NewAuthClient(clientCfg constant.ClientConfig, serverCfgs []constant.ServerConfig, agent http_agent.IHttpAgent) AuthClient { - client := AuthClient{ username: clientCfg.Username, password: clientCfg.Password, @@ -49,6 +48,13 @@ func (ac *AuthClient) GetAccessToken() string { } func (ac *AuthClient) AutoRefresh() { + + // If the username is not set, the automatic refresh Token is not enabled + + if ac.username == "" { + return + } + go func() { ticker := time.NewTicker(time.Millisecond * 5)