Skip to content

Commit 50ff4f1

Browse files
authored
fix subscribe (#528)
1 parent 0b9b305 commit 50ff4f1

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

Diff for: clients/naming_client/naming_client.go

+18-19
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func NewNamingClient(nc nacos_client.INacosClient) (NamingClient, error) {
9393
return naming, nil
9494
}
9595

96-
//RegisterInstance register instance
96+
// RegisterInstance register instance
9797
func (sc *NamingClient) RegisterInstance(param vo.RegisterInstanceParam) (bool, error) {
9898
if param.ServiceName == "" {
9999
return false, errors.New("serviceName cannot be empty!")
@@ -135,7 +135,7 @@ func (sc *NamingClient) RegisterInstance(param vo.RegisterInstanceParam) (bool,
135135

136136
}
137137

138-
//DeregisterInstance deregister instance
138+
// DeregisterInstance deregister instance
139139
func (sc *NamingClient) DeregisterInstance(param vo.DeregisterInstanceParam) (bool, error) {
140140
if len(param.GroupName) == 0 {
141141
param.GroupName = constant.DEFAULT_GROUP
@@ -149,7 +149,7 @@ func (sc *NamingClient) DeregisterInstance(param vo.DeregisterInstanceParam) (bo
149149
return true, nil
150150
}
151151

152-
//UpdateInstance update information for exist instance.
152+
// UpdateInstance update information for exist instance.
153153
func (sc *NamingClient) UpdateInstance(param vo.UpdateInstanceParam) (bool, error) {
154154
if len(param.GroupName) == 0 {
155155
param.GroupName = constant.DEFAULT_GROUP
@@ -183,7 +183,7 @@ func (sc *NamingClient) UpdateInstance(param vo.UpdateInstanceParam) (bool, erro
183183
return true, nil
184184
}
185185

186-
//GetService get service info
186+
// GetService get service info
187187
func (sc *NamingClient) GetService(param vo.GetServiceParam) (model.Service, error) {
188188
if len(param.GroupName) == 0 {
189189
param.GroupName = constant.DEFAULT_GROUP
@@ -192,7 +192,7 @@ func (sc *NamingClient) GetService(param vo.GetServiceParam) (model.Service, err
192192
return service, err
193193
}
194194

195-
//GetAllServicesInfo get all services info
195+
// GetAllServicesInfo get all services info
196196
func (sc *NamingClient) GetAllServicesInfo(param vo.GetAllServiceInfoParam) (model.ServiceList, error) {
197197
if len(param.GroupName) == 0 {
198198
param.GroupName = constant.DEFAULT_GROUP
@@ -214,7 +214,7 @@ func (sc *NamingClient) GetAllServicesInfo(param vo.GetAllServiceInfoParam) (mod
214214
return services, nil
215215
}
216216

217-
//SelectAllInstances select all instances
217+
// SelectAllInstances select all instances
218218
func (sc *NamingClient) SelectAllInstances(param vo.SelectAllInstancesParam) ([]model.Instance, error) {
219219
if len(param.GroupName) == 0 {
220220
param.GroupName = constant.DEFAULT_GROUP
@@ -226,7 +226,7 @@ func (sc *NamingClient) SelectAllInstances(param vo.SelectAllInstancesParam) ([]
226226
return service.Hosts, err
227227
}
228228

229-
//SelectInstances select instances
229+
// SelectInstances select instances
230230
func (sc *NamingClient) SelectInstances(param vo.SelectInstancesParam) ([]model.Instance, error) {
231231
if len(param.GroupName) == 0 {
232232
param.GroupName = constant.DEFAULT_GROUP
@@ -252,7 +252,7 @@ func (sc *NamingClient) selectInstances(service model.Service, healthy bool) ([]
252252
return result, nil
253253
}
254254

255-
//SelectOneHealthyInstance select one healthy instance
255+
// SelectOneHealthyInstance select one healthy instance
256256
func (sc *NamingClient) SelectOneHealthyInstance(param vo.SelectOneHealthInstanceParam) (*model.Instance, error) {
257257
if len(param.GroupName) == 0 {
258258
param.GroupName = constant.DEFAULT_GROUP
@@ -284,28 +284,27 @@ func (sc *NamingClient) selectOneHealthyInstances(service model.Service) (*model
284284
return nil, errors.New("healthy instance list is empty!")
285285
}
286286

287-
chooser := newChooser(result)
288-
instance := chooser.pick()
287+
instance := newChooser(result).pick()
289288
return &instance, nil
290289
}
291290

292-
type instance []model.Instance
291+
type instances []model.Instance
293292

294-
func (a instance) Len() int {
293+
func (a instances) Len() int {
295294
return len(a)
296295
}
297296

298-
func (a instance) Swap(i, j int) {
297+
func (a instances) Swap(i, j int) {
299298
a[i], a[j] = a[j], a[i]
300299
}
301300

302-
func (a instance) Less(i, j int) bool {
301+
func (a instances) Less(i, j int) bool {
303302
return a[i].Weight < a[j].Weight
304303
}
305304

306305
// NewChooser initializes a new Chooser for picking from the provided Choices.
307-
func newChooser(instances []model.Instance) Chooser {
308-
sort.Sort(instance(instances))
306+
func newChooser(instances instances) Chooser {
307+
sort.Sort(instances)
309308
totals := make([]int, len(instances))
310309
runningTotal := 0
311310
for i, c := range instances {
@@ -321,7 +320,7 @@ func (chs Chooser) pick() model.Instance {
321320
return chs.data[i]
322321
}
323322

324-
//Subscribe subscibe service
323+
// Subscribe subscribe service
325324
func (sc *NamingClient) Subscribe(param *vo.SubscribeParam) error {
326325
if len(param.GroupName) == 0 {
327326
param.GroupName = constant.DEFAULT_GROUP
@@ -337,13 +336,13 @@ func (sc *NamingClient) Subscribe(param *vo.SubscribeParam) error {
337336
if err != nil {
338337
return err
339338
}
340-
if !sc.hostReactor.serviceProxy.clientConfig.NotLoadCacheAtStart {
339+
if sc.hostReactor.serviceProxy.clientConfig.NotLoadCacheAtStart {
341340
sc.subCallback.ServiceChanged(&svc)
342341
}
343342
return nil
344343
}
345344

346-
//Unsubscribe unsubscribe service
345+
// Unsubscribe unsubscribe service
347346
func (sc *NamingClient) Unsubscribe(param *vo.SubscribeParam) error {
348347
sc.subCallback.RemoveCallbackFuncs(util.GetGroupName(param.ServiceName, param.GroupName), strings.Join(param.Clusters, ","), &param.SubscribeCallback)
349348
return nil

0 commit comments

Comments
 (0)