func()

in pkg/adapter/dubboregistry/registry/zookeeper/service_listener.go [126:177]


func (zkl *serviceListener) handleEvent() {
	// get all children of provider, such as /dubbo-app/org.apache.dubbo.samples.api.DemoService/providers
	children, err := zkl.client.GetChildren(zkl.path)
	if err != nil {
		// disable the service all methods
		bkConf, _, _, _ := registry.ParseDubboString(zkl.url.String())
		apiPattern := registry.GetAPIPattern(bkConf)
		// delete all config of an interface, such as /dubbo-app/org.apache.dubbo.samples.api.DemoService
		if err := zkl.adapterListener.OnDeleteRouter(config.Resource{Path: apiPattern}); err != nil {
			logger.Errorf("Error={%s} when try to remove API by path: %s", err.Error(), apiPattern)
		}
		return
	}
	zkl.url, err = common.NewURL(children[0])
	if err != nil {
		logger.Warnf("Parse service path failed: %s", children[0])
	}
	bkConfig, methods, location, err := registry.ParseDubboString(children[0])
	if err != nil {
		logger.Warnf("Parse dubbo interface provider %s failed; due to \n %s", children[0], err.Error())
		return
	}
	if len(bkConfig.ApplicationName) == 0 || len(bkConfig.Interface) == 0 {
		return
	}

	mappingParams := []config.MappingParam{
		{
			Name:  "requestBody.values",
			MapTo: "opt.values",
		},
		{
			Name:  "requestBody.types",
			MapTo: "opt.types",
		},
	}
	apiPattern := registry.GetAPIPattern(bkConfig)
	zkl.mutex.Lock()
	defer zkl.mutex.Unlock()
	for i := range methods {
		api := registry.CreateAPIConfig(apiPattern, location, bkConfig, methods[i], mappingParams)
		key := api.URLPattern + ":" + string(api.Method.HTTPVerb)
		if _, ok := zkl.registryMethod[key]; ok {
			return
		}
		if err := zkl.adapterListener.OnAddAPI(api); err != nil {
			logger.Errorf("Error={%s} happens when try to add api %s", err.Error(), api.Path)
		} else {
			zkl.registryMethod[key] = &api.Method
		}
	}
}