Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #655 Repeated subscriptions result in multiple callbacks #674

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,32 +267,35 @@ services, err := namingClient.GetService(vo.GetServiceParam{
* Listen service change event:Subscribe

```go

// Subscribe key = serviceName+groupName+cluster
// Note: We call add multiple SubscribeCallback with the same key.
err := namingClient.Subscribe(vo.SubscribeParam{
ServiceName: "demo.go",
GroupName: "group-a", // default value is DEFAULT_GROUP
Clusters: []string{"cluster-a"}, // default value is DEFAULT
SubscribeCallback: func (services []model.Instance, err error) {
log.Printf("\n\n callback return services:%s \n\n", utils.ToJsonString(services))
},
})
// Note: We call add multiple SubscribeCallback with the same key.
callback := func(services []model.Instance, err error) {
log.Printf("\n\n callback return services:%s \n\n", utils.ToJsonString(services))
}

err := namingClient.Subscribe(vo.SubscribeParam{
ServiceName: "demo.go",
GroupName: "group-a", // default value is DEFAULT_GROUP
Clusters: []string{"cluster-a"}, // default value is DEFAULT
SubscribeCallback: callback,
})

```

* Cancel listen of service change event:Unsubscribe

```go

callback := func(services []model.Instance, err error) {
log.Printf("\n\n callback return services:%s \n\n", utils.ToJsonString(services))
}

err := namingClient.Unsubscribe(vo.SubscribeParam{
ServiceName: "demo.go",
GroupName: "group-a", // default value is DEFAULT_GROUP
Clusters: []string{"cluster-a"}, // default value is DEFAULT
SubscribeCallback: func (services []model.Instance, err error) {
log.Printf("\n\n callback return services:%s \n\n", utils.ToJsonString(services))
},
})
ServiceName: "demo.go",
GroupName: "group-a", // default value is DEFAULT_GROUP
Clusters: []string{"cluster-a"}, // default value is DEFAULT
SubscribeCallback: callback,
})

```

Expand Down
17 changes: 11 additions & 6 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,16 @@ instance, err := namingClient.SelectOneHealthyInstance(vo.SelectOneHealthInstanc

// Subscribe key=serviceName+groupName+cluster
// 注意:我们可以在相同的key添加多个SubscribeCallback.

callback := func(services []model.Instance, err error) {
log.Printf("\n\n callback return services:%s \n\n", utils.ToJsonString(services))
}

err := namingClient.Subscribe(vo.SubscribeParam{
ServiceName: "demo.go",
GroupName: "group-a", // 默认值DEFAULT_GROUP
Clusters: []string{"cluster-a"}, // 默认值DEFAULT
SubscribeCallback: func(services []model.Instance, err error) {
log.Printf("\n\n callback return services:%s \n\n", utils.ToJsonString(services))
},
SubscribeCallback: callback,
})

```
Expand All @@ -273,13 +276,15 @@ err := namingClient.Subscribe(vo.SubscribeParam{

```go

callback := func(services []model.Instance, err error) {
log.Printf("\n\n callback return services:%s \n\n", utils.ToJsonString(services))
}

err := namingClient.Unsubscribe(vo.SubscribeParam{
ServiceName: "demo.go",
GroupName: "group-a", // 默认值DEFAULT_GROUP
Clusters: []string{"cluster-a"}, // 默认值DEFAULT
SubscribeCallback: func(services []model.Instance, err error) {
log.Printf("\n\n callback return services:%s \n\n", utils.ToJsonString(services))
},
SubscribeCallback: callback,
})

```
Expand Down
4 changes: 2 additions & 2 deletions clients/naming_client/naming_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func (sc *NamingClient) Subscribe(param *vo.SubscribeParam) error {
param.GroupName = constant.DEFAULT_GROUP
}
clusters := strings.Join(param.Clusters, ",")
sc.serviceInfoHolder.RegisterCallback(util.GetGroupName(param.ServiceName, param.GroupName), clusters, &param.SubscribeCallback)
sc.serviceInfoHolder.RegisterCallback(util.GetGroupName(param.ServiceName, param.GroupName), clusters, param.SubscribeCallback)
_, err := sc.serviceProxy.Subscribe(param.ServiceName, param.GroupName, clusters)
return err
}
Expand All @@ -334,7 +334,7 @@ func (sc *NamingClient) Subscribe(param *vo.SubscribeParam) error {
func (sc *NamingClient) Unsubscribe(param *vo.SubscribeParam) (err error) {
clusters := strings.Join(param.Clusters, ",")
serviceFullName := util.GetGroupName(param.ServiceName, param.GroupName)
sc.serviceInfoHolder.DeregisterCallback(serviceFullName, clusters, &param.SubscribeCallback)
sc.serviceInfoHolder.DeregisterCallback(serviceFullName, clusters, param.SubscribeCallback)
if sc.serviceInfoHolder.IsSubscribed(serviceFullName, clusters) {
err = sc.serviceProxy.Unsubscribe(param.ServiceName, param.GroupName, clusters)
}
Expand Down
8 changes: 4 additions & 4 deletions vo/service_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ type GetAllServiceInfoParam struct {
}

type SubscribeParam struct {
ServiceName string `param:"serviceName"` //required
Clusters []string `param:"clusters"` //optional
GroupName string `param:"groupName"` //optional,default:DEFAULT_GROUP
SubscribeCallback func(services []model.Instance, err error) //required
ServiceName string `param:"serviceName"` //required
Clusters []string `param:"clusters"` //optional
GroupName string `param:"groupName"` //optional,default:DEFAULT_GROUP
SubscribeCallback *func(services []model.Instance, err error) //required
}

type SelectAllInstancesParam struct {
Expand Down