3 Star 5 Fork 3

三三物联网/ssiot-core

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
memory.go 1.72 KB
一键复制 编辑 原始数据 按行查看 历史
三三物联网 提交于 2023-04-03 00:22 . 重构
// Package memory is a memory source
package memory
import (
source2 "gitee.com/sansaniot/ssiot-core/config/tool/source"
"sync"
"time"
"github.com/google/uuid"
)
type memory struct {
sync.RWMutex
ChangeSet *source2.ChangeSet
Watchers map[string]*watcher
}
func (s *memory) Read() (*source2.ChangeSet, error) {
s.RLock()
cs := &source2.ChangeSet{
Format: s.ChangeSet.Format,
Timestamp: s.ChangeSet.Timestamp,
Data: s.ChangeSet.Data,
Checksum: s.ChangeSet.Checksum,
Source: s.ChangeSet.Source,
}
s.RUnlock()
return cs, nil
}
func (s *memory) Watch() (source2.Watcher, error) {
w := &watcher{
Id: uuid.New().String(),
Updates: make(chan *source2.ChangeSet, 100),
Source: s,
}
s.Lock()
s.Watchers[w.Id] = w
s.Unlock()
return w, nil
}
func (m *memory) Write(cs *source2.ChangeSet) error {
m.Update(cs)
return nil
}
// Update allows manual updates of the config data.
func (s *memory) Update(c *source2.ChangeSet) {
// don't process nil
if c == nil {
return
}
// hash the file
s.Lock()
// update changeset
s.ChangeSet = &source2.ChangeSet{
Data: c.Data,
Format: c.Format,
Source: "memory",
Timestamp: time.Now(),
}
s.ChangeSet.Checksum = s.ChangeSet.Sum()
// update watchers
for _, w := range s.Watchers {
select {
case w.Updates <- s.ChangeSet:
default:
}
}
s.Unlock()
}
func (s *memory) String() string {
return "memory"
}
func NewSource(opts ...source2.Option) source2.Source {
var options source2.Options
for _, o := range opts {
o(&options)
}
s := &memory{
Watchers: make(map[string]*watcher),
}
if options.Context != nil {
c, ok := options.Context.Value(changeSetKey{}).(*source2.ChangeSet)
if ok {
s.Update(c)
}
}
return s
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sansaniot/ssiot-core.git
git@gitee.com:sansaniot/ssiot-core.git
sansaniot
ssiot-core
ssiot-core
v1.8.28

搜索帮助