diff --git a/cache/config/config_file.go b/cache/config/config_file.go index b649fee66..832e6cdb8 100644 --- a/cache/config/config_file.go +++ b/cache/config/config_file.go @@ -353,11 +353,10 @@ func (fc *fileCache) GetGroupActiveReleases(namespace, group string) ([]*model.C return nil, "" } ret := make([]*model.ConfigFileRelease, 0, 8) - groupBucket.Range(func(key string, val *model.SimpleConfigFileRelease) bool { + groupBucket.ReadRange(func(key string, val *model.SimpleConfigFileRelease) { ret = append(ret, &model.ConfigFileRelease{ SimpleConfigFileRelease: val, }) - return true }) groupRevisions, ok := fc.activeReleaseRevisions.Load(namespace) if !ok { @@ -431,46 +430,42 @@ func (fc *fileCache) QueryReleases(args *types.ConfigReleaseArgs) (uint32, []*mo } values := make([]*model.SimpleConfigFileRelease, 0, args.Limit) - fc.name2release.Range(func(namespace string, groups *utils.SyncMap[string, *utils.SyncMap[string, - *utils.SyncMap[string, *model.SimpleConfigFileRelease]]]) bool { + fc.name2release.ReadRange(func(namespace string, groups *utils.SyncMap[string, *utils.SyncMap[string, + *utils.SyncMap[string, *model.SimpleConfigFileRelease]]]) { if args.Namespace != "" && utils.IsWildNotMatch(namespace, args.Namespace) { - return true + return } - groups.Range(func(group string, files *utils.SyncMap[string, *utils.SyncMap[string, - *model.SimpleConfigFileRelease]]) bool { + groups.ReadRange(func(group string, files *utils.SyncMap[string, *utils.SyncMap[string, + *model.SimpleConfigFileRelease]]) { if args.Group != "" && utils.IsWildNotMatch(group, args.Group) { - return true + return } - files.Range(func(fileName string, releases *utils.SyncMap[string, *model.SimpleConfigFileRelease]) bool { + files.Range(func(fileName string, releases *utils.SyncMap[string, *model.SimpleConfigFileRelease]) { if args.FileName != "" && utils.IsWildNotMatch(fileName, args.FileName) { - return true + return } - releases.Range(func(releaseName string, item *model.SimpleConfigFileRelease) bool { + releases.Range(func(releaseName string, item *model.SimpleConfigFileRelease) { if args.ReleaseName != "" && utils.IsWildNotMatch(item.Name, args.ReleaseName) { - return true + return } if args.OnlyActive && !item.Active { - return true + return } if len(args.Metadata) > 0 { for k, v := range args.Metadata { sv := item.Metadata[k] if sv != v { - return true + return } } } values = append(values, item) - return true }) - return true }) - return true }) - return true }) sort.Slice(values, func(i, j int) bool { diff --git a/cache/config/config_file_metrics.go b/cache/config/config_file_metrics.go index fb10e478f..a23ef8b23 100644 --- a/cache/config/config_file_metrics.go +++ b/cache/config/config_file_metrics.go @@ -69,8 +69,8 @@ func (fc *fileCache) reportMetricsInfo() { } } - fc.metricsReleaseCount.Range(func(namespace string, groups *utils.SyncMap[string, uint64]) bool { - groups.Range(func(groupName string, count uint64) bool { + fc.metricsReleaseCount.ReadRange(func(namespace string, groups *utils.SyncMap[string, uint64]) { + groups.ReadRange(func(groupName string, count uint64) { metricValues = append(metricValues, metrics.ConfigMetrics{ Type: metrics.ReleaseFileMetric, Total: int64(count), @@ -79,9 +79,7 @@ func (fc *fileCache) reportMetricsInfo() { metrics.LabelGroup: groupName, }, }) - return true }) - return true }) plugin.GetStatis().ReportConfigMetrics(metricValues...) diff --git a/cache/config/config_group.go b/cache/config/config_group.go index 552ddeb87..587bc1d03 100644 --- a/cache/config/config_group.go +++ b/cache/config/config_group.go @@ -194,32 +194,30 @@ func (fc *configGroupCache) Query(args *types.ConfigGroupArgs) (uint32, []*model } values := make([]*model.ConfigFileGroup, 0, 8) - fc.name2groups.Range(func(namespce string, groups *utils.SyncMap[string, *model.ConfigFileGroup]) bool { + fc.name2groups.ReadRange(func(namespce string, groups *utils.SyncMap[string, *model.ConfigFileGroup]) { if args.Namespace != "" && utils.IsWildNotMatch(namespce, args.Namespace) { - return true + return } - groups.Range(func(name string, group *model.ConfigFileGroup) bool { + groups.ReadRange(func(name string, group *model.ConfigFileGroup) { if args.Name != "" && utils.IsWildNotMatch(name, args.Name) { - return true + return } if args.Business != "" && utils.IsWildNotMatch(group.Business, args.Business) { - return true + return } if args.Department != "" && utils.IsWildNotMatch(group.Department, args.Department) { - return true + return } if len(args.Metadata) > 0 { for k, v := range args.Metadata { sv, ok := group.Metadata[k] if !ok || sv != v { - return true + return } } } values = append(values, group) - return true }) - return true }) sort.Slice(values, func(i, j int) bool { diff --git a/cache/namespace/namespace.go b/cache/namespace/namespace.go index 467a39517..cf8bd5e34 100644 --- a/cache/namespace/namespace.go +++ b/cache/namespace/namespace.go @@ -135,17 +135,16 @@ func (nsCache *namespaceCache) GetVisibleNamespaces(namespace string) []*model.N // 根据命名空间级别的可见性进行查询 // 先看精确的 - nsCache.exportNamespace.Range(func(exportNs string, viewerNs *utils.SyncSet[string]) bool { + nsCache.exportNamespace.Range(func(exportNs string, viewerNs *utils.SyncSet[string]) { exactMatch := viewerNs.Contains(namespace) allMatch := viewerNs.Contains(types.AllMatched) if !exactMatch && !allMatch { - return true + return } val := nsCache.GetNamespace(exportNs) if val != nil { ret[val.Name] = val } - return true }) values := make([]*model.Namespace, 0, len(ret)) @@ -209,10 +208,8 @@ func (nsCache *namespaceCache) GetNamespacesByName(names []string) []*model.Name func (nsCache *namespaceCache) GetNamespaceList() []*model.Namespace { nsArr := make([]*model.Namespace, 0, 8) - nsCache.ids.Range(func(key string, ns *model.Namespace) bool { + nsCache.ids.Range(func(key string, ns *model.Namespace) { nsArr = append(nsArr, ns) - - return true }) return nsArr diff --git a/cache/service/instance.go b/cache/service/instance.go index a16ca6cac..0b2004ba6 100644 --- a/cache/service/instance.go +++ b/cache/service/instance.go @@ -493,13 +493,7 @@ func (ic *instanceCache) IteratorInstancesWithService(serviceID string, iterProc // GetInstancesCount 获取实例的个数 func (ic *instanceCache) GetInstancesCount() int { - count := 0 - ic.ids.Range(func(key string, value *model.Instance) bool { - count++ - return true - }) - - return count + return ic.ids.Len() } // GetInstanceLabels 获取某个服务下实例的所有标签信息集合 @@ -548,17 +542,11 @@ func (ic *instanceCache) GetServicePorts(serviceID string) []*model.ServicePort // iteratorInstancesProc 迭代指定的instance数据,id->instance func iteratorInstancesProc(data *utils.SyncMap[string, *model.Instance], iterProc types.InstanceIterProc) error { - var ( - cont bool - err error - ) - - proc := func(k string, v *model.Instance) bool { - cont, err = iterProc(k, v) - if err != nil { - return false + var err error + proc := func(k string, v *model.Instance) { + if _, err = iterProc(k, v); err != nil { + return } - return cont } data.Range(proc) diff --git a/cache/service/instance_metrics.go b/cache/service/instance_metrics.go index 9b90685ee..852e8f58e 100644 --- a/cache/service/instance_metrics.go +++ b/cache/service/instance_metrics.go @@ -62,11 +62,11 @@ func (ic *instanceCache) reportMetricsInfo() { }) // instance count metrics - ic.instanceCounts.Range(func(serviceID string, countInfo *model.InstanceCount) bool { + ic.instanceCounts.ReadRange(func(serviceID string, countInfo *model.InstanceCount) { svc := serviceCache.GetServiceByID(serviceID) if svc == nil { log.Debug("[Cache][Instance] report metrics get service not found", zap.String("svc-id", serviceID)) - return true + return } if _, ok := onlineService[svc.Namespace]; !ok { @@ -95,7 +95,7 @@ func (ic *instanceCache) reportMetricsInfo() { }, }) - return true + return }) for ns := range allServices { diff --git a/cache/service/l5.go b/cache/service/l5.go index 3aae9546d..671c5200b 100644 --- a/cache/service/l5.go +++ b/cache/service/l5.go @@ -112,11 +112,11 @@ func (lc *l5Cache) GetRouteByIP(ip uint32) []*model.Route { return out } - entry.Range(func(key string, value string) bool { + entry.ReadRange(func(key string, value string) { // sidStr -> setID sid, err := model.UnmarshalSid(key) if err != nil { - return true + return } item := &model.Route{ @@ -126,7 +126,6 @@ func (lc *l5Cache) GetRouteByIP(ip uint32) []*model.Route { SetID: value, } out = append(out, item) - return true }) return out @@ -140,19 +139,17 @@ func (lc *l5Cache) CheckRouteExisted(ip uint32, modID uint32, cmdID uint32) bool } found := false - entry.Range(func(key string, value string) bool { + entry.ReadRange(func(key string, value string) { sid, err := model.UnmarshalSid(key) if err != nil { // continue range - return true + return } if modID == sid.ModID && cmdID == sid.CmdID { found = true - // break range - return false + return } - return true }) return found diff --git a/cache/service/ratelimit_bucket.go b/cache/service/ratelimit_bucket.go index 481121dd8..b89964acd 100644 --- a/cache/service/ratelimit_bucket.go +++ b/cache/service/ratelimit_bucket.go @@ -43,19 +43,13 @@ type rateLimitRuleBucket struct { } func (r *rateLimitRuleBucket) foreach(proc types.RateLimitIterProc) { - r.rules.Range(func(key string, val *subRateLimitRuleBucket) bool { + r.rules.Range(func(key string, val *subRateLimitRuleBucket) { val.foreach(proc) - return true }) } func (r *rateLimitRuleBucket) count() int { - count := 0 - r.rules.Range(func(key string, val *subRateLimitRuleBucket) bool { - count += val.count() - return true - }) - return count + return r.rules.Len() } func (r *rateLimitRuleBucket) saveRule(rule *model.RateLimit) { diff --git a/cache/service/service.go b/cache/service/service.go index b8ffad9f4..15a73c4ec 100644 --- a/cache/service/service.go +++ b/cache/service/service.go @@ -297,18 +297,12 @@ func (sc *serviceCache) CleanNamespace(namespace string) { // IteratorServices 对缓存中的服务进行迭代 func (sc *serviceCache) IteratorServices(iterProc types.ServiceIterProc) error { - var ( - cont bool - err error - ) - - proc := func(k string, svc *model.Service) bool { + var err error + proc := func(k string, svc *model.Service) { sc.fillServicePorts(svc) - cont, err = iterProc(k, svc) - if err != nil { - return false + if _, err = iterProc(k, svc); err != nil { + return } - return cont } sc.ids.Range(proc) return err @@ -330,13 +324,7 @@ func (sc *serviceCache) GetNamespaceCntInfo(namespace string) model.NamespaceSer // GetServicesCount 获取缓存中服务的个数 func (sc *serviceCache) GetServicesCount() int { - count := 0 - sc.ids.Range(func(key string, value *model.Service) bool { - count++ - return true - }) - - return count + return sc.ids.Len() } // ListServices get service list and revision by namespace @@ -505,14 +493,13 @@ func (sc *serviceCache) appendServiceCountChangeNamespace(changeNs map[string]st sc.plock.Lock() defer sc.plock.Unlock() waitDel := map[string]struct{}{} - sc.pendingServices.Range(func(svcId string, _ struct{}) bool { + sc.pendingServices.ReadRange(func(svcId string, _ struct{}) { svc, ok := sc.ids.Load(svcId) if !ok { - return true + return } changeNs[svc.Namespace] = struct{}{} waitDel[svcId] = struct{}{} - return true }) for svcId := range waitDel { sc.pendingServices.Delete(svcId) @@ -563,12 +550,11 @@ func (sc *serviceCache) postProcessUpdatedServices(affect map[string]struct{}) { count.ServiceCount = 0 count.InstanceCnt = &model.InstanceCount{} - value.Range(func(key string, svc *model.Service) bool { + value.ReadRange(func(key string, svc *model.Service) { count.ServiceCount++ insCnt := sc.instCache.GetInstancesCountByServiceID(svc.ID) count.InstanceCnt.TotalInstanceCount += insCnt.TotalInstanceCount count.InstanceCnt.HealthyInstanceCount += insCnt.HealthyInstanceCount - return true }) } } @@ -603,32 +589,29 @@ func (sc *serviceCache) updateCl5SidAndNames(service *model.Service) { func (sc *serviceCache) GetVisibleServicesInOtherNamespace(svcName, namespace string) []*model.Service { ret := make(map[string]*model.Service) // 根据服务级别的可见性进行查询, 先查询精确匹配 - sc.exportServices.Range(func(exportToNs string, services *utils.SyncMap[string, *model.Service]) bool { + sc.exportServices.ReadRange(func(exportToNs string, services *utils.SyncMap[string, *model.Service]) { if exportToNs != namespace && exportToNs != types.AllMatched { - return true + return } - services.Range(func(_ string, svc *model.Service) bool { + services.ReadRange(func(_ string, svc *model.Service) { if svc.Name == svcName && svc.Namespace != namespace { ret[svc.ID] = svc } - return true }) - return true }) // 根据命名空间级别的可见性进行查询, 先看精确的 - sc.exportNamespace.Range(func(exportNs string, viewerNs *utils.SyncSet[string]) bool { + sc.exportNamespace.ReadRange(func(exportNs string, viewerNs *utils.SyncSet[string]) { exactMatch := viewerNs.Contains(namespace) allMatch := viewerNs.Contains(types.AllMatched) if !exactMatch && !allMatch { - return true + return } svc := sc.GetServiceByName(svcName, exportNs) if svc == nil { - return true + return } ret[svc.ID] = svc - return true }) visibleServices := make([]*model.Service, 0, len(ret)) diff --git a/cache/service/service_contract.go b/cache/service/service_contract.go index ba12872cd..e276d994d 100644 --- a/cache/service/service_contract.go +++ b/cache/service/service_contract.go @@ -166,45 +166,42 @@ func (sc *serviceContractCache) Query(filter map[string]string, offset, limit ui searchProtocol := filter["protocol"] searchVersion := filter["version"] - sc.contracts.Range(func(namespace string, services *utils.SyncMap[string, *utils.SyncMap[string, *model.EnrichServiceContract]]) bool { + sc.contracts.ReadRange(func(namespace string, services *utils.SyncMap[string, *utils.SyncMap[string, *model.EnrichServiceContract]]) { if searchNamespace != "" { if !utils.IsWildMatch(namespace, searchNamespace) { - return true + return } } - services.Range(func(service string, contracts *utils.SyncMap[string, *model.EnrichServiceContract]) bool { + services.ReadRange(func(service string, contracts *utils.SyncMap[string, *model.EnrichServiceContract]) { if searchService != "" { if !utils.IsWildMatch(service, searchService) { - return true + return } } - contracts.Range(func(_ string, val *model.EnrichServiceContract) bool { + contracts.ReadRange(func(_ string, val *model.EnrichServiceContract) { if searchName != "" { names := strings.Split(searchName, ",") for i := range names { if !utils.IsWildMatch(names[i], searchName) { - return true + return } } } if searchProtocol != "" { if !utils.IsWildMatch(val.Protocol, searchProtocol) { - return true + return } } if searchVersion != "" { if !utils.IsWildMatch(val.Version, searchVersion) { - return true + return } } values = append(values, val) - return true + return }) - return true }) - - return true }) sort.Slice(values, func(i, j int) bool { diff --git a/cache/service/service_query.go b/cache/service/service_query.go index 45a4f6b3e..da32d9012 100644 --- a/cache/service/service_query.go +++ b/cache/service/service_query.go @@ -143,9 +143,8 @@ func (sc *serviceCache) matchInstances(instances []*model.Instance, instanceFilt // GetAllNamespaces 返回所有的命名空间 func (sc *serviceCache) GetAllNamespaces() []string { var res []string - sc.names.Range(func(k string, v *utils.SyncMap[string, *model.Service]) bool { + sc.names.ReadRange(func(k string, v *utils.SyncMap[string, *model.Service]) { res = append(res, k) - return true }) return res } @@ -299,9 +298,8 @@ func (sc *serviceCache) getServicesByIteratingCache( if !ok { return 0, nil, nil } - spaces.Range(func(key string, value *model.Service) bool { + spaces.ReadRange(func(key string, value *model.Service) { process(value) - return true }) } else { // 直接名字匹配 diff --git a/common/conn/limit/listener.go b/common/conn/limit/listener.go index 071fc836d..dad9129fd 100644 --- a/common/conn/limit/listener.go +++ b/common/conn/limit/listener.go @@ -159,8 +159,8 @@ func (l *Listener) GetHostConnCount(host string) int32 { // Range 遍历当前持有连接的host func (l *Listener) Range(fn func(host string, count int32) bool) { - l.conns.Range(func(host string, value *counter) bool { - return fn(host, l.GetHostConnCount(host)) + l.conns.Range(func(host string, value *counter) { + _ = fn(host, l.GetHostConnCount(host)) }) } @@ -171,12 +171,7 @@ func (l *Listener) GetListenerConnCount() int32 { // GetDistinctHostCount 获取当前缓存的host的个数 func (l *Listener) GetDistinctHostCount() int32 { - var count int32 - l.conns.Range(func(key string, value *counter) bool { - count++ - return true - }) - return count + return int32(l.conns.Len()) } // GetHostActiveConns 获取指定host的活跃的连接 @@ -224,9 +219,8 @@ func (l *Listener) GetHostConnStats(host string) []*HostConnStat { } // 全量扫描,比较耗时 - l.conns.Range(func(key string, value *counter) bool { + l.conns.ReadRange(func(key string, value *counter) { out = append(out, loadStat(key, value)) - return true }) return out } @@ -386,7 +380,7 @@ func (l *Listener) purgeExpireCounterHandler() { scanCount := 0 purgeCount := 0 waitDel := []string{} - l.conns.Range(func(key string, ct *counter) bool { + l.conns.ReadRange(func(key string, ct *counter) { scanCount++ ct.mu.RLock() if ct.size == 0 && time.Now().Unix()-ct.lastAccess > l.purgeCounterExpire { @@ -394,7 +388,6 @@ func (l *Listener) purgeExpireCounterHandler() { purgeCount++ } ct.mu.RUnlock() - return true }) for i := range waitDel { diff --git a/common/utils/collection.go b/common/utils/collection.go index bd7422891..cbf438bb2 100644 --- a/common/utils/collection.go +++ b/common/utils/collection.go @@ -305,6 +305,15 @@ func (s *SyncMap[K, V]) Range(f func(key K, val V)) { } } +// ReadRange . +func (s *SyncMap[K, V]) ReadRange(f func(key K, val V)) { + s.lock.RLock() + defer s.lock.RUnlock() + for k, v := range s.m { + f(k, v) + } +} + // Delete func (s *SyncMap[K, V]) Delete(key K) { s.lock.Lock() diff --git a/version b/version index a2f6f2c9c..f72019128 100644 --- a/version +++ b/version @@ -1 +1 @@ -v1.17.3 +v1.18.0