github.com/alibaba/sentinel-golang@v1.0.4/core/stat/node_storage.go (about) 1 // Copyright 1999-2020 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package stat 16 17 import ( 18 "sync" 19 20 "github.com/alibaba/sentinel-golang/core/base" 21 "github.com/alibaba/sentinel-golang/logging" 22 ) 23 24 type ResourceNodeMap map[string]*ResourceNode 25 26 var ( 27 inboundNode = NewResourceNode(base.TotalInBoundResourceName, base.ResTypeCommon) 28 29 resNodeMap = make(ResourceNodeMap) 30 rnsMux = new(sync.RWMutex) 31 ) 32 33 // InboundNode returns the global inbound statistic node. 34 func InboundNode() *ResourceNode { 35 return inboundNode 36 } 37 38 // ResourceNodeList returns the slice of all existing resource nodes. 39 func ResourceNodeList() []*ResourceNode { 40 rnsMux.RLock() 41 defer rnsMux.RUnlock() 42 43 list := make([]*ResourceNode, 0, len(resNodeMap)) 44 for _, v := range resNodeMap { 45 list = append(list, v) 46 } 47 return list 48 } 49 50 func GetResourceNode(resource string) *ResourceNode { 51 rnsMux.RLock() 52 defer rnsMux.RUnlock() 53 54 return resNodeMap[resource] 55 } 56 57 func GetOrCreateResourceNode(resource string, resourceType base.ResourceType) *ResourceNode { 58 node := GetResourceNode(resource) 59 if node != nil { 60 return node 61 } 62 rnsMux.Lock() 63 defer rnsMux.Unlock() 64 65 node = resNodeMap[resource] 66 if node != nil { 67 return node 68 } 69 70 if len(resNodeMap) >= int(base.DefaultMaxResourceAmount) { 71 logging.Warn("[GetOrCreateResourceNode] Resource amount exceeds the threshold", "maxResourceAmount", base.DefaultMaxResourceAmount) 72 } 73 node = NewResourceNode(resource, resourceType) 74 resNodeMap[resource] = node 75 return node 76 } 77 78 func ResetResourceNodeMap() { 79 rnsMux.Lock() 80 defer rnsMux.Unlock() 81 resNodeMap = make(ResourceNodeMap) 82 }