/
snapshot.go
130 lines (106 loc) · 2.77 KB
/
snapshot.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
package snapshot
import (
"encoding/binary"
"hash/crc32"
"math/rand"
"sync"
"time"
//"github.com/lyft/goruntime/snapshot/entry"
"github.com/mgarnaudFTOS/mygoruntime/snapshot/entry"
)
func min(lhs uint64, rhs uint64) uint64 {
if lhs < rhs {
return lhs
} else {
return rhs
}
}
// Random number generator. Implementations should be thread safe.
type RandomGenerator interface {
// @return uint64 a new random number.
Random() uint64
}
// Implementation of RandomGenerator that uses a time seeded random generator.
type randomGeneratorImpl struct {
sync.Mutex
random *rand.Rand
}
func (r *randomGeneratorImpl) Random() uint64 {
r.Lock()
v := uint64(r.random.Int63())
r.Unlock()
return v
}
var defaultRandomGenerator RandomGenerator = &randomGeneratorImpl{
random: rand.New(rand.NewSource(time.Now().UnixNano())),
}
// Implementation of Snapshot for the filesystem loader.
type Snapshot struct {
entries map[string]*entry.Entry
}
func New() (s *Snapshot) {
s = &Snapshot{
entries: make(map[string]*entry.Entry),
}
return
}
func (s *Snapshot) FeatureEnabled(key string, defaultValue uint64) bool {
return defaultRandomGenerator.Random()%100 < min(s.GetInteger(key, defaultValue), 100)
}
// FeatureEnabledForID checks that the crc32 of the id and key's byte value falls within the mod of
// the 0-100 value for the given feature. Use this method for "sticky" features
func (s *Snapshot) FeatureEnabledForID(key string, id uint64, defaultPercentage uint32) bool {
if e, ok := s.Entries()[key]; ok {
if e.Uint64Valid {
return enabled(id, uint32(e.Uint64Value), key)
}
}
return enabled(id, defaultPercentage, key)
}
func (s *Snapshot) Get(key string) string {
e, ok := s.entries[key]
if ok {
return e.StringValue
} else {
return ""
}
}
func (s *Snapshot) GetInteger(key string, defaultValue uint64) uint64 {
e, ok := s.entries[key]
if ok && e.Uint64Valid {
return e.Uint64Value
} else {
return defaultValue
}
}
// GetModified returns the last modified timestamp for key. If key does not
// exist, the zero value for time.Time is returned.
func (s *Snapshot) GetModified(key string) time.Time {
if e, ok := s.entries[key]; ok {
return e.Modified
}
return time.Time{}
}
func (s *Snapshot) Keys() []string {
ret := []string{}
for key := range s.entries {
ret = append(ret, key)
}
return ret
}
func (s *Snapshot) Entries() map[string]*entry.Entry {
return s.entries
}
func (s *Snapshot) SetEntry(key string, e *entry.Entry) {
s.entries[key] = e
}
func enabled(id uint64, percentage uint32, feature string) bool {
uid := crc(id, feature)
return uid%100 < percentage
}
func crc(id uint64, feature string) uint32 {
b := make([]byte, 8, len(feature)+8)
binary.LittleEndian.PutUint64(b, id)
b = append(b, []byte(feature)...)
return crc32.ChecksumIEEE(b)
}