Skip to content

Commit

Permalink
Add Request-Module header when requesting endpoint (#685)
Browse files Browse the repository at this point in the history
* Add Request-Module header when requesting endpoint

* Sync headers
  • Loading branch information
AlbumenJ authored Nov 30, 2023
1 parent d934e30 commit 355c2fd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion clients/config_client/config_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type ConfigProxy struct {
func NewConfigProxy(ctx context.Context, serverConfig []constant.ServerConfig, clientConfig constant.ClientConfig, httpAgent http_agent.IHttpAgent) (IConfigProxy, error) {
proxy := ConfigProxy{}
var err error
proxy.nacosServer, err = nacos_server.NewNacosServer(ctx, serverConfig, clientConfig, httpAgent, clientConfig.TimeoutMs, clientConfig.Endpoint)
proxy.nacosServer, err = nacos_server.NewNacosServer(ctx, serverConfig, clientConfig, httpAgent, clientConfig.TimeoutMs, clientConfig.Endpoint, nil)
proxy.clientConfig = clientConfig
return &proxy, err
}
Expand Down
13 changes: 12 additions & 1 deletion clients/naming_client/naming_proxy_delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package naming_client

import (
"context"
"github.com/nacos-group/nacos-sdk-go/v2/inner/uuid"

"github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client/naming_cache"
"github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client/naming_grpc"
Expand All @@ -40,7 +41,17 @@ type NamingProxyDelegate struct {
func NewNamingProxyDelegate(ctx context.Context, clientCfg constant.ClientConfig, serverCfgs []constant.ServerConfig,
httpAgent http_agent.IHttpAgent, serviceInfoHolder *naming_cache.ServiceInfoHolder) (naming_proxy.INamingProxy, error) {

nacosServer, err := nacos_server.NewNacosServer(ctx, serverCfgs, clientCfg, httpAgent, clientCfg.TimeoutMs, clientCfg.Endpoint)
uid, err := uuid.NewV4()
if err != nil {
return nil, err
}
namingHeader := map[string][]string{
"Client-Version": {constant.CLIENT_VERSION},
"User-Agent": {constant.CLIENT_VERSION},
"RequestId": {uid.String()},
"Request-Module": {"Naming"},
}
nacosServer, err := nacos_server.NewNacosServer(ctx, serverCfgs, clientCfg, httpAgent, clientCfg.TimeoutMs, clientCfg.Endpoint, namingHeader)
if err != nil {
return nil, err
}
Expand Down
12 changes: 7 additions & 5 deletions common/nacos_server/nacos_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ type NacosServer struct {
contextPath string
endpointContextPath string
endpointQueryParams string
endpointQueryHeader map[string][]string
clusterName string
currentIndex int32
ServerSrcChangeSignal chan struct{}
}

func NewNacosServer(ctx context.Context, serverList []constant.ServerConfig, clientCfg constant.ClientConfig, httpAgent http_agent.IHttpAgent, timeoutMs uint64, endpoint string) (*NacosServer, error) {
func NewNacosServer(ctx context.Context, serverList []constant.ServerConfig, clientCfg constant.ClientConfig, httpAgent http_agent.IHttpAgent, timeoutMs uint64, endpoint string, endpointQueryHeader map[string][]string) (*NacosServer, error) {
severLen := len(serverList)
if severLen == 0 && endpoint == "" {
return &NacosServer{}, errors.New("both serverlist and endpoint are empty")
Expand All @@ -80,6 +81,7 @@ func NewNacosServer(ctx context.Context, serverList []constant.ServerConfig, cli
vipSrvRefInterMills: 10000,
endpointContextPath: clientCfg.EndpointContextPath,
endpointQueryParams: clientCfg.EndpointQueryParams,
endpointQueryHeader: endpointQueryHeader,
clusterName: clientCfg.ClusterName,
contextPath: clientCfg.ContextPath,
ServerSrcChangeSignal: make(chan struct{}, 1),
Expand Down Expand Up @@ -285,29 +287,29 @@ func (server *NacosServer) initRefreshSrvIfNeed(ctx context.Context) {
}
logger.Infof("nacos address server url: <%s>", urlString)

server.refreshServerSrvIfNeed(urlString)
server.refreshServerSrvIfNeed(urlString, server.endpointQueryHeader)
go func() {
for {
select {
case <-ctx.Done():
return
default:
time.Sleep(time.Duration(10) * time.Second)
server.refreshServerSrvIfNeed(urlString)
server.refreshServerSrvIfNeed(urlString, server.endpointQueryHeader)
}
}
}()

}

func (server *NacosServer) refreshServerSrvIfNeed(urlString string) {
func (server *NacosServer) refreshServerSrvIfNeed(urlString string, header map[string][]string) {
if util.CurrentMillis()-server.lastSrvRefTime < server.vipSrvRefInterMills && len(server.serverList) > 0 {
return
}

var list []string

result := server.httpAgent.RequestOnlyResult(http.MethodGet, urlString, nil, server.timeoutMs, nil)
result := server.httpAgent.RequestOnlyResult(http.MethodGet, urlString, header, server.timeoutMs, nil)
list = strings.Split(result, "\n")

var servers []constant.ServerConfig
Expand Down
3 changes: 2 additions & 1 deletion common/nacos_server/nacos_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func buildNacosServer(clientConfig constant.ClientConfig) (*NacosServer, error)
clientConfig,
&http_agent.HttpAgent{},
1000,
"")
"",
nil)
}

func TestNacosServer_InjectSignForNamingHttp_NoAk(t *testing.T) {
Expand Down

0 comments on commit 355c2fd

Please sign in to comment.