AlexStocks commented on a change in pull request #354:
URL: https://github.com/apache/dubbo-go-pixiu/pull/354#discussion_r791369215



##########
File path: 
pkg/adapter/springcloud/servicediscovery/zookeeper/application_listener.go
##########
@@ -0,0 +1,191 @@
+/*
+ * 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 zookeeper
+
+import (
+       "strings"
+       "sync"
+       "time"
+)
+
+import (
+       "github.com/dubbogo/go-zookeeper/zk"
+       gzk "github.com/dubbogo/gost/database/kv/zk"
+)
+
+import (
+       "github.com/apache/dubbo-go-pixiu/pkg/common/constant"
+       "github.com/apache/dubbo-go-pixiu/pkg/logger"
+       "github.com/apache/dubbo-go-pixiu/pkg/remote/zookeeper"
+)
+
+type zkAppListener struct {
+       servicesPath string
+       exit         chan struct{}
+       svcListeners *SvcListeners
+       wg           sync.WaitGroup
+
+       ds *zookeeperDiscovery
+}
+
+func newZkAppListener(ds *zookeeperDiscovery) zookeeper.Listener {
+       return &zkAppListener{
+               servicesPath: ds.basePath,
+               exit:         make(chan struct{}),
+               svcListeners: &SvcListeners{listeners: 
make(map[string]zookeeper.Listener), listenerLock: sync.Mutex{}},
+               ds:           ds,
+       }
+}
+
+func (z *zkAppListener) Close() {
+       for k, listener := range z.svcListeners.GetAllListener() {
+               z.svcListeners.RemoveListener(k)
+               listener.Close()
+       }
+       close(z.exit)
+       z.wg.Wait()
+}
+
+func (z *zkAppListener) WatchAndHandle() {
+       z.wg.Add(1)
+       go z.watch()
+}
+
+func (z *zkAppListener) watch() {
+       defer z.wg.Done()
+
+       var (
+               failTimes  int64 = 0
+               delayTimer       = time.NewTimer(ConnDelay * 
time.Duration(failTimes))
+       )
+       defer delayTimer.Stop()
+       for {
+               children, e, err := 
z.ds.getClient().GetChildrenW(z.servicesPath)
+               if err != nil {
+                       failTimes++
+                       logger.Infof("watching (path{%s}) = error{%v}", 
z.servicesPath, err)
+                       if err == gzk.ErrNilNode {
+                               logger.Errorf("watching (path{%s}) got 
errNilNode,so exit listen", z.servicesPath)
+                               return
+                       }
+                       if failTimes > MaxFailTimes {
+                               logger.Errorf("Error happens on (path{%s}) 
exceed max fail times: %s,so exit listen",
+                                       z.servicesPath, MaxFailTimes)
+                               return
+                       }
+                       delayTimer.Reset(ConnDelay * time.Duration(failTimes))
+                       <-delayTimer.C
+                       continue
+               }
+               failTimes = 0
+               if continueLoop := z.waitEventAndHandlePeriod(children, e); 
!continueLoop {
+                       return
+               }
+       }
+}
+
+func (z *zkAppListener) waitEventAndHandlePeriod(children []string, e <-chan 
zk.Event) bool {

Review comment:
       waitEvent 就可以了,后面这个 HandlePeriod 名字太难理解




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