Skip to content

Commit 6de0843

Browse files
committed
apply PR feedback
1 parent f6f317a commit 6de0843

File tree

6 files changed

+30
-23
lines changed

6 files changed

+30
-23
lines changed

backend/app/instance_provider.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ type InstanceFactoryFunc func(ctx context.Context, settings backend.AppInstanceS
1616
// NewInstanceManager creates a new app instance manager.
1717
//
1818
// This is a helper method for calling NewInstanceProvider and creating a new instancemgmt.InstanceProvider,
19-
// and providing that to instancemgmt.NewContextAwareInstanceManager.
19+
// and providing that to instancemgmt.NewInstanceManagerWrapper.
2020
func NewInstanceManager(fn InstanceFactoryFunc) instancemgmt.InstanceManager {
2121
ip := NewInstanceProvider(fn)
22-
return instancemgmt.NewContextAwareInstanceManager(ip)
22+
return instancemgmt.NewInstanceManagerWrapper(ip)
2323
}
2424

2525
// NewInstanceProvider create a new app instance provider,

backend/datasource/instance_provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type InstanceFactoryFunc func(ctx context.Context, settings backend.DataSourceIn
3030
// and providing that to instancemgmt.New.
3131
func NewInstanceManager(fn InstanceFactoryFunc) instancemgmt.InstanceManager {
3232
ip := NewInstanceProvider(fn)
33-
return instancemgmt.NewContextAwareInstanceManager(ip)
33+
return instancemgmt.NewInstanceManagerWrapper(ip)
3434
}
3535

3636
// NewInstanceProvider create a new data source instance provuder,

backend/instancemgmt/context_aware_manager.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@ import (
77
"github.com/grafana/grafana-plugin-sdk-go/experimental/featuretoggles"
88
)
99

10-
// NewContextAwareInstanceManager creates a new instance manager that dynamically selects
10+
// NewInstanceManagerWrapper creates a new instance manager that dynamically selects
1111
// between standard and TTL instance managers based on feature toggles from the Grafana config.
12-
func NewContextAwareInstanceManager(provider InstanceProvider) InstanceManager {
13-
return &contextAwareInstanceManager{
12+
func NewInstanceManagerWrapper(provider InstanceProvider) InstanceManager {
13+
return &instanceManagerWrapper{
1414
provider: provider,
1515
standardManager: New(provider),
1616
ttlManager: NewTTLInstanceManager(provider),
1717
}
1818
}
1919

20-
// contextAwareInstanceManager is a wrapper that dynamically selects the appropriate
20+
// instanceManagerWrapper is a wrapper that dynamically selects the appropriate
2121
// instance manager implementation based on feature toggles in the context.
22-
type contextAwareInstanceManager struct {
22+
type instanceManagerWrapper struct {
2323
provider InstanceProvider
2424
standardManager InstanceManager
2525
ttlManager InstanceManager
2626
}
2727

2828
// selectManager returns the appropriate instance manager based on the feature toggle
2929
// from the Grafana config in the plugin context.
30-
func (c *contextAwareInstanceManager) selectManager(_ context.Context, pluginContext backend.PluginContext) InstanceManager {
30+
func (c *instanceManagerWrapper) selectManager(_ context.Context, pluginContext backend.PluginContext) InstanceManager {
3131
// Check if TTL instance manager feature toggle is enabled
3232
if pluginContext.GrafanaConfig != nil {
3333
featureToggles := pluginContext.GrafanaConfig.FeatureToggles()
@@ -41,13 +41,13 @@ func (c *contextAwareInstanceManager) selectManager(_ context.Context, pluginCon
4141
}
4242

4343
// Get returns an Instance using the appropriate manager based on feature toggles.
44-
func (c *contextAwareInstanceManager) Get(ctx context.Context, pluginContext backend.PluginContext) (Instance, error) {
44+
func (c *instanceManagerWrapper) Get(ctx context.Context, pluginContext backend.PluginContext) (Instance, error) {
4545
manager := c.selectManager(ctx, pluginContext)
4646
return manager.Get(ctx, pluginContext)
4747
}
4848

4949
// Do provides an Instance as argument to fn using the appropriate manager based on feature toggles.
50-
func (c *contextAwareInstanceManager) Do(ctx context.Context, pluginContext backend.PluginContext, fn InstanceCallbackFunc) error {
50+
func (c *instanceManagerWrapper) Do(ctx context.Context, pluginContext backend.PluginContext, fn InstanceCallbackFunc) error {
5151
manager := c.selectManager(ctx, pluginContext)
5252
return manager.Do(ctx, pluginContext, fn)
5353
}

backend/instancemgmt/context_aware_manager_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111
"github.com/grafana/grafana-plugin-sdk-go/experimental/featuretoggles"
1212
)
1313

14-
func TestContextAwareInstanceManager(t *testing.T) {
14+
func TestInstanceManagerWrapper(t *testing.T) {
1515
ctx := context.Background()
1616
tip := &testInstanceProvider{}
17-
im := NewContextAwareInstanceManager(tip)
17+
im := NewInstanceManagerWrapper(tip)
1818

1919
t.Run("Should use standard manager when feature toggle is disabled", func(t *testing.T) {
2020
pCtx := backend.PluginContext{
@@ -27,7 +27,7 @@ func TestContextAwareInstanceManager(t *testing.T) {
2727
}),
2828
}
2929

30-
manager := im.(*contextAwareInstanceManager).selectManager(ctx, pCtx)
30+
manager := im.(*instanceManagerWrapper).selectManager(ctx, pCtx)
3131
require.IsType(t, &instanceManager{}, manager)
3232
})
3333

@@ -42,7 +42,7 @@ func TestContextAwareInstanceManager(t *testing.T) {
4242
}),
4343
}
4444

45-
manager := im.(*contextAwareInstanceManager).selectManager(ctx, pCtx)
45+
manager := im.(*instanceManagerWrapper).selectManager(ctx, pCtx)
4646
require.IsType(t, &instanceManagerWithTTL{}, manager)
4747
})
4848

@@ -55,7 +55,7 @@ func TestContextAwareInstanceManager(t *testing.T) {
5555
GrafanaConfig: nil,
5656
}
5757

58-
manager := im.(*contextAwareInstanceManager).selectManager(ctx, pCtx)
58+
manager := im.(*instanceManagerWrapper).selectManager(ctx, pCtx)
5959
require.IsType(t, &instanceManager{}, manager)
6060
})
6161

@@ -70,7 +70,7 @@ func TestContextAwareInstanceManager(t *testing.T) {
7070
}),
7171
}
7272

73-
manager := im.(*contextAwareInstanceManager).selectManager(ctx, pCtx)
73+
manager := im.(*instanceManagerWrapper).selectManager(ctx, pCtx)
7474
require.IsType(t, &instanceManagerWithTTL{}, manager)
7575
})
7676

