@@ -2,15 +2,14 @@ package instancemgmt
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
5
"reflect"
6
+ "sync"
7
7
"time"
8
8
9
9
"github.com/prometheus/client_golang/prometheus"
10
10
"github.com/prometheus/client_golang/prometheus/promauto"
11
11
12
12
"github.com/grafana/grafana-plugin-sdk-go/backend"
13
- gocache "github.com/patrickmn/go-cache"
14
13
)
15
14
16
15
var (
19
18
Name : "active_instances" ,
20
19
Help : "The number of active plugin instances" ,
21
20
})
22
- defaultDisposeTTL = 5 * time .Second
23
-
24
- defaultInstanceTTL = 24 * time .Hour
25
- defaultInstanceCleanup = 48 * time .Hour
21
+ disposeTTL = 5 * time .Second
26
22
)
27
23
28
24
// Instance is a marker interface for an instance.
@@ -77,35 +73,21 @@ type InstanceProvider interface {
77
73
78
74
// New create a new instance manager.
79
75
func New (provider InstanceProvider ) InstanceManager {
80
- return NewWithOptions (provider , defaultInstanceTTL , defaultInstanceCleanup , defaultDisposeTTL )
81
- }
82
-
83
- func NewWithOptions (provider InstanceProvider , instanceTTL , instanceCleanup , disposeTTL time.Duration ) InstanceManager {
84
76
if provider == nil {
85
77
panic ("provider cannot be nil" )
86
78
}
87
- cache := gocache .New (instanceTTL , instanceCleanup )
88
- cache .OnEvicted (func (_ string , i interface {}) {
89
- ci := i .(CachedInstance )
90
- if disposer , valid := ci .instance .(InstanceDisposer ); valid {
91
- time .AfterFunc (disposeTTL , func () {
92
- disposer .Dispose ()
93
- })
94
- }
95
- activeInstances .Dec ()
96
- })
97
79
98
80
return & instanceManager {
99
81
provider : provider ,
100
- cache : cache ,
82
+ cache : sync. Map {} ,
101
83
locker : newLocker (),
102
84
}
103
85
}
104
86
105
87
type instanceManager struct {
106
88
locker * locker
107
89
provider InstanceProvider
108
- cache * gocache. Cache
90
+ cache sync. Map
109
91
}
110
92
111
93
func (im * instanceManager ) Get (ctx context.Context , pluginContext backend.PluginContext ) (Instance , error ) {
@@ -114,8 +96,9 @@ func (im *instanceManager) Get(ctx context.Context, pluginContext backend.Plugin
114
96
return nil , err
115
97
}
116
98
// Double-checked locking for update/create criteria
117
- strKey := fmt .Sprintf ("%v" , cacheKey )
118
- item , ok := im .cache .Get (strKey )
99
+ im .locker .RLock (cacheKey )
100
+ item , ok := im .cache .Load (cacheKey )
101
+ im .locker .RUnlock (cacheKey )
119
102
120
103
if ok {
121
104
ci := item .(CachedInstance )
@@ -126,25 +109,32 @@ func (im *instanceManager) Get(ctx context.Context, pluginContext backend.Plugin
126
109
}
127
110
}
128
111
129
- im .locker .Lock (strKey )
130
- defer im .locker .Unlock (strKey )
112
+ im .locker .Lock (cacheKey )
113
+ defer im .locker .Unlock (cacheKey )
131
114
132
- if item , ok := im .cache .Get ( strKey ); ok {
115
+ if item , ok := im .cache .Load ( cacheKey ); ok {
133
116
ci := item .(CachedInstance )
134
117
needsUpdate := im .provider .NeedsUpdate (ctx , pluginContext , ci )
135
118
136
119
if ! needsUpdate {
137
120
return ci .instance , nil
138
121
}
139
122
140
- im .cache .Delete (strKey )
123
+ if disposer , valid := ci .instance .(InstanceDisposer ); valid {
124
+ time .AfterFunc (disposeTTL , func () {
125
+ disposer .Dispose ()
126
+ activeInstances .Dec ()
127
+ })
128
+ } else {
129
+ activeInstances .Dec ()
130
+ }
141
131
}
142
132
143
133
instance , err := im .provider .NewInstance (ctx , pluginContext )
144
134
if err != nil {
145
135
return nil , err
146
136
}
147
- im .cache .SetDefault ( strKey , CachedInstance {
137
+ im .cache .Store ( cacheKey , CachedInstance {
148
138
PluginContext : pluginContext ,
149
139
instance : instance ,
150
140
})
0 commit comments