Ai
1 Star 0 Fork 0

SasukeBo/go-micro

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
watcher.go 1.23 KB
一键复制 编辑 原始数据 按行查看 历史
汪波 提交于 2023-02-23 10:27 +08:00 . fix: 替换包名
package nats
import (
"time"
"gitee.com/sasukebo/go-micro/v4/config/encoder"
"gitee.com/sasukebo/go-micro/v4/config/source"
natsgo "github.com/nats-io/nats.go"
)
type watcher struct {
e encoder.Encoder
name string
bucket string
key string
ch chan *source.ChangeSet
exit chan bool
}
func newWatcher(kv natsgo.KeyValue, bucket, key, name string, e encoder.Encoder) (source.Watcher, error) {
w := &watcher{
e: e,
name: name,
bucket: bucket,
key: key,
ch: make(chan *source.ChangeSet),
exit: make(chan bool),
}
wh, _ := kv.Watch(key)
go func() {
for {
select {
case v := <-wh.Updates():
if v != nil {
w.handle(v.Value())
}
case <-w.exit:
_ = wh.Stop()
return
}
}
}()
return w, nil
}
func (w *watcher) handle(data []byte) {
cs := &source.ChangeSet{
Timestamp: time.Now(),
Format: w.e.String(),
Source: w.name,
Data: data,
}
cs.Checksum = cs.Sum()
w.ch <- cs
}
func (w *watcher) Next() (*source.ChangeSet, error) {
select {
case cs := <-w.ch:
return cs, nil
case <-w.exit:
return nil, source.ErrWatcherStopped
}
}
func (w *watcher) Stop() error {
select {
case <-w.exit:
return nil
default:
close(w.exit)
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sasukebo/go-micro.git
git@gitee.com:sasukebo/go-micro.git
sasukebo
go-micro
go-micro
6e18eb58b836

搜索帮助