Skip to content

Commit

Permalink
Merge pull request #181 from undom/fix-deregister-data-race
Browse files Browse the repository at this point in the history
Fix deregister data race(#180)
  • Loading branch information
lzp0412 authored Feb 1, 2021
2 parents 4d41ccd + e8f23a1 commit 14f269e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
16 changes: 8 additions & 8 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ constant.ServerConfig{
}
```

<b>Note:我们可以配置多个ServerConfig,客户端会对这些服务端做轮训请求</b>
<b>Note:我们可以配置多个ServerConfig,客户端会对这些服务端做轮询请求</b>

### Create client

```go
//创建clientConfig
// 创建clientConfig
clientConfig := constant.ClientConfig{
NamespaceId: "e525eafa-f7d7-4029-83d9-008937f9d468", // 如果需要支持多namespace,我们可以场景多个client,它们有不同的NamespaceId
TimeoutMs: 5000,
Expand All @@ -72,7 +72,7 @@ clientConfig := constant.ClientConfig{
LogLevel: "debug",
}

//创建clientConfig的另一种方式
// 创建clientConfig的另一种方式
clientConfig := *constant.NewClientConfig(
constant.WithNamespaceId("e525eafa-f7d7-4029-83d9-008937f9d468"),
constant.WithTimeoutMs(5000),
Expand Down Expand Up @@ -100,7 +100,7 @@ serverConfigs := []constant.ServerConfig{
},
}

//创建serverConfig的另一种方式
// 创建serverConfig的另一种方式
serverConfigs := []constant.ServerConfig{
*constant.NewServerConfig(
"console1.nacos.io",
Expand Down Expand Up @@ -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
})

```
Expand All @@ -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
})

```
Expand Down Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion clients/config_client/config_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ func Test_SearchConfig(t *testing.T) {
})
assert.Nil(t, err)
assert.NotEmpty(t, configPage)
assert.NotEmpty(t, configPage.PageItems)
}

func Test_GetConfigWithErrorResponse_401(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions clients/naming_client/beat_reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package naming_client

import (
"strconv"
"sync/atomic"
"time"

"github.com/nacos-group/nacos-sdk-go/clients/cache"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions clients/naming_client/naming_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -168,8 +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 service.Hosts == nil || 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
}
Expand Down
7 changes: 6 additions & 1 deletion model/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ package model

import "time"

const (
StateRunning = iota
StateShutdown
)

type Instance struct {
Valid bool `json:"valid"`
Marked bool `json:"marked"`
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 14f269e

Please sign in to comment.