forked from openflagr/flagr
/
eval_cache.go
162 lines (136 loc) · 3.42 KB
/
eval_cache.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package handler
import (
"sync"
"sync/atomic"
"time"
"github.com/paubox/paubox-flagr/swagger_gen/models"
"github.com/paubox/paubox-flagr/pkg/config"
"github.com/paubox/paubox-flagr/pkg/entity"
"github.com/paubox/paubox-flagr/pkg/util"
"github.com/sirupsen/logrus"
"github.com/zhouzhuojie/withtimeout"
)
var (
singletonEvalCache *EvalCache
singletonEvalCacheOnce sync.Once
)
type cacheContainer struct {
idCache map[string]*entity.Flag
keyCache map[string]*entity.Flag
tagCache map[string]map[uint]*entity.Flag
}
// EvalCache is the in-memory cache just for evaluation
type EvalCache struct {
cache atomic.Value
refreshTimeout time.Duration
refreshInterval time.Duration
}
// GetEvalCache gets the EvalCache
var GetEvalCache = func() *EvalCache {
singletonEvalCacheOnce.Do(func() {
ec := &EvalCache{
refreshTimeout: config.Config.EvalCacheRefreshTimeout,
refreshInterval: config.Config.EvalCacheRefreshInterval,
}
singletonEvalCache = ec
})
return singletonEvalCache
}
// Start starts the polling of EvalCache
func (ec *EvalCache) Start() {
err := ec.reloadMapCache()
if err != nil {
panic(err)
}
go func() {
for range time.Tick(ec.refreshInterval) {
err := ec.reloadMapCache()
if err != nil {
logrus.WithField("err", err).Error("reload evaluation cache error")
}
}
}()
}
func (ec *EvalCache) GetByTags(tags []string, operator *string) []*entity.Flag {
var results map[uint]*entity.Flag
if operator == nil || *operator == models.EvaluationBatchRequestFlagTagsOperatorANY {
results = ec.getByTagsANY(tags)
}
if operator != nil && *operator == models.EvaluationBatchRequestFlagTagsOperatorALL {
results = ec.getByTagsALL(tags)
}
values := make([]*entity.Flag, 0, len(results))
for _, f := range results {
values = append(values, f)
}
return values
}
func (ec *EvalCache) getByTagsANY(tags []string) map[uint]*entity.Flag {
results := map[uint]*entity.Flag{}
cache := ec.cache.Load().(*cacheContainer)
for _, t := range tags {
fSet, ok := cache.tagCache[t]
if ok {
for fID, f := range fSet {
results[fID] = f
}
}
}
return results
}
func (ec *EvalCache) getByTagsALL(tags []string) map[uint]*entity.Flag {
results := map[uint]*entity.Flag{}
cache := ec.cache.Load().(*cacheContainer)
for i, t := range tags {
fSet, ok := cache.tagCache[t]
if !ok {
// no flags
return map[uint]*entity.Flag{}
}
if i == 0 {
// store all the flags
for fID, f := range fSet {
results[fID] = f
}
} else {
for fID := range results {
if _, ok := fSet[fID]; !ok {
delete(results, fID)
}
}
// no flags left
if len(results) == 0 {
return results
}
}
}
return results
}
// GetByFlagKeyOrID gets the flag by Key or ID
func (ec *EvalCache) GetByFlagKeyOrID(keyOrID interface{}) *entity.Flag {
s := util.SafeString(keyOrID)
cache := ec.cache.Load().(*cacheContainer)
f, ok := cache.idCache[s]
if !ok {
f = cache.keyCache[s]
}
return f
}
func (ec *EvalCache) reloadMapCache() error {
if config.Config.NewRelicEnabled {
defer config.Global.NewrelicApp.StartTransaction("eval_cache_reload", nil, nil).End()
}
_, _, err := withtimeout.Do(ec.refreshTimeout, func() (interface{}, error) {
idCache, keyCache, tagCache, err := ec.fetchAllFlags()
if err != nil {
return nil, err
}
ec.cache.Store(&cacheContainer{
idCache: idCache,
keyCache: keyCache,
tagCache: tagCache,
})
return nil, err
})
return err
}