mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
CloudWatch: Fix high CPU load (#20579)
* Cache decrypted securejsondata * Models: Add datasource cache tests
This commit is contained in:
committed by
Arve Knudsen
parent
29b46f7a28
commit
3fc5f4552a
@@ -162,3 +162,49 @@ func (ds *DataSource) getCustomHeaders() map[string]string {
|
||||
|
||||
return headers
|
||||
}
|
||||
|
||||
type cachedDecryptedJSON struct {
|
||||
updated time.Time
|
||||
json map[string]string
|
||||
}
|
||||
|
||||
type secureJSONDecryptionCache struct {
|
||||
cache map[int64]cachedDecryptedJSON
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
var dsDecryptionCache = secureJSONDecryptionCache{
|
||||
cache: make(map[int64]cachedDecryptedJSON),
|
||||
}
|
||||
|
||||
// DecryptedValues returns cached decrypted values from secureJsonData.
|
||||
func (ds *DataSource) DecryptedValues() map[string]string {
|
||||
dsDecryptionCache.Lock()
|
||||
defer dsDecryptionCache.Unlock()
|
||||
|
||||
if item, present := dsDecryptionCache.cache[ds.Id]; present && ds.Updated.Equal(item.updated) {
|
||||
return item.json
|
||||
}
|
||||
|
||||
json := ds.SecureJsonData.Decrypt()
|
||||
dsDecryptionCache.cache[ds.Id] = cachedDecryptedJSON{
|
||||
updated: ds.Updated,
|
||||
json: json,
|
||||
}
|
||||
|
||||
return json
|
||||
}
|
||||
|
||||
// DecryptedValue returns cached decrypted value from cached secureJsonData.
|
||||
func (ds *DataSource) DecryptedValue(key string) (string, bool) {
|
||||
value, exists := ds.DecryptedValues()[key]
|
||||
return value, exists
|
||||
}
|
||||
|
||||
// ClearDSDecryptionCache clears the datasource decryption cache.
|
||||
func ClearDSDecryptionCache() {
|
||||
dsDecryptionCache.Lock()
|
||||
defer dsDecryptionCache.Unlock()
|
||||
|
||||
dsDecryptionCache.cache = make(map[int64]cachedDecryptedJSON)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user