Chore: Remove context.TODO (#43458)
* Remove context.TODO() from services * Fix live test
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package managedstream
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
@@ -11,7 +12,7 @@ type FrameCache interface {
|
||||
// GetActiveChannels returns active managed stream channels with JSON schema.
|
||||
GetActiveChannels(orgID int64) (map[string]json.RawMessage, error)
|
||||
// GetFrame returns full JSON frame for a channel in org.
|
||||
GetFrame(orgID int64, channel string) (json.RawMessage, bool, error)
|
||||
GetFrame(ctx context.Context, orgID int64, channel string) (json.RawMessage, bool, error)
|
||||
// Update updates frame cache and returns true if schema changed.
|
||||
Update(orgID int64, channel string, frameJson data.FrameJSONCache) (bool, error)
|
||||
Update(ctx context.Context, orgID int64, channel string, frameJson data.FrameJSONCache) (bool, error)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package managedstream
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"sync"
|
||||
|
||||
@@ -34,14 +35,14 @@ func (c *MemoryFrameCache) GetActiveChannels(orgID int64) (map[string]json.RawMe
|
||||
return info, nil
|
||||
}
|
||||
|
||||
func (c *MemoryFrameCache) GetFrame(orgID int64, channel string) (json.RawMessage, bool, error) {
|
||||
func (c *MemoryFrameCache) GetFrame(ctx context.Context, orgID int64, channel string) (json.RawMessage, bool, error) {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
cachedFrame, ok := c.frames[orgID][channel]
|
||||
return cachedFrame.Bytes(data.IncludeAll), ok, nil
|
||||
}
|
||||
|
||||
func (c *MemoryFrameCache) Update(orgID int64, channel string, jsonFrame data.FrameJSONCache) (bool, error) {
|
||||
func (c *MemoryFrameCache) Update(ctx context.Context, orgID int64, channel string, jsonFrame data.FrameJSONCache) (bool, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if _, ok := c.frames[orgID]; !ok {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package managedstream
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
@@ -15,7 +16,7 @@ func testFrameCache(t *testing.T, c FrameCache) {
|
||||
frameJsonCache, err := data.FrameToJSONCache(frame)
|
||||
require.NoError(t, err)
|
||||
|
||||
updated, err := c.Update(1, "test", frameJsonCache)
|
||||
updated, err := c.Update(context.Background(), 1, "test", frameJsonCache)
|
||||
require.NoError(t, err)
|
||||
require.True(t, updated)
|
||||
|
||||
@@ -27,7 +28,7 @@ func testFrameCache(t *testing.T, c FrameCache) {
|
||||
require.NotZero(t, schema)
|
||||
|
||||
// Make sure the same frame does not update schema.
|
||||
updated, err = c.Update(1, "test", frameJsonCache)
|
||||
updated, err = c.Update(context.Background(), 1, "test", frameJsonCache)
|
||||
require.NoError(t, err)
|
||||
require.False(t, updated)
|
||||
|
||||
@@ -37,17 +38,17 @@ func testFrameCache(t *testing.T, c FrameCache) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// Make sure schema updated.
|
||||
updated, err = c.Update(1, "test", frameJsonCache)
|
||||
updated, err = c.Update(context.Background(), 1, "test", frameJsonCache)
|
||||
require.NoError(t, err)
|
||||
require.True(t, updated)
|
||||
|
||||
// Add the same with another orgID and make sure schema updated.
|
||||
updated, err = c.Update(2, "test", frameJsonCache)
|
||||
updated, err = c.Update(context.Background(), 2, "test", frameJsonCache)
|
||||
require.NoError(t, err)
|
||||
require.True(t, updated)
|
||||
|
||||
// Make sure that the last frame successfully saved in cache.
|
||||
frameJSON, ok, err := c.GetFrame(1, "test")
|
||||
frameJSON, ok, err := c.GetFrame(context.Background(), 1, "test")
|
||||
require.NoError(t, err)
|
||||
require.True(t, ok)
|
||||
|
||||
|
||||
@@ -42,9 +42,9 @@ func (c *RedisFrameCache) GetActiveChannels(orgID int64) (map[string]json.RawMes
|
||||
return info, nil
|
||||
}
|
||||
|
||||
func (c *RedisFrameCache) GetFrame(orgID int64, channel string) (json.RawMessage, bool, error) {
|
||||
func (c *RedisFrameCache) GetFrame(ctx context.Context, orgID int64, channel string) (json.RawMessage, bool, error) {
|
||||
key := getCacheKey(orgchannel.PrependOrgID(orgID, channel))
|
||||
cmd := c.redisClient.HGetAll(context.TODO(), key)
|
||||
cmd := c.redisClient.HGetAll(ctx, key)
|
||||
result, err := cmd.Result()
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
@@ -59,7 +59,7 @@ const (
|
||||
frameCacheTTL = 7 * 24 * time.Hour
|
||||
)
|
||||
|
||||
func (c *RedisFrameCache) Update(orgID int64, channel string, jsonFrame data.FrameJSONCache) (bool, error) {
|
||||
func (c *RedisFrameCache) Update(ctx context.Context, orgID int64, channel string, jsonFrame data.FrameJSONCache) (bool, error) {
|
||||
c.mu.Lock()
|
||||
if _, ok := c.frames[orgID]; !ok {
|
||||
c.frames[orgID] = map[string]data.FrameJSONCache{}
|
||||
@@ -74,8 +74,6 @@ func (c *RedisFrameCache) Update(orgID int64, channel string, jsonFrame data.Fra
|
||||
pipe := c.redisClient.TxPipeline()
|
||||
defer func() { _ = pipe.Close() }()
|
||||
|
||||
ctx := context.TODO()
|
||||
|
||||
pipe.HGetAll(ctx, key)
|
||||
pipe.HMSet(ctx, key, map[string]string{
|
||||
"schema": stringSchema,
|
||||
|
||||
@@ -170,7 +170,7 @@ func NewNamespaceStream(orgID int64, scope string, namespace string, publisher m
|
||||
// Push sends frame to the stream and saves it for later retrieval by subscribers.
|
||||
// * Saves the entire frame to cache.
|
||||
// * If schema has been changed sends entire frame to channel, otherwise only data.
|
||||
func (s *NamespaceStream) Push(path string, frame *data.Frame) error {
|
||||
func (s *NamespaceStream) Push(ctx context.Context, path string, frame *data.Frame) error {
|
||||
jsonFrameCache, err := data.FrameToJSONCache(frame)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -179,7 +179,7 @@ func (s *NamespaceStream) Push(path string, frame *data.Frame) error {
|
||||
// The channel this will be posted into.
|
||||
channel := live.Channel{Scope: s.scope, Namespace: s.namespace, Path: path}.String()
|
||||
|
||||
isUpdated, err := s.frameCache.Update(s.orgID, channel, jsonFrameCache)
|
||||
isUpdated, err := s.frameCache.Update(ctx, s.orgID, channel, jsonFrameCache)
|
||||
if err != nil {
|
||||
logger.Error("Error updating managed stream schema", "error", err)
|
||||
return err
|
||||
@@ -238,9 +238,9 @@ func (s *NamespaceStream) GetHandlerForPath(_ string) (models.ChannelHandler, er
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (s *NamespaceStream) OnSubscribe(_ context.Context, u *models.SignedInUser, e models.SubscribeEvent) (models.SubscribeReply, backend.SubscribeStreamStatus, error) {
|
||||
func (s *NamespaceStream) OnSubscribe(ctx context.Context, u *models.SignedInUser, e models.SubscribeEvent) (models.SubscribeReply, backend.SubscribeStreamStatus, error) {
|
||||
reply := models.SubscribeReply{}
|
||||
frameJSON, ok, err := s.frameCache.GetFrame(u.OrgId, e.Channel)
|
||||
frameJSON, ok, err := s.frameCache.GetFrame(ctx, u.OrgId, e.Channel)
|
||||
if err != nil {
|
||||
return reply, 0, err
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package managedstream
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -57,13 +58,13 @@ func TestGetManagedStreams(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Len(t, managedChannels, 4) // 4 hardcoded testdata streams.
|
||||
|
||||
err = s1.Push("cpu1", data.NewFrame("cpu1"))
|
||||
err = s1.Push(context.Background(), "cpu1", data.NewFrame("cpu1"))
|
||||
require.NoError(t, err)
|
||||
|
||||
err = s1.Push("cpu2", data.NewFrame("cpu2"))
|
||||
err = s1.Push(context.Background(), "cpu2", data.NewFrame("cpu2"))
|
||||
require.NoError(t, err)
|
||||
|
||||
err = s2.Push("cpu1", data.NewFrame("cpu1"))
|
||||
err = s2.Push(context.Background(), "cpu1", data.NewFrame("cpu1"))
|
||||
require.NoError(t, err)
|
||||
|
||||
managedChannels, err = runner.GetManagedChannels(1)
|
||||
@@ -76,7 +77,7 @@ func TestGetManagedStreams(t *testing.T) {
|
||||
// Different org.
|
||||
s3, err := runner.GetOrCreateStream(2, "stream", "test1")
|
||||
require.NoError(t, err)
|
||||
err = s3.Push("cpu1", data.NewFrame("cpu1"))
|
||||
err = s3.Push(context.Background(), "cpu1", data.NewFrame("cpu1"))
|
||||
require.NoError(t, err)
|
||||
managedChannels, err = runner.GetManagedChannels(1)
|
||||
require.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user