robocanic commented on code in PR #1367:
URL: https://github.com/apache/dubbo-admin/pull/1367#discussion_r2616215952


##########
pkg/discovery/nacos2/listerwatcher/nacos_service.go:
##########
@@ -0,0 +1,273 @@
+/*
+ * 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 listerwatcher
+
+import (
+       "fmt"
+       "time"
+
+       "github.com/duke-git/lancet/v2/slice"
+       "github.com/go-co-op/gocron"
+       nacosnamingclient 
"github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client"
+       nacosmodel "github.com/nacos-group/nacos-sdk-go/v2/model"
+       nacosvo "github.com/nacos-group/nacos-sdk-go/v2/vo"
+       metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+       k8sruntime "k8s.io/apimachinery/pkg/runtime"
+       "k8s.io/apimachinery/pkg/watch"
+       "k8s.io/client-go/tools/cache"
+
+       meshproto "github.com/apache/dubbo-admin/api/mesh/v1alpha1"
+       "github.com/apache/dubbo-admin/pkg/common/bizerror"
+       discoverycfg "github.com/apache/dubbo-admin/pkg/config/discovery"
+       "github.com/apache/dubbo-admin/pkg/core/logger"
+       meshresource 
"github.com/apache/dubbo-admin/pkg/core/resource/apis/mesh/v1alpha1"
+       coremodel "github.com/apache/dubbo-admin/pkg/core/resource/model"
+)
+
+type NacosServiceListerWatcher struct {
+       cfg          *discoverycfg.Config
+       namingClient nacosnamingclient.INamingClient
+       // watchingServices is used to record the services that are currently 
being watched
+       // key -> nacos service name, value -> true if it is watched in current 
list turn
+       watchingServices map[string]bool
+       scheduler        *gocron.Scheduler
+       resultChan       chan watch.Event
+       stopWatch        bool
+}
+
+func NewNacosServiceListerWatcher(cfg *discoverycfg.Config, namingClient 
nacosnamingclient.INamingClient) *NacosServiceListerWatcher {
+       return &NacosServiceListerWatcher{
+               cfg:              cfg,
+               namingClient:     namingClient,
+               resultChan:       make(chan watch.Event),
+               watchingServices: make(map[string]bool),
+               scheduler:        gocron.NewScheduler(time.UTC),
+               stopWatch:        false,
+       }
+}
+
+func (lw *NacosServiceListerWatcher) List(_ metav1.ListOptions) 
(k8sruntime.Object, error) {
+       serviceNames, err := lw.fetchAllServiceNames()
+       if err != nil {
+               return nil, err
+       }
+       nacosServiceList := meshresource.NewNacosServiceResourceListWithItems()
+       resList := make([]*meshresource.NacosServiceResource, 0)
+       for _, serviceName := range serviceNames {
+               service, err := 
lw.namingClient.GetService(nacosvo.GetServiceParam{
+                       ServiceName: serviceName,
+               })
+               if err != nil {
+                       logger.Errorf("get instances of service %s failed in 
nacos %s, cause: %v", serviceName, lw.nacosAddress(), err)
+                       continue
+               }
+               resList = append(resList, 
lw.toNacosServiceResource(serviceName, service.Hosts))
+       }
+       nacosServiceList.Items = resList
+       return nacosServiceList, nil
+}
+
+func (lw *NacosServiceListerWatcher) Watch(_ metav1.ListOptions) 
(watch.Interface, error) {
+       _, err := 
lw.scheduler.Every(lw.cfg.Properties.ServiceWatchPeriod).Seconds().Do(func() {
+               if lw.stopWatch {
+                       logger.Debugf("stop watch all services of nacos %s", 
lw.mesh())
+                       lw.scheduler.Stop()
+                       return
+               }
+               startTime := time.Now()
+               err := lw.fetchAndProcessService()
+               if err != nil {
+                       logger.Errorf("fetch all service failed in nacos %s, 
cause: %v", lw.nacosAddress(), err)
+                       return
+               }
+               costs := time.Now().UnixMilli() - startTime.UnixMilli()
+               logger.Debugf("finish fetching and processing all services in 
nacos %s, costs %dms", lw.nacosAddress(), costs)
+       })
+       if err != nil {
+               return nil, bizerror.Wrap(err, bizerror.UnknownError,
+                       fmt.Sprintf("watch all services of nacos %s in a 
schedule occurs error", lw.mesh()))
+       }
+       lw.scheduler.StartAsync()
+       return lw, nil
+}
+
+func (lw *NacosServiceListerWatcher) fetchAllServiceNames() ([]string, error) {
+       serviceNameList := make([]string, 0)
+       var pageNum uint32 = 1
+       var pageSize uint32 = 100
+       // If the number of serviceInfos is less than the page size, it means 
that the last page has been reached
+       for {
+               serviceNames, err := lw.listPage(pageNum, pageSize)
+               if err != nil {
+                       return nil, err
+               }
+               serviceNameList = append(serviceNameList, serviceNames...)
+               if len(serviceNames) <= int(pageNum*pageSize) {

Review Comment:
   fixed



-- 
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