github.com/newrelic/go-agent@v3.26.0+incompatible/internal/rules_cache.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package internal
     5  
     6  import "sync"
     7  
     8  // rulesCache is designed to avoid applying url-rules, txn-name-rules, and
     9  // segment-rules since regexes are expensive!
    10  type rulesCache struct {
    11  	sync.RWMutex
    12  	cache        map[rulesCacheKey]string
    13  	maxCacheSize int
    14  }
    15  
    16  type rulesCacheKey struct {
    17  	isWeb     bool
    18  	inputName string
    19  }
    20  
    21  func newRulesCache(maxCacheSize int) *rulesCache {
    22  	return &rulesCache{
    23  		cache:        make(map[rulesCacheKey]string, maxCacheSize),
    24  		maxCacheSize: maxCacheSize,
    25  	}
    26  }
    27  
    28  func (cache *rulesCache) find(inputName string, isWeb bool) string {
    29  	if nil == cache {
    30  		return ""
    31  	}
    32  	cache.RLock()
    33  	defer cache.RUnlock()
    34  
    35  	return cache.cache[rulesCacheKey{
    36  		inputName: inputName,
    37  		isWeb:     isWeb,
    38  	}]
    39  }
    40  
    41  func (cache *rulesCache) set(inputName string, isWeb bool, finalName string) {
    42  	if nil == cache {
    43  		return
    44  	}
    45  	cache.Lock()
    46  	defer cache.Unlock()
    47  
    48  	if len(cache.cache) >= cache.maxCacheSize {
    49  		return
    50  	}
    51  	cache.cache[rulesCacheKey{
    52  		inputName: inputName,
    53  		isWeb:     isWeb,
    54  	}] = finalName
    55  }