Chore: Remove context.TODO() (#43409)

* Remove context.TODO() from services

* Fix live test

* Remove context.TODO
This commit is contained in:
idafurjes
2021-12-22 11:02:42 +01:00
committed by GitHub
parent 2409d8dc1f
commit b8852ef6a3
45 changed files with 133 additions and 126 deletions
+6 -6
View File
@@ -52,9 +52,9 @@ func (dc *databaseCache) internalRunGC() {
}
}
func (dc *databaseCache) Get(key string) (interface{}, error) {
func (dc *databaseCache) Get(ctx context.Context, key string) (interface{}, error) {
cacheHit := CacheData{}
session := dc.SQLStore.NewSession(context.Background())
session := dc.SQLStore.NewSession(ctx)
defer session.Close()
exist, err := session.Where("cache_key= ?", key).Get(&cacheHit)
@@ -70,7 +70,7 @@ func (dc *databaseCache) Get(key string) (interface{}, error) {
if cacheHit.Expires > 0 {
existedButExpired := getTime().Unix()-cacheHit.CreatedAt >= cacheHit.Expires
if existedButExpired {
err = dc.Delete(key) // ignore this error since we will return `ErrCacheItemNotFound` anyway
err = dc.Delete(ctx, key) // ignore this error since we will return `ErrCacheItemNotFound` anyway
if err != nil {
dc.log.Debug("Deletion of expired key failed: %v", err)
}
@@ -86,7 +86,7 @@ func (dc *databaseCache) Get(key string) (interface{}, error) {
return item.Val, nil
}
func (dc *databaseCache) Set(key string, value interface{}, expire time.Duration) error {
func (dc *databaseCache) Set(ctx context.Context, key string, value interface{}, expire time.Duration) error {
item := &cachedItem{Val: value}
data, err := encodeGob(item)
if err != nil {
@@ -123,8 +123,8 @@ func (dc *databaseCache) Set(key string, value interface{}, expire time.Duration
return err
}
func (dc *databaseCache) Delete(key string) error {
return dc.SQLStore.WithDbSession(context.Background(), func(session *sqlstore.DBSession) error {
func (dc *databaseCache) Delete(ctx context.Context, key string) error {
return dc.SQLStore.WithDbSession(ctx, func(session *sqlstore.DBSession) error {
sql := "DELETE FROM cache_data WHERE cache_key=?"
_, err := session.Exec(sql, key)
+13 -12
View File
@@ -1,6 +1,7 @@
package remotecache
import (
"context"
"testing"
"time"
@@ -22,37 +23,37 @@ func TestDatabaseStorageGarbageCollection(t *testing.T) {
// set time.now to 2 weeks ago
var err error
getTime = func() time.Time { return time.Now().AddDate(0, 0, -2) }
err = db.Set("key1", obj, 1000*time.Second)
err = db.Set(context.Background(), "key1", obj, 1000*time.Second)
assert.Equal(t, err, nil)
err = db.Set("key2", obj, 1000*time.Second)
err = db.Set(context.Background(), "key2", obj, 1000*time.Second)
assert.Equal(t, err, nil)
err = db.Set("key3", obj, 1000*time.Second)
err = db.Set(context.Background(), "key3", obj, 1000*time.Second)
assert.Equal(t, err, nil)
// insert object that should never expire
err = db.Set("key4", obj, 0)
err = db.Set(context.Background(), "key4", obj, 0)
assert.Equal(t, err, nil)
getTime = time.Now
err = db.Set("key5", obj, 1000*time.Second)
err = db.Set(context.Background(), "key5", obj, 1000*time.Second)
assert.Equal(t, err, nil)
// run GC
db.internalRunGC()
// try to read values
_, err = db.Get("key1")
_, err = db.Get(context.Background(), "key1")
assert.Equal(t, err, ErrCacheItemNotFound, "expected cache item not found. got: ", err)
_, err = db.Get("key2")
_, err = db.Get(context.Background(), "key2")
assert.Equal(t, err, ErrCacheItemNotFound)
_, err = db.Get("key3")
_, err = db.Get(context.Background(), "key3")
assert.Equal(t, err, ErrCacheItemNotFound)
_, err = db.Get("key4")
_, err = db.Get(context.Background(), "key4")
assert.Equal(t, err, nil)
_, err = db.Get("key5")
_, err = db.Get(context.Background(), "key5")
assert.Equal(t, err, nil)
}
@@ -67,9 +68,9 @@ func TestSecondSet(t *testing.T) {
obj := &CacheableStruct{String: "hey!"}
err = db.Set("killa-gorilla", obj, 0)
err = db.Set(context.Background(), "killa-gorilla", obj, 0)
assert.Equal(t, err, nil)
err = db.Set("killa-gorilla", obj, 0)
err = db.Set(context.Background(), "killa-gorilla", obj, 0)
assert.Equal(t, err, nil)
}
+4 -3
View File
@@ -1,6 +1,7 @@
package remotecache
import (
"context"
"time"
"github.com/bradfitz/gomemcache/memcache"
@@ -28,7 +29,7 @@ func newItem(sid string, data []byte, expire int32) *memcache.Item {
}
// Set sets value to given key in the cache.
func (s *memcachedStorage) Set(key string, val interface{}, expires time.Duration) error {
func (s *memcachedStorage) Set(ctx context.Context, key string, val interface{}, expires time.Duration) error {
item := &cachedItem{Val: val}
bytes, err := encodeGob(item)
if err != nil {
@@ -45,7 +46,7 @@ func (s *memcachedStorage) Set(key string, val interface{}, expires time.Duratio
}
// Get gets value by given key in the cache.
func (s *memcachedStorage) Get(key string) (interface{}, error) {
func (s *memcachedStorage) Get(ctx context.Context, key string) (interface{}, error) {
memcachedItem, err := s.c.Get(key)
if err != nil && err.Error() == "memcache: cache miss" {
return nil, ErrCacheItemNotFound
@@ -66,6 +67,6 @@ func (s *memcachedStorage) Get(key string) (interface{}, error) {
}
// Delete delete a key from the cache
func (s *memcachedStorage) Delete(key string) error {
func (s *memcachedStorage) Delete(ctx context.Context, key string) error {
return s.c.Delete(key)
}
+6 -6
View File
@@ -86,19 +86,19 @@ func newRedisStorage(opts *setting.RemoteCacheOptions) (*redisStorage, error) {
}
// Set sets value to given key in session.
func (s *redisStorage) Set(key string, val interface{}, expires time.Duration) error {
func (s *redisStorage) Set(ctx context.Context, key string, val interface{}, expires time.Duration) error {
item := &cachedItem{Val: val}
value, err := encodeGob(item)
if err != nil {
return err
}
status := s.c.Set(context.TODO(), key, string(value), expires)
status := s.c.Set(ctx, key, string(value), expires)
return status.Err()
}
// Get gets value by given key in session.
func (s *redisStorage) Get(key string) (interface{}, error) {
v := s.c.Get(context.TODO(), key)
func (s *redisStorage) Get(ctx context.Context, key string) (interface{}, error) {
v := s.c.Get(ctx, key)
item := &cachedItem{}
err := decodeGob([]byte(v.Val()), item)
@@ -113,7 +113,7 @@ func (s *redisStorage) Get(key string) (interface{}, error) {
}
// Delete delete a key from session.
func (s *redisStorage) Delete(key string) error {
cmd := s.c.Del(context.TODO(), key)
func (s *redisStorage) Delete(ctx context.Context, key string) error {
cmd := s.c.Del(ctx, key)
return cmd.Err()
}
+11 -11
View File
@@ -28,7 +28,7 @@ const (
)
func ProvideService(cfg *setting.Cfg, sqlStore *sqlstore.SQLStore) (*RemoteCache, error) {
client, err := createClient(cfg.RemoteCacheOptions, sqlStore)
client, err := createClient(context.Background(), cfg.RemoteCacheOptions, sqlStore)
if err != nil {
return nil, err
}
@@ -47,13 +47,13 @@ func ProvideService(cfg *setting.Cfg, sqlStore *sqlstore.SQLStore) (*RemoteCache
// ex `remotecache.Register(CacheableStruct{})``
type CacheStorage interface {
// Get reads object from Cache
Get(key string) (interface{}, error)
Get(ctx context.Context, key string) (interface{}, error)
// Set sets an object into the cache. if `expire` is set to zero it will default to 24h
Set(key string, value interface{}, expire time.Duration) error
Set(ctx context.Context, key string, value interface{}, expire time.Duration) error
// Delete object from cache
Delete(key string) error
Delete(ctx context.Context, key string) error
}
// RemoteCache allows Grafana to cache data outside its own process
@@ -65,22 +65,22 @@ type RemoteCache struct {
}
// Get reads object from Cache
func (ds *RemoteCache) Get(key string) (interface{}, error) {
return ds.client.Get(key)
func (ds *RemoteCache) Get(ctx context.Context, key string) (interface{}, error) {
return ds.client.Get(ctx, key)
}
// Set sets an object into the cache. if `expire` is set to zero it will default to 24h
func (ds *RemoteCache) Set(key string, value interface{}, expire time.Duration) error {
func (ds *RemoteCache) Set(ctx context.Context, key string, value interface{}, expire time.Duration) error {
if expire == 0 {
expire = defaultMaxCacheExpiration
}
return ds.client.Set(key, value, expire)
return ds.client.Set(ctx, key, value, expire)
}
// Delete object from cache
func (ds *RemoteCache) Delete(key string) error {
return ds.client.Delete(key)
func (ds *RemoteCache) Delete(ctx context.Context, key string) error {
return ds.client.Delete(ctx, key)
}
// Run starts the backend processes for cache clients.
@@ -95,7 +95,7 @@ func (ds *RemoteCache) Run(ctx context.Context) error {
return ctx.Err()
}
func createClient(opts *setting.RemoteCacheOptions, sqlstore *sqlstore.SQLStore) (CacheStorage, error) {
func createClient(ctx context.Context, opts *setting.RemoteCacheOptions, sqlstore *sqlstore.SQLStore) (CacheStorage, error) {
if opts.Name == redisCacheType {
return newRedisStorage(opts)
}
+8 -7
View File
@@ -1,6 +1,7 @@
package remotecache
import (
"context"
"testing"
"time"
@@ -43,7 +44,7 @@ func TestCachedBasedOnConfig(t *testing.T) {
}
func TestInvalidCacheTypeReturnsError(t *testing.T) {
_, err := createClient(&setting.RemoteCacheOptions{Name: "invalid"}, nil)
_, err := createClient(context.Background(), &setting.RemoteCacheOptions{Name: "invalid"}, nil)
assert.Equal(t, err, ErrInvalidCacheType)
}
@@ -55,10 +56,10 @@ func runTestsForClient(t *testing.T, client CacheStorage) {
func canPutGetAndDeleteCachedObjects(t *testing.T, client CacheStorage) {
cacheableStruct := CacheableStruct{String: "hej", Int64: 2000}
err := client.Set("key1", cacheableStruct, 0)
err := client.Set(context.Background(), "key1", cacheableStruct, 0)
assert.Equal(t, err, nil, "expected nil. got: ", err)
data, err := client.Get("key1")
data, err := client.Get(context.Background(), "key1")
assert.Equal(t, err, nil)
s, ok := data.(CacheableStruct)
@@ -66,23 +67,23 @@ func canPutGetAndDeleteCachedObjects(t *testing.T, client CacheStorage) {
assert.Equal(t, s.String, "hej")
assert.Equal(t, s.Int64, int64(2000))
err = client.Delete("key1")
err = client.Delete(context.Background(), "key1")
assert.Equal(t, err, nil)
_, err = client.Get("key1")
_, err = client.Get(context.Background(), "key1")
assert.Equal(t, err, ErrCacheItemNotFound)
}
func canNotFetchExpiredItems(t *testing.T, client CacheStorage) {
cacheableStruct := CacheableStruct{String: "hej", Int64: 2000}
err := client.Set("key1", cacheableStruct, time.Second)
err := client.Set(context.Background(), "key1", cacheableStruct, time.Second)
assert.Equal(t, err, nil)
// not sure how this can be avoided when testing redis/memcached :/
<-time.After(time.Second + time.Millisecond)
// should not be able to read that value since its expired
_, err = client.Get("key1")
_, err = client.Get(context.Background(), "key1")
assert.Equal(t, err, ErrCacheItemNotFound)
}
+9 -9
View File
@@ -51,9 +51,9 @@ func (uss *UsageStats) GetUsageReport(ctx context.Context) (usagestats.Report, e
metrics["stats.viewers.count"] = statsQuery.Result.Viewers
metrics["stats.orgs.count"] = statsQuery.Result.Orgs
metrics["stats.playlist.count"] = statsQuery.Result.Playlists
metrics["stats.plugins.apps.count"] = uss.appCount()
metrics["stats.plugins.panels.count"] = uss.panelCount()
metrics["stats.plugins.datasources.count"] = uss.dataSourceCount()
metrics["stats.plugins.apps.count"] = uss.appCount(ctx)
metrics["stats.plugins.panels.count"] = uss.panelCount(ctx)
metrics["stats.plugins.datasources.count"] = uss.dataSourceCount(ctx)
metrics["stats.alerts.count"] = statsQuery.Result.Alerts
metrics["stats.active_users.count"] = statsQuery.Result.ActiveUsers
metrics["stats.active_admins.count"] = statsQuery.Result.ActiveAdmins
@@ -366,14 +366,14 @@ func (uss *UsageStats) GetUsageStatsId(ctx context.Context) string {
return anonId
}
func (uss *UsageStats) appCount() int {
return len(uss.pluginStore.Plugins(context.TODO(), plugins.App))
func (uss *UsageStats) appCount(ctx context.Context) int {
return len(uss.pluginStore.Plugins(ctx, plugins.App))
}
func (uss *UsageStats) panelCount() int {
return len(uss.pluginStore.Plugins(context.TODO(), plugins.Panel))
func (uss *UsageStats) panelCount(ctx context.Context) int {
return len(uss.pluginStore.Plugins(ctx, plugins.Panel))
}
func (uss *UsageStats) dataSourceCount() int {
return len(uss.pluginStore.Plugins(context.TODO(), plugins.DataSource))
func (uss *UsageStats) dataSourceCount(ctx context.Context) int {
return len(uss.pluginStore.Plugins(ctx, plugins.DataSource))
}
@@ -317,9 +317,9 @@ func TestMetrics(t *testing.T) {
assert.Equal(t, getSystemStatsQuery.Result.Viewers, metrics.Get("stats.viewers.count").MustInt64())
assert.Equal(t, getSystemStatsQuery.Result.Orgs, metrics.Get("stats.orgs.count").MustInt64())
assert.Equal(t, getSystemStatsQuery.Result.Playlists, metrics.Get("stats.playlist.count").MustInt64())
assert.Equal(t, uss.appCount(), metrics.Get("stats.plugins.apps.count").MustInt())
assert.Equal(t, uss.panelCount(), metrics.Get("stats.plugins.panels.count").MustInt())
assert.Equal(t, uss.dataSourceCount(), metrics.Get("stats.plugins.datasources.count").MustInt())
assert.Equal(t, uss.appCount(context.Background()), metrics.Get("stats.plugins.apps.count").MustInt())
assert.Equal(t, uss.panelCount(context.Background()), metrics.Get("stats.plugins.panels.count").MustInt())
assert.Equal(t, uss.dataSourceCount(context.Background()), metrics.Get("stats.plugins.datasources.count").MustInt())
assert.Equal(t, getSystemStatsQuery.Result.Alerts, metrics.Get("stats.alerts.count").MustInt64())
assert.Equal(t, getSystemStatsQuery.Result.ActiveUsers, metrics.Get("stats.active_users.count").MustInt64())
assert.Equal(t, getSystemStatsQuery.Result.ActiveAdmins, metrics.Get("stats.active_admins.count").MustInt64())