-
Notifications
You must be signed in to change notification settings - Fork 0
/
wathcer.go
65 lines (55 loc) · 995 Bytes
/
wathcer.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
package mongodb
import (
"errors"
"log"
"time"
"github.com/boxgo/box/pkg/config/source"
)
type watcher struct {
name string
source *mongoSource
exit chan bool
changeSet chan *source.ChangeSet
}
func newWatcher(mgo *mongoSource) (source.Watcher, error) {
w := &watcher{
name: "mongodb",
source: mgo,
changeSet: make(chan *source.ChangeSet),
exit: make(chan bool),
}
go w.watch()
return w, nil
}
func (w *watcher) Next() (*source.ChangeSet, error) {
select {
case cs := <-w.changeSet:
return cs, nil
case <-w.exit:
return nil, errors.New("watcher stopped")
}
}
func (w *watcher) Stop() error {
select {
case <-w.exit:
return nil
default:
close(w.exit)
}
return nil
}
func (w *watcher) watch() {
for {
time.Sleep(time.Second * 3)
select {
case <-w.exit:
return
default:
data, err := w.source.Read()
if err != nil {
log.Printf("config mongodb watch error: %#v", err)
}
w.changeSet <- data
}
}
}