func()

in pkg/adapter/springcloud/servicediscovery/zookeeper/application_listener.go [125:171]


func (z *zkAppListener) handleEvent(children []string) {

	fetchChildren, err := z.ds.getClient().GetChildren(z.servicesPath)

	if err != nil {
		// todo refactor gost zk, make it return the definite err
		if strings.Contains(err.Error(), "none children") {
			logger.Debugf("%s get nodes from zookeeper fail: %s", common.ZKLogDiscovery, err.Error())
		} else {
			logger.Warnf("Error when retrieving newChildren in path: %s, Error:%s", z.servicesPath, err.Error())
		}
	}

	discovery := z.ds
	serviceMap := discovery.getServiceMap()

	// del services
	for sn := range serviceMap {

		if !contains(fetchChildren, sn) {

			// service zk event listener
			serviceNodePath := strings.Join([]string{z.servicesPath, sn}, constant.PathSlash)
			z.svcListeners.RemoveListener(serviceNodePath)

			// service cluster
			for _, instance := range serviceMap[sn] {
				_, _ = discovery.delServiceInstance(instance)
			}
		}
	}

	for _, serviceName := range fetchChildren {
		serviceNodePath := strings.Join([]string{z.servicesPath, serviceName}, constant.PathSlash)
		instances := serviceMap[serviceName]
		if len(instances) == 0 {
			z.svcListeners.RemoveListener(serviceNodePath)
		}
		if z.svcListeners.GetListener(serviceNodePath) != nil {
			continue
		}
		l := newApplicationServiceListener(serviceNodePath, serviceName, discovery)
		l.wg.Add(1)
		go l.WatchAndHandle()
		z.svcListeners.SetListener(serviceNodePath, l)
	}
}