justxuewei commented on code in PR #2191:
URL: https://github.com/apache/dubbo-go/pull/2191#discussion_r1092733919


##########
registry/event/service_mapping_change_listener_impl.go:
##########
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package event
+
+import (
+       "sync"
+)
+
+import (
+       gxset "github.com/dubbogo/gost/container/set"
+       "github.com/dubbogo/gost/gof/observer"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common"
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
+       "dubbo.apache.org/dubbo-go/v3/common/extension"
+       "dubbo.apache.org/dubbo-go/v3/registry"
+)
+
+type ServiceMappingChangedListenerImpl struct {
+       oldServiceNames *gxset.HashSet
+       listener        registry.NotifyListener
+       registryUrl     *common.URL
+       serviceUrl      *common.URL
+       mappingCache    *sync.Map
+       stop            int
+}
+
+const (
+       ServiceMappingListenerStart = iota
+       ServiceMappingListenerStop
+)
+
+func NewMappingListener(registryUrl *common.URL, serviceUrl *common.URL, 
oldServiceNames *gxset.HashSet, listener registry.NotifyListener) 
*ServiceMappingChangedListenerImpl {
+       return &ServiceMappingChangedListenerImpl{
+               listener:        listener,
+               oldServiceNames: oldServiceNames,
+               registryUrl:     registryUrl,
+               serviceUrl:      serviceUrl,
+               stop:            ServiceMappingListenerStart,
+               mappingCache:    &sync.Map{},
+       }
+}
+
+// OnEvent on ServiceMappingChangedEvent the service mapping change event
+func (lstn *ServiceMappingChangedListenerImpl) OnEvent(e observer.Event) error 
{
+       var (
+               err error
+               reg registry.Registry
+       )
+       if lstn.stop == ServiceMappingListenerStop {
+               return nil
+       }
+       sm, ok := e.(*registry.ServiceMappingChangeEvent)
+       if !ok {
+               return nil
+       }
+       newServiceNames := sm.GetServiceNames()
+       oldServiceNames := lstn.oldServiceNames
+
+       // serviceMapping is orderly
+       if newServiceNames.Empty() || oldServiceNames.String() == 
newServiceNames.String() {
+               return nil
+       }
+
+       if newServiceNames.Size() > 0 && oldServiceNames.Empty() {
+               if reg, err = 
extension.GetRegistry(constant.ServiceRegistryProtocol, lstn.registryUrl); err 
!= nil {
+                       return err
+               }
+               reg.SubscribeURL(lstn.serviceUrl, lstn.listener, 
newServiceNames)

Review Comment:
   As previously discussed in 
https://github.com/apache/dubbo-go/pull/2191#discussion_r1092732833, do a type 
conversion explicitly, like:
   
   ```go
   if sdreg, ok := reg.(*serviceDiscoveryRegistry); ok {
       // call sdreg.SubscribeURL() here
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to