1 Star 1 Fork 0

titan-kit/titan

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
process.go 1.13 KB
Copy Edit Raw Blame History
蝶衣人生 authored 2021-05-29 14:04 . first commit
package config
import (
"path"
"sync"
"time"
)
type Processor interface {
Process(listener ChangedListener)
}
type watchProcessor struct {
lastIndex uint64
prefix string
keys []string
stopChan chan bool
errChan chan error
wg sync.WaitGroup
store StoreClient
}
func WatchProcessor(prefix string, keys []string, stopChan chan bool, errChan chan error, store StoreClient) Processor {
var wg sync.WaitGroup
s := make([]string, len(keys))
for i, k := range keys {
s[i] = path.Join(prefix, k)
}
return &watchProcessor{0, prefix, s, stopChan, errChan, wg, store}
}
func (p *watchProcessor) Process(listener ChangedListener) {
p.wg.Add(1)
go p.monitorPrefix(listener)
p.wg.Wait()
}
func (p *watchProcessor) monitorPrefix(listener ChangedListener) {
defer p.wg.Done()
for {
index, err := p.store.WatchPrefix(p.prefix, p.keys, p.lastIndex, p.stopChan)
if err != nil {
p.errChan <- err
time.Sleep(time.Second * 2) // 防止后端错误占用所有资源.
continue
}
p.lastIndex = index
if vl, err := p.store.GetValues(p.keys); err == nil {
listener.Changed(vl)
} else {
p.errChan <- err
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/titan-kit/titan.git
git@gitee.com:titan-kit/titan.git
titan-kit
titan
titan
v0.0.4

Search