Skip to content

Commit

Permalink
Merge pull request #90 from sanxun0325/getServiceInfo_Concurrent
Browse files Browse the repository at this point in the history
[ISSUE #63]Get service info concurrent
  • Loading branch information
lzp0412 authored Jul 24, 2020
2 parents 56869ac + 8103660 commit 7a3fc3e
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ success, _ := namingClient.RegisterInstance(vo.RegisterInstanceParam{

```go

success, _ := namingClient.DeregisterInstance(vo.RegisterInstanceParam{
success, _ := namingClient.DeregisterInstance(vo.DeregisterInstanceParam{
Ip: "10.0.0.11",
Port: 8848,
ServiceName: "demo.go",
Expand Down
109 changes: 109 additions & 0 deletions clients/naming_client/host_reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,117 @@ package naming_client

import (
"testing"

"github.com/nacos-group/nacos-sdk-go/utils"

"github.com/stretchr/testify/assert"

"github.com/nacos-group/nacos-sdk-go/vo"

"github.com/nacos-group/nacos-sdk-go/clients/nacos_client"
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/common/http_agent"
)

func TestHostReactor_GetServiceInfo(t *testing.T) {
nc := nacos_client.NacosClient{}
nc.SetServerConfig([]constant.ServerConfig{serverConfigTest})
nc.SetClientConfig(clientConfigTest)
nc.SetHttpAgent(&http_agent.HttpAgent{})
client, _ := NewNamingClient(&nc)
param := vo.RegisterInstanceParam{
Ip: "10.0.0.11",
Port: 8848,
ServiceName: "test",
Weight: 10,
ClusterName: "test",
Enable: true,
Healthy: true,
Ephemeral: true,
}
if param.GroupName == "" {
param.GroupName = constant.DEFAULT_GROUP
}
param.ServiceName = utils.GetGroupName(param.ServiceName, param.GroupName)
client.RegisterInstance(param)
_, err := client.hostReactor.GetServiceInfo(param.ServiceName, param.ClusterName)
assert.Nil(t, err)
}

func TestHostReactor_GetServiceInfoErr(t *testing.T) {
nc := nacos_client.NacosClient{}
nc.SetServerConfig([]constant.ServerConfig{serverConfigTest})
nc.SetClientConfig(clientConfigTest)
nc.SetHttpAgent(&http_agent.HttpAgent{})
client, _ := NewNamingClient(&nc)
param := vo.RegisterInstanceParam{
Ip: "10.0.0.11",
Port: 8848,
ServiceName: "test",
Weight: 10,
ClusterName: "test",
Enable: true,
Healthy: true,
Ephemeral: true,
}
client.RegisterInstance(param)
_, err := client.hostReactor.GetServiceInfo(param.ServiceName, param.ClusterName)
assert.NotNil(t, err)
}

func TestHostReactor_GetServiceInfoConcurrent(t *testing.T) {
nc := nacos_client.NacosClient{}
nc.SetServerConfig([]constant.ServerConfig{serverConfigTest})
nc.SetClientConfig(clientConfigTest)
nc.SetHttpAgent(&http_agent.HttpAgent{})
client, _ := NewNamingClient(&nc)
param := vo.RegisterInstanceParam{
Ip: "10.0.0.11",
Port: 8848,
ServiceName: "test",
Weight: 10,
ClusterName: "test",
Enable: true,
Healthy: true,
Ephemeral: true,
}
if param.GroupName == "" {
param.GroupName = constant.DEFAULT_GROUP
}
param.ServiceName = utils.GetGroupName(param.ServiceName, param.GroupName)
client.RegisterInstance(param)
for i := 0; i < 10000; i++ {
go func() {
_, err := client.hostReactor.GetServiceInfo(param.ServiceName, param.ClusterName)
assert.Nil(t, err)
}()

}
}

func BenchmarkHostReactor_GetServiceInfoConcurrent(b *testing.B) {
nc := nacos_client.NacosClient{}
nc.SetServerConfig([]constant.ServerConfig{serverConfigTest})
nc.SetClientConfig(clientConfigTest)
nc.SetHttpAgent(&http_agent.HttpAgent{})
client, _ := NewNamingClient(&nc)
param := vo.RegisterInstanceParam{
Ip: "10.0.0.11",
Port: 8848,
ServiceName: "test",
Weight: 10,
ClusterName: "test",
Enable: true,
Healthy: true,
Ephemeral: true,
}
if param.GroupName == "" {
param.GroupName = constant.DEFAULT_GROUP
}
param.ServiceName = utils.GetGroupName(param.ServiceName, param.GroupName)
client.RegisterInstance(param)
b.ResetTimer()
for i := 0; i < b.N; i++ {
client.hostReactor.GetServiceInfo(param.ServiceName, param.ClusterName)
}
}
15 changes: 8 additions & 7 deletions clients/naming_client/host_reator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package naming_client

import (
"encoding/json"
"errors"
"log"
"reflect"
"time"
Expand Down Expand Up @@ -84,17 +85,17 @@ func (hr *HostReactor) ProcessServiceJson(result string) {
}
}

func (hr *HostReactor) GetServiceInfo(serviceName string, clusters string) model.Service {
func (hr *HostReactor) GetServiceInfo(serviceName string, clusters string) (model.Service, error) {
key := utils.GetServiceCacheKey(serviceName, clusters)
cacheService, ok := hr.serviceInfoMap.Get(key)
if !ok {
cacheService = model.Service{Name: serviceName, Clusters: clusters}
hr.serviceInfoMap.Set(key, cacheService)
hr.updateServiceNow(serviceName, clusters)
if cacheService, ok = hr.serviceInfoMap.Get(key); !ok {
return model.Service{}, errors.New("get service info failed")
}
}
newService, _ := hr.serviceInfoMap.Get(key)

return newService.(model.Service)
return cacheService.(model.Service), nil
}

func (hr *HostReactor) GetAllServiceInfo(nameSpace, groupName string, pageNo, pageSize uint32) model.ServiceList {
Expand All @@ -117,8 +118,9 @@ func (hr *HostReactor) GetAllServiceInfo(nameSpace, groupName string, pageNo, pa
return data
}

func (hr *HostReactor) updateServiceNow(serviceName string, clusters string) {
func (hr *HostReactor) updateServiceNow(serviceName, clusters string) {
result, err := hr.serviceProxy.QueryList(serviceName, clusters, hr.pushReceiver.port, false)

if err != nil {
log.Printf("[ERROR]:query list return error!servieName:%s cluster:%s err:%s \n", serviceName, clusters, err.Error())
return
Expand Down Expand Up @@ -149,5 +151,4 @@ func (hr *HostReactor) asyncUpdateService() {
}
time.Sleep(1 * time.Second)
}

}
18 changes: 12 additions & 6 deletions clients/naming_client/naming_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ func (sc *NamingClient) GetService(param vo.GetServiceParam) (model.Service, err
if param.GroupName == "" {
param.GroupName = constant.DEFAULT_GROUP
}
service := sc.hostReactor.GetServiceInfo(utils.GetGroupName(param.ServiceName, param.GroupName), strings.Join(param.Clusters, ","))
return service, nil
service, err := sc.hostReactor.GetServiceInfo(utils.GetGroupName(param.ServiceName, param.GroupName), strings.Join(param.Clusters, ","))
return service, err
}

func (sc *NamingClient) GetAllServicesInfo(param vo.GetAllServiceInfoParam) (model.ServiceList, error) {
Expand All @@ -141,18 +141,21 @@ func (sc *NamingClient) SelectAllInstances(param vo.SelectAllInstancesParam) ([]
if param.GroupName == "" {
param.GroupName = constant.DEFAULT_GROUP
}
service := sc.hostReactor.GetServiceInfo(utils.GetGroupName(param.ServiceName, param.GroupName), strings.Join(param.Clusters, ","))
service, err := sc.hostReactor.GetServiceInfo(utils.GetGroupName(param.ServiceName, param.GroupName), strings.Join(param.Clusters, ","))
if service.Hosts == nil || len(service.Hosts) == 0 {
return []model.Instance{}, errors.New("instance list is empty!")
}
return service.Hosts, nil
return service.Hosts, err
}

func (sc *NamingClient) SelectInstances(param vo.SelectInstancesParam) ([]model.Instance, error) {
if param.GroupName == "" {
param.GroupName = constant.DEFAULT_GROUP
}
service := sc.hostReactor.GetServiceInfo(utils.GetGroupName(param.ServiceName, param.GroupName), strings.Join(param.Clusters, ","))
service, err := sc.hostReactor.GetServiceInfo(utils.GetGroupName(param.ServiceName, param.GroupName), strings.Join(param.Clusters, ","))
if err != nil {
return nil, err
}
return sc.selectInstances(service, param.HealthyOnly)
}

Expand All @@ -174,7 +177,10 @@ func (sc *NamingClient) SelectOneHealthyInstance(param vo.SelectOneHealthInstanc
if param.GroupName == "" {
param.GroupName = constant.DEFAULT_GROUP
}
service := sc.hostReactor.GetServiceInfo(utils.GetGroupName(param.ServiceName, param.GroupName), strings.Join(param.Clusters, ","))
service, err := sc.hostReactor.GetServiceInfo(utils.GetGroupName(param.ServiceName, param.GroupName), strings.Join(param.Clusters, ","))
if err != nil {
return nil, err
}
return sc.selectOneHealthyInstances(service)
}

Expand Down

0 comments on commit 7a3fc3e

Please sign in to comment.