func()

in pkg/adapter/springcloud/servicediscovery/nacos/nacos.go [104:154]


func (n *nacosServiceDiscovery) Callback(services []model2.SubscribeService, err error) {

	addInstances := make([]servicediscovery.ServiceInstance, 0, len(services))
	delInstances := make([]servicediscovery.ServiceInstance, 0, len(services))
	updateInstances := make([]servicediscovery.ServiceInstance, 0, len(services))
	newInstanceMap := make(map[string]servicediscovery.ServiceInstance, len(services))

	n.cacheLock.Lock()

	for i := range services {
		service := services[i]
		if !service.Enable {
			// instance is not available,so ignore it
			continue
		}

		instance := fromSubscribeServiceToServiceInstance(service)
		key := instance.GetUniqKey()
		newInstanceMap[instance.GetUniqKey()] = instance
		if old, ok := n.instanceMap[key]; !ok {
			// instance does not exist in cache, add it to cache
			addInstances = append(addInstances, instance)
		} else {
			// instance is not different from cache, update it to cache
			if !reflect.DeepEqual(old, instance) {
				updateInstances = append(updateInstances, instance)
			}
		}
	}

	for host, inst := range n.instanceMap {
		if _, ok := newInstanceMap[host]; !ok {
			// cache instance does not exist in new instance list, remove it from cache
			delInstances = append(delInstances, inst)
		}
	}

	n.instanceMap = newInstanceMap
	n.cacheLock.Unlock()

	for _, instance := range addInstances {
		n.listener.OnAddServiceInstance(&instance)
	}
	for _, instance := range delInstances {
		n.listener.OnDeleteServiceInstance(&instance)
	}

	for _, instance := range updateInstances {
		n.listener.OnUpdateServiceInstance(&instance)
	}
}