backend/instancemgmt/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// 2. TTL Instance Manager (NewTTLInstanceManager): Uses TTL-based caching
99
// that automatically evicts instances after a configurable time period.
1010
//
11-
// 3. Context-Aware Instance Manager (NewContextAwareInstanceManager):
11+
// 3. Instance Manager Wrapper (NewInstanceManagerWrapper):
1212
// Dynamically selects between standard and TTL managers based on
1313
// feature toggles from the Grafana config in the context.
1414
//

backend/instancemgmt/intance_manager_ttl.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,17 @@ func (im *instanceManagerWithTTL) Get(ctx context.Context, pluginContext backend
6262
}
6363
// Double-checked locking for update/create criteria
6464
cacheKey := fmt.Sprintf("%v", providerKey)
65+
im.locker.RLock(cacheKey)
6566
item, ok := im.cache.Get(cacheKey)
66-
67+
im.locker.RUnlock(cacheKey)
6768
if ok {
6869
ci := item.(CachedInstance)
6970
needsUpdate := im.provider.NeedsUpdate(ctx, pluginContext, ci)
7071

7172
if !needsUpdate {
72-
// SetDefault() creates a new cache entry with fresh TTL, effectively extending the instance's lifetime.
73-
im.cache.SetDefault(cacheKey, ci)
73+
im.locker.Lock(cacheKey)
74+
im.refreshTTL(cacheKey, ci)
75+
im.locker.Unlock(cacheKey)
7476
return ci.instance, nil
7577
}
7678
}
@@ -83,8 +85,7 @@ func (im *instanceManagerWithTTL) Get(ctx context.Context, pluginContext backend
8385
needsUpdate := im.provider.NeedsUpdate(ctx, pluginContext, ci)
8486

8587
if !needsUpdate {
86-
// SetDefault() creates a new cache entry with fresh TTL, effectively extending the instance's lifetime.
87-
im.cache.SetDefault(cacheKey, ci)
88+
im.refreshTTL(cacheKey, ci)
8889
return ci.instance, nil
8990
}
9091

@@ -117,3 +118,9 @@ func (im *instanceManagerWithTTL) Do(ctx context.Context, pluginContext backend.
117118
callInstanceHandlerFunc(fn, instance)
118119
return nil
119120
}
121+
122+
// refreshTTL updates the TTL of the cached instance by resetting its expiration time.
123+
func (im *instanceManagerWithTTL) refreshTTL(cacheKey string, ci CachedInstance) {
124+
// SetDefault() technically creates a new cache entry with fresh TTL, effectively extending the instance's lifetime.
125+
im.cache.SetDefault(cacheKey, ci)
126+
}

0 commit comments

Comments
 (0)