-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuser.go
79 lines (64 loc) · 1.17 KB
/
user.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
package featureprobe
import (
"strconv"
"sync"
"time"
)
type FPUser struct {
mu *sync.RWMutex
key string
attrs map[string]string
}
func NewUser() FPUser {
return FPUser{
mu: &sync.RWMutex{},
attrs: map[string]string{},
}
}
func (u FPUser) StableRollout(key string) FPUser {
u.key = key
return u
}
func (u FPUser) Key() string {
if len(u.key) == 0 {
u.key = u.generateKey()
}
return u.key
}
func (u FPUser) generateKey() string {
current := time.Now().UnixNano()
return strconv.FormatInt(current, 10)
}
func (u FPUser) With(key string, value string) FPUser {
u.mu.Lock()
u.attrs[key] = value
u.mu.Unlock()
return u
}
func (u FPUser) GetAll() map[string]string {
u.mu.RLock()
snapshot := make(map[string]string, len(u.attrs))
for k, v := range u.attrs {
snapshot[k] = v
}
u.mu.RUnlock()
return snapshot
}
func (u FPUser) Get(key string) string {
u.mu.RLock()
v := u.attrs[key]
u.mu.RUnlock()
return v
}
func (u FPUser) ContainAttr(key string) bool {
u.mu.RLock()
_, ok := u.attrs[key]
u.mu.RUnlock()
return ok
}
func (u FPUser) ToMap() map[string]interface{} {
return map[string]interface{}{
"key": u.Key(),
"attrs": u.GetAll(),
}
}