-
Notifications
You must be signed in to change notification settings - Fork 3
/
configurationstore.go
60 lines (48 loc) · 1.53 KB
/
configurationstore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package eppoclient
import (
"errors"
lru "github.com/hashicorp/golang-lru/v2"
)
type configurationStore struct {
cache *lru.Cache[string, experimentConfiguration]
}
type Variation struct {
Name string `json:"name"`
Value Value `json:"typedValue"`
ShardRange shardRange `json:"shardRange"`
}
type Allocation struct {
PercentExposure float32 `json:"percentExposure"`
Variations []Variation `json:"variations"`
}
type experimentConfiguration struct {
Name string `json:"name"`
Enabled bool `json:"enabled"`
SubjectShards int `json:"subjectShards"`
Rules []rule `json:"rules"`
Overrides map[string]Value `json:"typedOverrides"`
Allocations map[string]Allocation `json:"allocations"`
}
func newConfigurationStore(maxEntries int) *configurationStore {
var configStore = &configurationStore{}
lruCache, err := lru.New[string, experimentConfiguration](maxEntries)
configStore.cache = lruCache
if err != nil {
panic(err)
}
return configStore
}
func (cs *configurationStore) GetConfiguration(key string) (expConfig experimentConfiguration, err error) {
// Attempt to get the value from the cache
expConfig, ok := cs.cache.Get(key)
if !ok {
return expConfig, errors.New("configuration not found in cache")
}
return expConfig, nil
}
func (cs *configurationStore) SetConfigurations(configs map[string]experimentConfiguration) error {
for key, config := range configs {
cs.cache.Add(key, config)
}
return nil
}