-
Notifications
You must be signed in to change notification settings - Fork 2
/
entries.go
59 lines (50 loc) · 1.4 KB
/
entries.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
package store
import (
"encoding/json"
"github.com/configcat/configcat-proxy/internal/utils"
configcat "github.com/configcat/go-sdk/v9"
"github.com/configcat/go-sdk/v9/configcatcache"
"sync/atomic"
"time"
)
type EntryStore interface {
LoadEntry() *EntryWithEtag
ComposeBytes() []byte
StoreEntry(data []byte, fetchTime time.Time, eTag string)
}
type EntryWithEtag struct {
ConfigJson []byte
ETag string
FetchTime time.Time
Empty bool
}
type entryStore struct {
entry atomic.Pointer[EntryWithEtag]
modified chan struct{}
}
func NewEntryStore() EntryStore {
e := entryStore{
modified: make(chan struct{}, 1),
}
config := configcat.ConfigJson{}
initial, _ := json.Marshal(config)
e.entry.Store(entryWithEtag(initial, time.Time{}, utils.GenerateEtag(initial), true))
return &e
}
func (e *entryStore) LoadEntry() *EntryWithEtag {
return e.entry.Load()
}
func (e *entryStore) ComposeBytes() []byte {
entry := e.entry.Load()
return configcatcache.CacheSegmentsToBytes(entry.FetchTime, entry.ETag, entry.ConfigJson)
}
func (e *entryStore) StoreEntry(configJson []byte, fetchTime time.Time, eTag string) {
e.entry.Store(entryWithEtag(configJson, fetchTime, eTag, false))
}
func entryWithEtag(configJson []byte, fetchTime time.Time, eTag string, empty bool) *EntryWithEtag {
return &EntryWithEtag{
ConfigJson: configJson,
ETag: eTag,
FetchTime: fetchTime,
Empty: empty}
}