-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathwatch.go
174 lines (146 loc) · 3.54 KB
/
watch.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package eskipfile
import (
"os"
"reflect"
"sync"
"github.com/zalando/skipper/eskip"
)
type watchResponse struct {
routes []*eskip.Route
deletedIDs []string
err error
}
// WatchClient implements a route configuration client with file watching. Use the Watch function to initialize
// instances of it.
type WatchClient struct {
fileName string
routes map[string]*eskip.Route
getAll chan (chan<- watchResponse)
getUpdates chan (chan<- watchResponse)
quit chan struct{}
once sync.Once
}
// Watch creates a route configuration client with file watching. Watch doesn't follow file system nodes, it
// always reads from the file identified by the initially provided file name.
func Watch(name string) *WatchClient {
c := &WatchClient{
fileName: name,
getAll: make(chan (chan<- watchResponse)),
getUpdates: make(chan (chan<- watchResponse)),
quit: make(chan struct{}),
once: sync.Once{},
}
go c.watch()
return c
}
func mapRoutes(r []*eskip.Route) map[string]*eskip.Route {
m := make(map[string]*eskip.Route)
for i := range r {
m[r[i].Id] = r[i]
}
return m
}
func (c *WatchClient) storeRoutes(r []*eskip.Route) {
c.routes = mapRoutes(r)
}
func (c *WatchClient) diffStoreRoutes(r []*eskip.Route) (upsert []*eskip.Route, deletedIDs []string) {
for i := range r {
if !reflect.DeepEqual(r[i], c.routes[r[i].Id]) {
upsert = append(upsert, r[i])
}
}
m := mapRoutes(r)
for id := range c.routes {
if _, keep := m[id]; !keep {
deletedIDs = append(deletedIDs, id)
}
}
c.routes = m
return
}
func (c *WatchClient) deleteAllListIDs() []string {
var ids []string
for id := range c.routes {
ids = append(ids, id)
}
c.routes = nil
return ids
}
func cloneRoutes(r []*eskip.Route) []*eskip.Route {
if len(r) == 0 {
return nil
}
c := make([]*eskip.Route, len(r))
for i, ri := range r {
c[i] = ri.Copy()
}
return c
}
func (c *WatchClient) loadAll() watchResponse {
content, err := os.ReadFile(c.fileName)
if err != nil {
return watchResponse{err: err}
}
r, err := eskip.Parse(string(content))
if err != nil {
return watchResponse{err: err}
}
c.storeRoutes(r)
return watchResponse{routes: cloneRoutes(r)}
}
func (c *WatchClient) loadUpdates() watchResponse {
content, err := os.ReadFile(c.fileName)
if err != nil {
if os.IsNotExist(err) {
deletedIDs := c.deleteAllListIDs()
return watchResponse{deletedIDs: deletedIDs}
}
return watchResponse{err: err}
}
r, err := eskip.Parse(string(content))
if err != nil {
return watchResponse{err: err}
}
upsert, del := c.diffStoreRoutes(r)
return watchResponse{routes: cloneRoutes(upsert), deletedIDs: del}
}
func (c *WatchClient) watch() {
for {
select {
case req := <-c.getAll:
req <- c.loadAll()
case req := <-c.getUpdates:
req <- c.loadUpdates()
case <-c.quit:
return
}
}
}
// LoadAll returns the parsed route definitions found in the file.
func (c *WatchClient) LoadAll() ([]*eskip.Route, error) {
req := make(chan watchResponse)
select {
case c.getAll <- req:
case <-c.quit:
return nil, nil
}
rsp := <-req
return rsp.routes, rsp.err
}
// LoadUpdate returns differential updates when a watched file has changed.
func (c *WatchClient) LoadUpdate() ([]*eskip.Route, []string, error) {
req := make(chan watchResponse)
select {
case c.getUpdates <- req:
case <-c.quit:
return nil, nil, nil
}
rsp := <-req
return rsp.routes, rsp.deletedIDs, rsp.err
}
// Close stops watching the configured file and providing updates.
func (c *WatchClient) Close() {
c.once.Do(func() {
close(c.quit)
})
}