-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.go
68 lines (55 loc) · 1.05 KB
/
watcher.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
package micro_consul
import (
"go-micro.dev/v4/config/source"
"time"
"github.com/hashicorp/consul/api"
)
type watcher struct {
c *consul
kv *api.KV
exit chan bool
}
func newWatcher(c *consul) (source.Watcher, error) {
config := api.DefaultConfig()
config.Address = c.url
config.Token = c.token
client, err := api.NewClient(config)
if err != nil {
return nil, err
}
kv := client.KV()
return &watcher{
c: c,
kv: kv,
exit: make(chan bool),
}, nil
}
func (w *watcher) Next() (*source.ChangeSet, error) {
select {
case <-w.exit:
return nil, source.ErrWatcherStopped
default:
}
options := api.QueryOptions{
WaitIndex: w.c.lastWaitIndex,
}
pair, meta, err := w.kv.Get(w.c.key, &options)
if err != nil {
return nil, err
}
if pair == nil {
return nil, source.ErrWatcherStopped
}
w.c.lastWaitIndex = meta.LastIndex
cs := &source.ChangeSet{
Format: "json",
Source: w.c.String(),
Timestamp: time.Now(),
Data: pair.Value,
}
cs.Checksum = cs.Sum()
return cs, nil
}
func (w *watcher) Stop() error {
return nil
}