ztelur commented on a change in pull request #354: URL: https://github.com/apache/dubbo-go-pixiu/pull/354#discussion_r791296473
########## File path: pkg/remote/zookeeper/client.go ########## @@ -0,0 +1,100 @@ +/* + * 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 ( + gxzookeeper "github.com/dubbogo/gost/database/kv/zk" + perrors "github.com/pkg/errors" +) + +import ( + "github.com/apache/dubbo-go-pixiu/pkg/logger" + "github.com/apache/dubbo-go-pixiu/pkg/model" +) + +func ValidateZookeeperClient(container ZkClientFacade, zkName string) error { + lock := container.ZkClientLock() + config := container.GetConfig() + + lock.Lock() + defer lock.Unlock() + + if container.ZkClient() == nil { + newClient, cltErr := NewZkClient(zkName, config) + if cltErr != nil { + return perrors.WithMessagef(cltErr, "create zookeeper client (address:%+v)", config.Address) + } + container.SetZkClient(newClient) + } + return nil +} + +func NewZkClient(zkName string, config *model.RemoteConfig) (*gxzookeeper.ZookeeperClient, error) { Review comment: is there a zk client in pixiu or gost for reuse ? ########## File path: pkg/adapter/springcloud/cloud.go ########## @@ -18,6 +18,8 @@ package springcloud import ( + "github.com/apache/dubbo-go-pixiu/pkg/adapter/springcloud/servicediscovery/zookeeper" Review comment: use `imports-formatter -path . -module github.com/apache/dubbo-go-pixiu -bl false` to split import block ########## 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 { + tickerTTL := defaultTTL + ticker := time.NewTicker(tickerTTL) + defer ticker.Stop() + z.handleEvent(children) + for { + select { + case <-ticker.C: + z.handleEvent(children) + case zkEvent := <-e: + logger.Debugf("get a zookeeper e{type:%s, server:%s, path:%s, state:%d-%s, err:%s}", + zkEvent.Type.String(), zkEvent.Server, zkEvent.Path, zkEvent.State, gzk.StateToString(zkEvent.State), zkEvent.Err) + if zkEvent.Type != zk.EventNodeChildrenChanged { + return true + } + z.handleEvent(children) + return true + case <-z.exit: + logger.Warnf("listen(path{%s}) goroutine exit now...", z.servicesPath) + return false + } + } +} + +func (z *zkAppListener) handleEvent(children []string) { + fetchChildren, err := z.ds.getClient().GetChildren(z.servicesPath) + if err != nil { + logger.Warnf("Error when retrieving newChildren in path: %s, Error:%s", z.servicesPath, err.Error()) + } + + discovery := z.ds + + del := func() { + keys := Keys(discovery.getServiceMap()) + diff := Diff(keys, fetchChildren) + if diff != nil { + logger.Debugf("Del the service %s", diff) + for _, sn := range diff { + for _, instance := range discovery.getServiceMap()[sn] { + discovery.delServiceInstance(instance) + } + } + } + } + del() Review comment: maybe use below format is better ` func() { .... }() ` -- 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]
