Ai
1 Star 9 Fork 0

Feature Probe/server-sdk-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
user.go 1.17 KB
一键复制 编辑 原始数据 按行查看 历史
hezean 提交于 2023-05-31 14:11 +08:00 . fix: add RWMutex for map current write (#19)
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(),
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/featureprobe/server-sdk-go.git
git@gitee.com:featureprobe/server-sdk-go.git
featureprobe
server-sdk-go
server-sdk-go
main

搜索帮助