Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunrisea committed Feb 19, 2025
1 parent f726c05 commit 3fdfc74
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 170 deletions.
2 changes: 1 addition & 1 deletion clients/naming_client/naming_grpc/naming_grpc_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package naming_grpc

import (
"context"
"github.com/nacos-group/nacos-sdk-go/v2/common/security"
"time"

"github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client/naming_cache"
Expand All @@ -29,6 +28,7 @@ import (
"github.com/nacos-group/nacos-sdk-go/v2/common/remote/rpc"
"github.com/nacos-group/nacos-sdk-go/v2/common/remote/rpc/rpc_request"
"github.com/nacos-group/nacos-sdk-go/v2/common/remote/rpc/rpc_response"
"github.com/nacos-group/nacos-sdk-go/v2/common/security"
"github.com/nacos-group/nacos-sdk-go/v2/inner/uuid"
"github.com/nacos-group/nacos-sdk-go/v2/model"
"github.com/nacos-group/nacos-sdk-go/v2/util"
Expand Down
171 changes: 91 additions & 80 deletions common/constant/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,84 +27,95 @@ const (
UNKNOWN_KMS_VERSION KMSVersion = "UNKNOWN_KMS_VERSION"
)
const (
KEY_USERNAME = "username"
KEY_PASSWORD = "password"
KEY_ENDPOINT = "endpoint"
KEY_NAME_SPACE = "namespace"
KEY_ACCESS_KEY = "accessKey"
KEY_SECRET_KEY = "secretKey"
KEY_SERVER_ADDR = "serverAddr"
KEY_CONTEXT_PATH = "contextPath"
KEY_ENCODE = "encode"
KEY_DATA_ID = "dataId"
KEY_GROUP = "group"
KEY_TENANT = "tenant"
KEY_DESC = "desc"
KEY_APP_NAME = "appName"
KEY_CONTENT = "content"
KEY_TIMEOUT_MS = "timeoutMs"
KEY_LISTEN_INTERVAL = "listenInterval"
KEY_SERVER_CONFIGS = "serverConfigs"
KEY_CLIENT_CONFIG = "clientConfig"
KEY_TOKEN = "token"
KEY_ACCESS_TOKEN = "accessToken"
KEY_TOKEN_TTL = "tokenTtl"
KEY_GLOBAL_ADMIN = "globalAdmin"
KEY_TOKEN_REFRESH_WINDOW = "tokenRefreshWindow"
WEB_CONTEXT = "/nacos"
CONFIG_BASE_PATH = "/v1/cs"
CONFIG_PATH = CONFIG_BASE_PATH + "/configs"
CONFIG_AGG_PATH = "/datum.do"
CONFIG_LISTEN_PATH = CONFIG_BASE_PATH + "/configs/listener"
SERVICE_BASE_PATH = "/v1/ns"
SERVICE_PATH = SERVICE_BASE_PATH + "/instance"
SERVICE_INFO_PATH = SERVICE_BASE_PATH + "/service"
SERVICE_SUBSCRIBE_PATH = SERVICE_PATH + "/list"
NAMESPACE_PATH = "/v1/console/namespaces"
SPLIT_CONFIG = string(rune(1))
SPLIT_CONFIG_INNER = string(rune(2))
KEY_LISTEN_CONFIGS = "Listening-Configs"
KEY_SERVICE_NAME = "serviceName"
KEY_IP = "ip"
KEY_PORT = "port"
KEY_WEIGHT = "weight"
KEY_ENABLE = "enable"
KEY_HEALTHY = "healthy"
KEY_METADATA = "metadata"
KEY_CLUSTER_NAME = "clusterName"
KEY_CLUSTER = "cluster"
KEY_BEAT = "beat"
KEY_DOM = "dom"
DEFAULT_CONTEXT_PATH = "/nacos"
CLIENT_VERSION = "Nacos-Go-Client:v2.2.7"
REQUEST_DOMAIN_RETRY_TIME = 3
SERVICE_INFO_SPLITER = "@@"
CONFIG_INFO_SPLITER = "@@"
DEFAULT_NAMESPACE_ID = "public"
DEFAULT_GROUP = "DEFAULT_GROUP"
NAMING_INSTANCE_ID_SPLITTER = "#"
DefaultClientErrorCode = "SDK.NacosError"
DEFAULT_SERVER_SCHEME = "http"
HTTPS_SERVER_SCHEME = "https"
LABEL_SOURCE = "source"
LABEL_SOURCE_SDK = "sdk"
LABEL_MODULE = "module"
LABEL_MODULE_CONFIG = "config"
LABEL_MODULE_NAMING = "naming"
RESPONSE_CODE_SUCCESS = 200
UN_REGISTER = 301
KEEP_ALIVE_TIME = 5
DEFAULT_TIMEOUT_MILLS = 3000
ALL_SYNC_INTERNAL = 5 * time.Minute
CLIENT_APPNAME_HEADER = "Client-AppName"
APPNAME_HEADER = "AppName"
CLIENT_REQUEST_TS_HEADER = "Client-RequestTS"
CLIENT_REQUEST_TOKEN_HEADER = "Client-RequestToken"
EX_CONFIG_INFO = "exConfigInfo"
CHARSET_KEY = "charset"
LOG_FILE_NAME = "nacos-sdk.log"
HTTPS_SERVER_PORT = 443
GRPC = "grpc"
RpcPortOffset = 1000
MSE_KMSv1_DEFAULT_KEY_ID = "alias/acs/mse"
KEY_USERNAME = "username"
KEY_PASSWORD = "password"
KEY_ENDPOINT = "endpoint"
KEY_NAME_SPACE = "namespace"
KEY_ACCESS_KEY = "accessKey"
KEY_SECRET_KEY = "secretKey"
KEY_SERVER_ADDR = "serverAddr"
KEY_CONTEXT_PATH = "contextPath"
KEY_ENCODE = "encode"
KEY_DATA_ID = "dataId"
KEY_GROUP = "group"
KEY_TENANT = "tenant"
KEY_DESC = "desc"
KEY_APP_NAME = "appName"
KEY_CONTENT = "content"
KEY_TIMEOUT_MS = "timeoutMs"
KEY_LISTEN_INTERVAL = "listenInterval"
KEY_SERVER_CONFIGS = "serverConfigs"
KEY_CLIENT_CONFIG = "clientConfig"
KEY_TOKEN = "token"
KEY_ACCESS_TOKEN = "accessToken"
KEY_TOKEN_TTL = "tokenTtl"
KEY_GLOBAL_ADMIN = "globalAdmin"
KEY_TOKEN_REFRESH_WINDOW = "tokenRefreshWindow"
WEB_CONTEXT = "/nacos"
CONFIG_BASE_PATH = "/v1/cs"
CONFIG_PATH = CONFIG_BASE_PATH + "/configs"
CONFIG_AGG_PATH = "/datum.do"
CONFIG_LISTEN_PATH = CONFIG_BASE_PATH + "/configs/listener"
SERVICE_BASE_PATH = "/v1/ns"
SERVICE_PATH = SERVICE_BASE_PATH + "/instance"
SERVICE_INFO_PATH = SERVICE_BASE_PATH + "/service"
SERVICE_SUBSCRIBE_PATH = SERVICE_PATH + "/list"
NAMESPACE_PATH = "/v1/console/namespaces"
SPLIT_CONFIG = string(rune(1))
SPLIT_CONFIG_INNER = string(rune(2))
KEY_LISTEN_CONFIGS = "Listening-Configs"
KEY_SERVICE_NAME = "serviceName"
KEY_IP = "ip"
KEY_PORT = "port"
KEY_WEIGHT = "weight"
KEY_ENABLE = "enable"
KEY_HEALTHY = "healthy"
KEY_METADATA = "metadata"
KEY_CLUSTER_NAME = "clusterName"
KEY_CLUSTER = "cluster"
KEY_BEAT = "beat"
KEY_DOM = "dom"
DEFAULT_CONTEXT_PATH = "/nacos"
CLIENT_VERSION = "Nacos-Go-Client:v2.2.7"
REQUEST_DOMAIN_RETRY_TIME = 3
SERVICE_INFO_SPLITER = "@@"
CONFIG_INFO_SPLITER = "@@"
DEFAULT_NAMESPACE_ID = "public"
DEFAULT_GROUP = "DEFAULT_GROUP"
NAMING_INSTANCE_ID_SPLITTER = "#"
DefaultClientErrorCode = "SDK.NacosError"
DEFAULT_SERVER_SCHEME = "http"
HTTPS_SERVER_SCHEME = "https"
LABEL_SOURCE = "source"
LABEL_SOURCE_SDK = "sdk"
LABEL_MODULE = "module"
LABEL_MODULE_CONFIG = "config"
LABEL_MODULE_NAMING = "naming"
RESPONSE_CODE_SUCCESS = 200
UN_REGISTER = 301
KEEP_ALIVE_TIME = 5
DEFAULT_TIMEOUT_MILLS = 3000
ALL_SYNC_INTERNAL = 5 * time.Minute
CLIENT_APPNAME_HEADER = "Client-AppName"
APPNAME_HEADER = "AppName"
CLIENT_REQUEST_TS_HEADER = "Client-RequestTS"
CLIENT_REQUEST_TOKEN_HEADER = "Client-RequestToken"
EX_CONFIG_INFO = "exConfigInfo"
CHARSET_KEY = "charset"
LOG_FILE_NAME = "nacos-sdk.log"
HTTPS_SERVER_PORT = 443
GRPC = "grpc"
RpcPortOffset = 1000
MSE_KMSv1_DEFAULT_KEY_ID = "alias/acs/mse"
CONFIG_PUBLISH_REQUEST_NAME = "ConfigPublishRequest"
CONFIG_QUERY_REQUEST_NAME = "ConfigQueryRequest"
CONFIG_REMOVE_REQUEST_NAME = "ConfigRemoveRequest"
INSTANCE_REQUEST_NAME = "InstanceRequest"
BATCH_INSTANCE_REQUEST_NAME = "BatchInstanceRequest"
SERVICE_LIST_REQUEST_NAME = "ServiceListRequest"
SERVICE_QUERY_REQUEST_NAME = "ServiceQueryRequest"
SUBSCRIBE_SERVICE_REQUEST_NAME = "SubscribeServiceRequest"
NOTIFY_SUBSCRIBE_REQUEST_NAME = "NotifySubscribeRequest"
CONFIG_BATCH_LISTEN_REQUEST_NAME = "ConfigBatchListenRequest"
CONFIG_CHANGE_NOTIFY_REQUEST_NAME = "ConfigChangeNotifyRequest"
)
13 changes: 7 additions & 6 deletions common/nacos_server/nacos_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ package nacos_server

import (
"context"
"testing"

"github.com/nacos-group/nacos-sdk-go/v2/common/http_agent"
"github.com/nacos-group/nacos-sdk-go/v2/common/security"
"testing"

"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -70,7 +71,7 @@ func TestNacosServer_InjectSignForNamingHttp_NoAk(t *testing.T) {
t.FailNow()
}

param := make(map[string]string)
param := make(map[string]string, 4)
param["serviceName"] = "s-0"
param["groupName"] = "g-0"
server.InjectSecurityInfo(param, security.BuildNamingResource(param["namespaceId"], param["groupName"], param["serviceName"]))
Expand All @@ -89,7 +90,7 @@ func TestNacosServer_InjectSignForNamingHttp_WithGroup(t *testing.T) {
t.FailNow()
}

param := make(map[string]string)
param := make(map[string]string, 4)
param["serviceName"] = "s-0"
param["groupName"] = "g-0"
server.InjectSecurityInfo(param, security.BuildNamingResource(param["namespaceId"], param["groupName"], param["serviceName"]))
Expand All @@ -109,7 +110,7 @@ func TestNacosServer_InjectSignForNamingHttp_WithoutGroup(t *testing.T) {
t.FailNow()
}

param := make(map[string]string)
param := make(map[string]string, 4)
param["serviceName"] = "s-0"
server.InjectSecurityInfo(param, security.BuildNamingResource(param["namespaceId"], param["groupName"], param["serviceName"]))
assert.Equal(t, "123", param["ak"])
Expand All @@ -129,7 +130,7 @@ func TestNacosServer_InjectSignForNamingHttp_WithoutServiceName(t *testing.T) {
t.FailNow()
}

param := make(map[string]string)
param := make(map[string]string, 4)
param["groupName"] = "g-0"
server.InjectSecurityInfo(param, security.BuildNamingResource(param["namespaceId"], param["groupName"], param["serviceName"]))
assert.Equal(t, "123", param["ak"])
Expand All @@ -149,7 +150,7 @@ func TestNacosServer_InjectSignForNamingHttp_WithoutServiceNameAndGroup(t *testi
t.FailNow()
}

param := make(map[string]string)
param := make(map[string]string, 4)
server.InjectSecurityInfo(param, security.BuildNamingResource(param["namespaceId"], param["serviceName"], param["groupName"]))
assert.Equal(t, "123", param["ak"])
assert.NotContains(t, param["data"], "@@")
Expand Down
17 changes: 10 additions & 7 deletions common/remote/rpc/rpc_request/config_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package rpc_request

import "github.com/nacos-group/nacos-sdk-go/v2/model"
import (
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
"github.com/nacos-group/nacos-sdk-go/v2/model"
)

type ConfigRequest struct {
*Request
Expand Down Expand Up @@ -51,7 +54,7 @@ func (r *ConfigRequest) GetTenant() string {
return r.Tenant
}

//request of listening a batch of configs.
// request of listening a batch of configs.
type ConfigBatchListenRequest struct {
*ConfigRequest
Listen bool `json:"listen"`
Expand All @@ -67,7 +70,7 @@ func NewConfigBatchListenRequest(cacheLen int) *ConfigBatchListenRequest {
}

func (r *ConfigBatchListenRequest) GetRequestType() string {
return "ConfigBatchListenRequest"
return constant.CONFIG_BATCH_LISTEN_REQUEST_NAME
}

type ConfigChangeNotifyRequest struct {
Expand All @@ -79,7 +82,7 @@ func NewConfigChangeNotifyRequest(group, dataId, tenant string) *ConfigChangeNot
}

func (r *ConfigChangeNotifyRequest) GetRequestType() string {
return "ConfigChangeNotifyRequest"
return constant.CONFIG_CHANGE_NOTIFY_REQUEST_NAME
}

type ConfigQueryRequest struct {
Expand All @@ -92,7 +95,7 @@ func NewConfigQueryRequest(group, dataId, tenant string) *ConfigQueryRequest {
}

func (r *ConfigQueryRequest) GetRequestType() string {
return "ConfigQueryRequest"
return constant.CONFIG_QUERY_REQUEST_NAME
}

type ConfigPublishRequest struct {
Expand All @@ -108,7 +111,7 @@ func NewConfigPublishRequest(group, dataId, tenant, content, casMd5 string) *Con
}

func (r *ConfigPublishRequest) GetRequestType() string {
return "ConfigPublishRequest"
return constant.CONFIG_PUBLISH_REQUEST_NAME
}

type ConfigRemoveRequest struct {
Expand All @@ -120,5 +123,5 @@ func NewConfigRemoveRequest(group, dataId, tenant string) *ConfigRemoveRequest {
}

func (r *ConfigRemoveRequest) GetRequestType() string {
return "ConfigRemoveRequest"
return constant.CONFIG_REMOVE_REQUEST_NAME
}
13 changes: 7 additions & 6 deletions common/remote/rpc/rpc_request/naming_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strconv"
"time"

"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
"github.com/nacos-group/nacos-sdk-go/v2/model"
)

Expand Down Expand Up @@ -68,7 +69,7 @@ func NewInstanceRequest(namespace, serviceName, groupName, Type string, instance
}

func (r *InstanceRequest) GetRequestType() string {
return "InstanceRequest"
return constant.INSTANCE_REQUEST_NAME
}

type BatchInstanceRequest struct {
Expand All @@ -86,7 +87,7 @@ func NewBatchInstanceRequest(namespace, serviceName, groupName, Type string, ins
}

func (r *BatchInstanceRequest) GetRequestType() string {
return "BatchInstanceRequest"
return constant.BATCH_INSTANCE_REQUEST_NAME
}

type NotifySubscriberRequest struct {
Expand All @@ -95,7 +96,7 @@ type NotifySubscriberRequest struct {
}

func (r *NotifySubscriberRequest) GetRequestType() string {
return "NotifySubscriberRequest"
return constant.NOTIFY_SUBSCRIBE_REQUEST_NAME
}

type ServiceListRequest struct {
Expand All @@ -115,7 +116,7 @@ func NewServiceListRequest(namespace, serviceName, groupName string, pageNo, pag
}

func (r *ServiceListRequest) GetRequestType() string {
return "ServiceListRequest"
return constant.SERVICE_LIST_REQUEST_NAME
}

type SubscribeServiceRequest struct {
Expand All @@ -133,7 +134,7 @@ func NewSubscribeServiceRequest(namespace, serviceName, groupName, clusters stri
}

func (r *SubscribeServiceRequest) GetRequestType() string {
return "SubscribeServiceRequest"
return constant.SUBSCRIBE_SERVICE_REQUEST_NAME
}

type ServiceQueryRequest struct {
Expand All @@ -153,5 +154,5 @@ func NewServiceQueryRequest(namespace, serviceName, groupName, cluster string, h
}

func (r *ServiceQueryRequest) GetRequestType() string {
return "ServiceQueryRequest"
return constant.SERVICE_QUERY_REQUEST_NAME
}
2 changes: 1 addition & 1 deletion common/security/nacos_auth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (ac *NacosAuthClient) GetAccessToken() string {
}

func (ac *NacosAuthClient) GetSecurityInfo(resource RequestResource) map[string]string {
var securityInfo = make(map[string]string)
var securityInfo = make(map[string]string, 4)
v := ac.accessToken.Load()
if v != nil {
securityInfo[constant.KEY_ACCESS_TOKEN] = v.(string)
Expand Down
Loading

0 comments on commit 3fdfc74

Please sign in to comment.