github.com/polarismesh/polaris@v1.17.8/apiserver/eurekaserver/vip.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package eurekaserver 19 20 import ( 21 "sort" 22 "strings" 23 ) 24 25 const ( 26 entityTypeVip = 0 27 entityTypeSVip = 1 28 ) 29 30 // VipCacheKey key for reference the vip cache 31 type VipCacheKey struct { 32 entityType int 33 targetVipAddress string 34 } 35 36 // BuildApplicationsForVip build applications with target vip 37 func BuildApplicationsForVip(key *VipCacheKey, appsCache *ApplicationsRespCache) *ApplicationsRespCache { 38 toReturn := newApplications() 39 applications := appsCache.AppsResp.Applications 40 hashBuilder := make(map[string]int) 41 var instCount int 42 for _, application := range applications.Application { 43 var appToAdd *Application 44 for _, instance := range application.Instance { 45 var vipAddress string 46 switch key.entityType { 47 case entityTypeVip: 48 vipAddress = instance.VipAddress 49 case entityTypeSVip: 50 vipAddress = instance.SecureVipAddress 51 default: 52 continue 53 } 54 if len(vipAddress) == 0 { 55 continue 56 } 57 vipAddresses := strings.Split(vipAddress, ",") 58 sort.Strings(vipAddresses) 59 searchIdx := sort.SearchStrings(vipAddresses, key.targetVipAddress) 60 found := searchIdx < len(vipAddresses) && vipAddresses[searchIdx] == key.targetVipAddress 61 if found { 62 if appToAdd == nil { 63 appToAdd = &Application{ 64 Name: application.Name, 65 InstanceMap: make(map[string]*InstanceInfo), 66 StatusCounts: make(map[string]int), 67 } 68 toReturn.Application = append(toReturn.Application, appToAdd) 69 } 70 appToAdd.Instance = append(appToAdd.Instance, instance) 71 instCount++ 72 appToAdd.StatusCounts[instance.Status] = appToAdd.StatusCounts[instance.Status] + 1 73 } 74 } 75 if appToAdd == nil { 76 continue 77 } 78 statusCount := appToAdd.StatusCounts 79 if len(statusCount) > 0 { 80 for status, count := range statusCount { 81 hashBuilder[status] = hashBuilder[status] + count 82 } 83 } 84 } 85 buildHashCode(applications.VersionsDelta, hashBuilder, toReturn) 86 return constructResponseCache(toReturn, instCount, false) 87 }