[v11.1.x] Anonymous: Fix anonymous cache ignoring device limit evaluation (#94255)

Anonymous: Fix anonymous cache ignoring device limit evaluation (#94218)

* ensure cache contains the evaluation result for device limit

* add device limit errors and warnings

* fix lint

(cherry picked from commit 544b5f905c)

Co-authored-by: Jo <joao.guerreiro@grafana.com>
This commit is contained in:
grafana-delivery-bot[bot]
2024-10-07 09:31:19 +02:00
committed by GitHub
co-authored by Jo
parent 2331c4b6a9
commit e4285198aa
3 changed files with 114 additions and 7 deletions
+14 -4
View File
@@ -2,6 +2,7 @@ package anonimpl
import (
"context"
"errors"
"net/http"
"time"
@@ -79,20 +80,29 @@ func (a *AnonDeviceService) usageStatFn(ctx context.Context) (map[string]any, er
}, nil
}
func (a *AnonDeviceService) tagDeviceUI(ctx context.Context, httpReq *http.Request, device *anonstore.Device) error {
func (a *AnonDeviceService) tagDeviceUI(ctx context.Context, device *anonstore.Device) error {
key := device.CacheKey()
if _, ok := a.localCache.Get(key); ok {
if val, ok := a.localCache.Get(key); ok {
if boolVal, ok := val.(bool); ok && !boolVal {
return anonstore.ErrDeviceLimitReached
}
return nil
}
a.localCache.SetDefault(key, struct{}{})
a.localCache.SetDefault(key, true)
if a.cfg.Env == setting.Dev {
a.log.Debug("Tagging device for UI", "deviceID", device.DeviceID, "device", device, "key", key)
}
if err := a.anonStore.CreateOrUpdateDevice(ctx, device); err != nil {
if errors.Is(err, anonstore.ErrDeviceLimitReached) {
a.localCache.SetDefault(key, false)
return err
}
// invalidate cache if there is an error
a.localCache.Delete(key)
return err
}
@@ -142,7 +152,7 @@ func (a *AnonDeviceService) TagDevice(ctx context.Context, httpReq *http.Request
UpdatedAt: time.Now(),
}
err = a.tagDeviceUI(ctx, httpReq, taggedDevice)
err = a.tagDeviceUI(ctx, taggedDevice)
if err != nil {
a.log.Debug("Failed to tag device for UI", "error", err)
return err