robocanic commented on code in PR #1371: URL: https://github.com/apache/dubbo-admin/pull/1371#discussion_r2635188964
########## pkg/discovery/zk/zkwatcher/watcher.go: ########## @@ -0,0 +1,251 @@ +/* + * 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 zkwatcher + +import ( + "sync" + "time" + + "github.com/dubbogo/go-zookeeper/zk" + set "github.com/duke-git/lancet/v2/datastructure/set" + + "github.com/apache/dubbo-admin/pkg/core/logger" +) + +type EventType string + +const ( + NodeCreated EventType = "NodeCreated" + NodeDeleted EventType = "NodeDeleted" + NodeChanged EventType = "NodeChanged" +) + +// ZookeeperEvent represents a zookeeper event +type ZookeeperEvent struct { + Type EventType + // node path + Path string + // node data + Data string + // whether it is a leaf node + leafNode bool +} + +// RecursiveWatcher recursive watcher +type RecursiveWatcher struct { + conn *zk.Conn + basePath string + eventChan chan ZookeeperEvent + stopChan chan struct{} + wg sync.WaitGroup + mu sync.Mutex +} + +// NewRecursiveWatcher create a new recursive watcher +func NewRecursiveWatcher(conn *zk.Conn, basePath string) *RecursiveWatcher { + return &RecursiveWatcher{ + conn: conn, + basePath: basePath, + eventChan: make(chan ZookeeperEvent, 1000), + stopChan: make(chan struct{}), + } +} + +// Start begin watching +func (rw *RecursiveWatcher) Start() error { + logger.Infof("Start watching path: %s", rw.basePath) + // Recursively watch the initial path + return rw.watchPathRecursively(rw.basePath) +} + +// Stop watching +func (rw *RecursiveWatcher) Stop() { + close(rw.stopChan) + rw.wg.Wait() + rw.conn.Close() + close(rw.eventChan) +} + +// EventChan return event channel +func (rw *RecursiveWatcher) EventChan() <-chan ZookeeperEvent { + return rw.eventChan +} + +// watchPathRecursively watch path recursively +func (rw *RecursiveWatcher) watchPathRecursively(path string) error { + // Watch data changes for the current path + go rw.watchDataChanges(path) + + // Get children of current path and watch + children, _, err := rw.conn.Children(path) + if err != nil { + logger.Errorf("Failed to get children of path: %s, cause: %v", path, err) + return err + } + // Watch children list changes + go rw.watchChildrenChanges(path) + + // Recursively watch child nodes + for _, child := range children { + childPath := path + "/" + child + if err := rw.watchPathRecursively(childPath); err != nil { + logger.Errorf("Failed to watch subpath %s: %v", childPath, err) + } + } + return nil +} + +// watchDataChanges watch node data changes +func (rw *RecursiveWatcher) watchDataChanges(path string) { + logger.Debugf("Watching data changes for path: %s", path) + for { + + select { + case <-rw.stopChan: + return + default: + } + + // Get data and set up watch + data, stat, watcher, err := rw.conn.GetW(path) + if err != nil { + logger.Errorf("Failed to watch data changes for path %s, cause: %v", path, err) + time.Sleep(time.Second) + continue + } + rw.eventChan <- ZookeeperEvent{ + Type: NodeCreated, + Path: path, + Data: string(data), + leafNode: stat.NumChildren == 0, + } + + select { + case event := <-watcher.EvtCh: + if event.Type == zk.EventNodeDataChanged { + logger.Debugf("node data changed: %s", path) + rw.eventChan <- ZookeeperEvent{ + Type: NodeChanged, + Path: path, + Data: string(data), + leafNode: stat.NumChildren == 0, + } 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]
