From c0c8155424f460e8bc9b83c140faec205de1d552 Mon Sep 17 00:00:00 2001
From: mengxiaodong <920432478@qq.com>
Date: Thu, 14 Jan 2021 15:20:49 +0800
Subject: [PATCH 1/4] Formatting and typos fixes
---
README_CN.md | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/README_CN.md b/README_CN.md
index e02118ad..08291062 100644
--- a/README_CN.md
+++ b/README_CN.md
@@ -55,12 +55,12 @@ constant.ServerConfig{
}
```
-Note:我们可以配置多个ServerConfig,客户端会对这些服务端做轮训请求
+Note:我们可以配置多个ServerConfig,客户端会对这些服务端做轮询请求
### Create client
```go
-//创建clientConfig
+// 创建clientConfig
clientConfig := constant.ClientConfig{
NamespaceId: "e525eafa-f7d7-4029-83d9-008937f9d468", // 如果需要支持多namespace,我们可以场景多个client,它们有不同的NamespaceId
TimeoutMs: 5000,
@@ -72,7 +72,7 @@ clientConfig := constant.ClientConfig{
LogLevel: "debug",
}
-//创建clientConfig的另一种方式
+// 创建clientConfig的另一种方式
clientConfig := *constant.NewClientConfig(
constant.WithNamespaceId("e525eafa-f7d7-4029-83d9-008937f9d468"),
constant.WithTimeoutMs(5000),
@@ -100,7 +100,7 @@ serverConfigs := []constant.ServerConfig{
},
}
-//创建serverConfig的另一种方式
+// 创建serverConfig的另一种方式
serverConfigs := []constant.ServerConfig{
*constant.NewServerConfig(
"console1.nacos.io",
@@ -185,7 +185,7 @@ success, err := namingClient.RegisterInstance(vo.RegisterInstanceParam{
Ephemeral: true,
Metadata: map[string]string{"idc":"shanghai"},
ClusterName: "cluster-a", // 默认值DEFAULT
- GroupName: "group-a", // 默认值DEFAULT_GROUP
+ GroupName: "group-a", // 默认值DEFAULT_GROUP
})
```
@@ -200,7 +200,7 @@ success, err := namingClient.DeregisterInstance(vo.DeregisterInstanceParam{
ServiceName: "demo.go",
Ephemeral: true,
Cluster: "cluster-a", // 默认值DEFAULT
- GroupName: "group-a", // 默认值DEFAULT_GROUP
+ GroupName: "group-a", // 默认值DEFAULT_GROUP
})
```
@@ -242,10 +242,10 @@ instances, err := namingClient.SelectInstances(vo.SelectInstancesParam{
```
-* 获取一个健康的实例(加权随机轮训):SelectOneHealthyInstance
+* 获取一个健康的实例(加权随机轮询):SelectOneHealthyInstance
```go
-// SelectOneHealthyInstance将会按加权随机轮训的负载均衡策略返回一个健康的实例
+// SelectOneHealthyInstance将会按加权随机轮询的负载均衡策略返回一个健康的实例
// 实例必须满足的条件:health=true,enable=true and weight>0
instance, err := namingClient.SelectOneHealthyInstance(vo.SelectOneHealthInstanceParam{
ServiceName: "demo.go",
From 91c7002e154cc6a0efb74e55ef8788f77b7655a5 Mon Sep 17 00:00:00 2001
From: mengxiaodong <920432478@qq.com>
Date: Thu, 14 Jan 2021 15:53:47 +0800
Subject: [PATCH 2/4] fix deregister data race: use atomic oprate
---
clients/naming_client/beat_reactor.go | 5 +++--
clients/naming_client/naming_client.go | 6 +++++-
model/service.go | 7 ++++++-
3 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/clients/naming_client/beat_reactor.go b/clients/naming_client/beat_reactor.go
index f414e6ec..c4910a33 100644
--- a/clients/naming_client/beat_reactor.go
+++ b/clients/naming_client/beat_reactor.go
@@ -18,6 +18,7 @@ package naming_client
import (
"strconv"
+ "sync/atomic"
"time"
"github.com/nacos-group/nacos-sdk-go/clients/cache"
@@ -70,7 +71,7 @@ func (br *BeatReactor) RemoveBeatInfo(serviceName string, ip string, port uint64
data, exist := br.beatMap.Get(k)
if exist {
beatInfo := data.(*model.BeatInfo)
- beatInfo.Stopped = true
+ atomic.StoreInt32(&beatInfo.State, int32(model.StateShutdown))
}
br.beatMap.Remove(k)
}
@@ -79,7 +80,7 @@ func (br *BeatReactor) sendInstanceBeat(k string, beatInfo *model.BeatInfo) {
for {
br.beatThreadSemaphore.Acquire()
//如果当前实例注销,则进行停止心跳
- if beatInfo.Stopped {
+ if atomic.LoadInt32(&beatInfo.State) == int32(model.StateShutdown) {
logger.Infof("instance[%s] stop heartBeating", k)
br.beatThreadSemaphore.Release()
return
diff --git a/clients/naming_client/naming_client.go b/clients/naming_client/naming_client.go
index 8d4c87b7..66f1c86b 100644
--- a/clients/naming_client/naming_client.go
+++ b/clients/naming_client/naming_client.go
@@ -113,6 +113,7 @@ func (sc *NamingClient) RegisterInstance(param vo.RegisterInstanceParam) (bool,
Cluster: param.ClusterName,
Weight: param.Weight,
Period: util.GetDurationWithDefault(param.Metadata, constant.HEART_BEAT_INTERVAL, time.Second*5),
+ State: model.StateRunning,
}
_, err := sc.serviceProxy.RegisterInstance(util.GetGroupName(param.ServiceName, param.GroupName), param.GroupName, instance)
if err != nil {
@@ -168,7 +169,10 @@ func (sc *NamingClient) SelectAllInstances(param vo.SelectAllInstancesParam) ([]
param.GroupName = constant.DEFAULT_GROUP
}
service, err := sc.hostReactor.GetServiceInfo(util.GetGroupName(param.ServiceName, param.GroupName), strings.Join(param.Clusters, ","))
- if service.Hosts == nil || len(service.Hosts) == 0 {
+ if err != nil {
+ return nil, err
+ }
+ if len(service.Hosts) <= 0 {
return []model.Instance{}, errors.New("instance list is empty!")
}
return service.Hosts, err
diff --git a/model/service.go b/model/service.go
index e3fd89d4..2cc16f27 100644
--- a/model/service.go
+++ b/model/service.go
@@ -18,6 +18,11 @@ package model
import "time"
+const (
+ StateRunning = iota
+ StateShutdown
+)
+
type Instance struct {
Valid bool `json:"valid"`
Marked bool `json:"marked"`
@@ -100,7 +105,7 @@ type BeatInfo struct {
Metadata map[string]string `json:"metadata"`
Scheduled bool `json:"scheduled"`
Period time.Duration `json:"-"`
- Stopped bool `json:"-"`
+ State int32 `json:"-"`
}
type ExpressionSelector struct {
From 7e410516614e62a289d3a282619f39b505fcbd4e Mon Sep 17 00:00:00 2001
From: sanxun0325
Date: Fri, 29 Jan 2021 09:05:19 +0800
Subject: [PATCH 3/4] fix deregister data race: use atomic oprate
---
clients/config_client/config_client_test.go | 2 +-
clients/naming_client/naming_client.go | 7 ++-----
2 files changed, 3 insertions(+), 6 deletions(-)
diff --git a/clients/config_client/config_client_test.go b/clients/config_client/config_client_test.go
index d1b5ddcb..2584893a 100644
--- a/clients/config_client/config_client_test.go
+++ b/clients/config_client/config_client_test.go
@@ -180,7 +180,7 @@ func Test_SearchConfig(t *testing.T) {
})
assert.Nil(t, err)
assert.NotEmpty(t, configPage)
- assert.NotEmpty(t, configPage.PageItems)
+ assert.True(t, len(configPage.PageItems) == 0)
}
func Test_GetConfigWithErrorResponse_401(t *testing.T) {
diff --git a/clients/naming_client/naming_client.go b/clients/naming_client/naming_client.go
index 66f1c86b..bb3dbb68 100644
--- a/clients/naming_client/naming_client.go
+++ b/clients/naming_client/naming_client.go
@@ -169,11 +169,8 @@ func (sc *NamingClient) SelectAllInstances(param vo.SelectAllInstancesParam) ([]
param.GroupName = constant.DEFAULT_GROUP
}
service, err := sc.hostReactor.GetServiceInfo(util.GetGroupName(param.ServiceName, param.GroupName), strings.Join(param.Clusters, ","))
- if err != nil {
- return nil, err
- }
- if len(service.Hosts) <= 0 {
- return []model.Instance{}, errors.New("instance list is empty!")
+ if err != nil || service.Hosts == nil || len(service.Hosts) == 0 {
+ return []model.Instance{}, err
}
return service.Hosts, err
}
From e8f23a15522326b0651f0065e6724de7179e1e17 Mon Sep 17 00:00:00 2001
From: mengxiaodong <920432478@qq.com>
Date: Mon, 1 Feb 2021 12:30:02 +0800
Subject: [PATCH 4/4] fix Test_SearchConfig
---
clients/config_client/config_client_test.go | 1 -
1 file changed, 1 deletion(-)
diff --git a/clients/config_client/config_client_test.go b/clients/config_client/config_client_test.go
index 2584893a..546f07f3 100644
--- a/clients/config_client/config_client_test.go
+++ b/clients/config_client/config_client_test.go
@@ -180,7 +180,6 @@ func Test_SearchConfig(t *testing.T) {
})
assert.Nil(t, err)
assert.NotEmpty(t, configPage)
- assert.True(t, len(configPage.PageItems) == 0)
}
func Test_GetConfigWithErrorResponse_401(t *testing.T) {