Girish Gowdra | 631ef3d | 2020-06-15 10:45:52 -0700 | [diff] [blame] | 1 | // Copyright (c) 2017 Uber Technologies, Inc. |
| 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 rpcmetrics |
| 16 | |
| 17 | import "sync" |
| 18 | |
| 19 | // normalizedEndpoints is a cache for endpointName -> safeName mappings. |
| 20 | type normalizedEndpoints struct { |
| 21 | names map[string]string |
| 22 | maxSize int |
| 23 | defaultName string |
| 24 | normalizer NameNormalizer |
| 25 | mux sync.RWMutex |
| 26 | } |
| 27 | |
| 28 | func newNormalizedEndpoints(maxSize int, normalizer NameNormalizer) *normalizedEndpoints { |
| 29 | return &normalizedEndpoints{ |
| 30 | maxSize: maxSize, |
| 31 | normalizer: normalizer, |
| 32 | names: make(map[string]string, maxSize), |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // normalize looks up the name in the cache, if not found it uses normalizer |
| 37 | // to convert the name to a safe name. If called with more than maxSize unique |
| 38 | // names it returns "" for all other names beyond those already cached. |
| 39 | func (n *normalizedEndpoints) normalize(name string) string { |
| 40 | n.mux.RLock() |
| 41 | norm, ok := n.names[name] |
| 42 | l := len(n.names) |
| 43 | n.mux.RUnlock() |
| 44 | if ok { |
| 45 | return norm |
| 46 | } |
| 47 | if l >= n.maxSize { |
| 48 | return "" |
| 49 | } |
| 50 | return n.normalizeWithLock(name) |
| 51 | } |
| 52 | |
| 53 | func (n *normalizedEndpoints) normalizeWithLock(name string) string { |
| 54 | norm := n.normalizer.Normalize(name) |
| 55 | n.mux.Lock() |
| 56 | defer n.mux.Unlock() |
| 57 | // cache may have grown while we were not holding the lock |
| 58 | if len(n.names) >= n.maxSize { |
| 59 | return "" |
| 60 | } |
| 61 | n.names[name] = norm |
| 62 | return norm |
| 63 | } |