-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwatch.go
More file actions
292 lines (266 loc) · 7.4 KB
/
watch.go
File metadata and controls
292 lines (266 loc) · 7.4 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package slicer
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"iter"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"time"
)
// SlicerFSWatchRequest configures a filesystem watch stream.
//
// Paths, Patterns and Events are repeated query parameters. Empty Events
// delivers all types (create, write, remove, rename, chmod).
//
// LastEventID is sent as the SSE `Last-Event-ID` HTTP header. The server
// validates it as a uint64; cross-connection replay is not yet implemented
// — the field is accepted for forward compatibility.
type SlicerFSWatchRequest struct {
Paths []string `json:"paths,omitempty"`
Patterns []string `json:"patterns,omitempty"`
Events []string `json:"events,omitempty"`
UID uint32 `json:"uid,omitempty"`
Recursive bool `json:"recursive,omitempty"`
OneShot bool `json:"one_shot,omitempty"`
Debounce string `json:"debounce,omitempty"`
Timeout string `json:"timeout,omitempty"`
MaxEvents int `json:"max_events,omitempty"`
LastEventID string `json:"last_event_id,omitempty"`
}
// SlicerFSWatchEvent is one filesystem event delivered over the watch stream.
// ID is the monotonic per-stream id from the SSE `id:` line. Timestamp is
// left as a string to isolate the SDK from any future wire-format drift;
// use ParseFSWatchTimestamp to decode it.
type SlicerFSWatchEvent struct {
ID uint64 `json:"id"`
Type string `json:"type"`
Path string `json:"path"`
Timestamp string `json:"timestamp"`
Size int64 `json:"size"`
IsDir bool `json:"isDir"`
Message string `json:"message,omitempty"`
}
// watchEventPayload mirrors the JSON payload emitted by the agent.
type watchEventPayload struct {
ID uint64 `json:"id"`
Type string `json:"type"`
Path string `json:"path"`
Timestamp string `json:"timestamp"`
Size int64 `json:"size"`
IsDir bool `json:"isDir"`
Message string `json:"message,omitempty"`
}
func (r SlicerFSWatchRequest) toQuery() (url.Values, error) {
if len(r.Paths) == 0 {
return nil, fmt.Errorf("paths is required")
}
qs := url.Values{}
for _, p := range r.Paths {
if p = strings.TrimSpace(p); p != "" {
qs.Add("paths", p)
}
}
if len(qs["paths"]) == 0 {
return nil, fmt.Errorf("paths is required")
}
for _, p := range r.Patterns {
if p = strings.TrimSpace(p); p != "" {
qs.Add("patterns", p)
}
}
for _, e := range r.Events {
if e = strings.TrimSpace(e); e != "" {
qs.Add("events", e)
}
}
if r.UID != 0 && r.UID != NonRootUser {
qs.Set("uid", strconv.FormatUint(uint64(r.UID), 10))
}
if r.Recursive {
qs.Set("recursive", "true")
}
if r.OneShot {
qs.Set("one_shot", "true")
}
if d := strings.TrimSpace(r.Debounce); d != "" {
qs.Set("debounce", d)
}
if t := strings.TrimSpace(r.Timeout); t != "" {
qs.Set("timeout", t)
}
if r.MaxEvents > 0 {
qs.Set("max_events", strconv.Itoa(r.MaxEvents))
}
return qs, nil
}
// WatchFS opens a filesystem watch stream at /vm/{vmName}/fs/watch and
// returns a channel of decoded events and a channel carrying any terminal
// error. Both channels are closed when the stream ends.
//
// Heartbeat SSE comments and `event:` lines are silently discarded; each
// delivered event includes its `id:` in SlicerFSWatchEvent.ID.
func (c *SlicerClient) WatchFS(ctx context.Context, vmName string, req SlicerFSWatchRequest) (<-chan SlicerFSWatchEvent, <-chan error) {
events := make(chan SlicerFSWatchEvent)
errs := make(chan error, 1)
go func() {
defer close(events)
defer close(errs)
qs, err := req.toQuery()
if err != nil {
errs <- err
return
}
u, err := url.Parse(c.baseURL)
if err != nil {
errs <- fmt.Errorf("invalid base URL: %w", err)
return
}
u.Path = path.Join(u.Path, fmt.Sprintf("/vm/%s/fs/watch", vmName))
u.RawQuery = qs.Encode()
httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
errs <- fmt.Errorf("failed to create watch request: %w", err)
return
}
if c.userAgent != "" {
httpReq.Header.Set("User-Agent", c.userAgent)
}
if c.token != "" {
httpReq.Header.Set("Authorization", "Bearer "+c.token)
}
httpReq.Header.Set("Accept", "text/event-stream")
if id := strings.TrimSpace(req.LastEventID); id != "" {
httpReq.Header.Set("Last-Event-ID", id)
}
res, err := c.httpClient.Do(httpReq)
if err != nil {
errs <- fmt.Errorf("failed to open watch stream: %w", err)
return
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
body, _ := io.ReadAll(res.Body)
errs <- fmt.Errorf("watch request failed: %s - %s", res.Status, strings.TrimSpace(string(body)))
return
}
scanner := bufio.NewScanner(res.Body)
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
var (
dataLines []string
eventID uint64
)
emit := func() bool {
if len(dataLines) == 0 {
return true
}
payload := strings.Join(dataLines, "\n")
dataLines = nil
var p watchEventPayload
if err := json.Unmarshal([]byte(payload), &p); err != nil {
errs <- fmt.Errorf("failed to parse watch event: %w", err)
return false
}
evt := SlicerFSWatchEvent{
ID: p.ID,
Type: p.Type,
Path: p.Path,
Timestamp: p.Timestamp,
Size: p.Size,
IsDir: p.IsDir,
Message: p.Message,
}
if evt.ID == 0 {
evt.ID = eventID
}
select {
case <-ctx.Done():
return false
case events <- evt:
return true
}
}
for scanner.Scan() {
line := scanner.Text()
switch {
case line == "":
if !emit() {
return
}
case strings.HasPrefix(line, ":"):
// heartbeat / comment, ignore
case strings.HasPrefix(line, "data:"):
dataLines = append(dataLines, strings.TrimSpace(strings.TrimPrefix(line, "data:")))
case strings.HasPrefix(line, "id:"):
if parsed, perr := strconv.ParseUint(strings.TrimSpace(strings.TrimPrefix(line, "id:")), 10, 64); perr == nil {
eventID = parsed
}
case strings.HasPrefix(line, "event:"):
// named event type, ignored (we only use `watch`)
}
}
if err := scanner.Err(); err != nil {
if ctx.Err() != nil {
return
}
errs <- fmt.Errorf("watch stream read error: %w", err)
return
}
// Flush any trailing SSE block without a closing blank line.
_ = emit()
}()
return events, errs
}
// WatchFSIter is a range-over-func adapter over WatchFS.
//
// Usage:
//
// for evt, err := range client.WatchFSIter(ctx, vm, req) {
// if err != nil {
// // stream error or cancellation
// break
// }
// // consume evt
// }
func (c *SlicerClient) WatchFSIter(ctx context.Context, vmName string, req SlicerFSWatchRequest) iter.Seq2[SlicerFSWatchEvent, error] {
return func(yield func(SlicerFSWatchEvent, error) bool) {
iterCtx, cancel := context.WithCancel(ctx)
defer cancel()
events, errs := c.WatchFS(iterCtx, vmName, req)
for {
select {
case err, ok := <-errs:
if !ok {
return
}
if err != nil {
_ = yield(SlicerFSWatchEvent{}, err)
}
return
case evt, ok := <-events:
if !ok {
return
}
if !yield(evt, nil) {
return
}
case <-iterCtx.Done():
return
}
}
}
}
// ParseFSWatchTimestamp parses the wire timestamp from a watch event.
// The timestamp is RFC3339Nano when present; an empty value returns the
// zero time without error.
func ParseFSWatchTimestamp(v string) (time.Time, error) {
if strings.TrimSpace(v) == "" {
return time.Time{}, nil
}
return time.Parse(time.RFC3339Nano, v)
}