You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2021/04/18 11:12:10 UTC
[GitHub] [dubbo-go] Patrick0308 commented on a change in pull request #1161: Imp: remote service discovery
Patrick0308 commented on a change in pull request #1161:
URL: https://github.com/apache/dubbo-go/pull/1161#discussion_r615382812
##########
File path: registry/event/event_listener.go
##########
@@ -0,0 +1,193 @@
+/*
+ * 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 (
+ "github.com/apache/dubbo-go/common"
+ "github.com/apache/dubbo-go/common/constant"
+ "github.com/apache/dubbo-go/common/extension"
+ "github.com/apache/dubbo-go/common/logger"
+ "github.com/apache/dubbo-go/metadata/service/inmemory"
+ "github.com/apache/dubbo-go/registry"
+ "github.com/apache/dubbo-go/remoting"
+ gxset "github.com/dubbogo/gost/container/set"
+ "reflect"
+)
+
+import (
+ "github.com/apache/dubbo-go/common/observer"
+)
+
+// The Service Discovery Changed Event Listener
+type ServiceInstancesChangedListener struct {
+ registry.ServiceInstancesChangedListenerBase
+ ServiceNames *gxset.HashSet
+ listeners map[string]registry.NotifyListener
+ serviceUrls map[string][]*common.URL
+ revisionToMetadata map[string]*common.MetadataInfo
+ allInstances map[string][]registry.ServiceInstance
+}
+
+func NewServiceInstancesChangedListener(services *gxset.HashSet) *ServiceInstancesChangedListener {
+ return &ServiceInstancesChangedListener{
+ ServiceNames: services,
+ listeners: make(map[string]registry.NotifyListener),
+ serviceUrls: make(map[string][]*common.URL),
+ revisionToMetadata: make(map[string]*common.MetadataInfo),
+ allInstances: make(map[string][]registry.ServiceInstance),
+ }
+}
+
+// OnEvent on ServiceInstancesChangedEvent the service instances change event
+func (lstn *ServiceInstancesChangedListener) OnEvent(e observer.Event) error {
+ ce, ok := e.(*registry.ServiceInstancesChangedEvent)
+ if !ok {
+ return nil
+ }
+ var err error
+ lstn.allInstances[ce.ServiceName] = ce.Instances
+ revisionToInstances := make(map[string][]registry.ServiceInstance)
+ newRevisionToMetadata := make(map[string]*common.MetadataInfo)
+ localServiceToRevisions := make(map[*common.ServiceInfo]*gxset.HashSet)
+ protocolRevisionsToUrls := make(map[string]map[*gxset.HashSet][]*common.URL)
+ newServiceURLs := make(map[string][]*common.URL)
+
+ for _, instances := range lstn.allInstances {
+ for _, instance := range instances {
+ revision := instance.GetMetadata()[constant.EXPORTED_SERVICES_REVISION_PROPERTY_NAME]
+ if "0" == revision {
+ logger.Infof("Find instance without valid service metadata: %s", instance.GetHost())
+ continue
+ }
+ subInstances := revisionToInstances[revision]
+ if subInstances == nil {
+ subInstances = make([]registry.ServiceInstance, 8)
+ }
+ revisionToInstances[revision] = append(subInstances, instance)
+ metadataInfo := lstn.revisionToMetadata[revision]
Review comment:
I think don't need judge ok
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org