-
Notifications
You must be signed in to change notification settings - Fork 0
/
filewatcher.go
122 lines (104 loc) · 2.58 KB
/
filewatcher.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package filewatcher
import (
"errors"
"log"
"github.com/yarcat/pubsub-go"
)
// WatchCloser allows to inject actual watching implementations.
type WatchCloser interface {
Watch(string, Publisher) error
Close() error
}
// Publisher broadcasts data.
type Publisher struct{ pub pubsub.Publisher }
// Publish broadcasts the string.
func (pub Publisher) Publish(s string) error { return pub.pub.Publish(s) }
var ErrClosed = errors.New("file watcher is closed")
// FileWatcher listens to file changes. Close method should be called to free
// allocated resources.
type FileWatcher struct {
topics topics
be pubsub.Backend
watcher WatchCloser
}
// New returns new file watcher ready to observe file changes. Close method
// should be called when this instance isn't needed anymore to free resources.
func New(watcher WatchCloser, be pubsub.Backend) FileWatcher {
return FileWatcher{
topics: newTopics(),
be: be,
watcher: watcher,
}
}
// Close stops the watcher and frees allocated resources.
func (fw FileWatcher) Close() error {
topics, err := fw.topics.Close()
if err != nil {
return nil
}
for _, t := range topics {
// Topic.Close errors are ignored, but at least we should log them.
if err := t.Close(); err != nil {
log.Printf("FileWatcher.Close() = %v, want = nil", err)
}
}
return fw.watcher.Close()
}
// Watch starts watching changes for the given file.
func (fw FileWatcher) Watch(name string) (*Iterator, error) {
// TODO: Normalize name.
var t pubsub.Topic
if err := fw.topics.Run(func(topics nameTopicMap) error {
var ok bool
t, ok = topics[name]
if !ok {
t = pubsub.CreateTopic(fw.be)
topics[name] = t
pub := Publisher{pubsub.NewPublisher(t)}
return fw.watcher.Watch(name, pub)
}
return nil
}); err != nil {
return nil, err
}
sub, err := pubsub.Subscribe(t)
if err != nil {
return nil, err
}
return &Iterator{sub: sub}, nil
}
// MustWatch is a version of Watch which panics if there was an error.
func (fw FileWatcher) MustWatch(name string) *Iterator {
it, err := fw.Watch(name)
if err != nil {
panic(err)
}
return it
}
type (
nameTopicMap map[string]pubsub.Topic
topics struct {
reg chan nameTopicMap
}
)
func newTopics() topics {
reg := make(chan nameTopicMap, 1)
reg <- make(nameTopicMap)
return topics{reg: reg}
}
func (t topics) Run(f func(nameTopicMap) error) error {
reg, ok := <-t.reg
if !ok {
return ErrClosed
}
defer func() { t.reg <- reg }()
return f(reg)
}
func (t topics) Close() (nameTopicMap, error) {
reg, ok := <-t.reg
if !ok {
return nil, ErrClosed
}
close(t.reg)
return reg, nil
}