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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
|
package reload
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
)
// Config is a Reload configuration point.
//type Config struct {
// Enable or disable Reload extension, default disable.
//Enabled bool
//
// Watch is general pattern of files to watch. It will be applied to every directory in project
//Watch []string
//
// Services is set of services which would be reloaded in case of FS changes
//Services map[string]ServiceConfig
//}
//
//type ServiceConfig struct {
// Watch is per-service specific files to watch
//Watch []string
// Dirs is per-service specific dirs which will be combined with Watch
//Dirs []string
// Ignore is set of files which would not be watched
//Ignore []string
//}
// An Op is a type that is used to describe what type
// of event has occurred during the watching process.
type Op uint32
// Ops
const (
Create Op = iota
Write
Remove
Rename
Chmod
Move
)
var ops = map[Op]string{
Create: "CREATE",
Write: "WRITE",
Remove: "REMOVE",
Rename: "RENAME",
Chmod: "CHMOD",
Move: "MOVE",
}
var ErrorSkip = errors.New("file is skipped")
// FilterFileHookFunc is a function that is called to filter files during listings.
// If a file is ok to be listed, nil is returned otherwise ErrSkip is returned.
type FilterFileHookFunc func(info os.FileInfo, fullPath string) error
// RegexFilterHook is a function that accepts or rejects a file
// for listing based on whether it's filename or full path matches
// a regular expression.
func RegexFilterHook(r *regexp.Regexp, useFullPath bool) FilterFileHookFunc {
return func(info os.FileInfo, fullPath string) error {
str := info.Name()
if useFullPath {
str = fullPath
}
// Match
if r.MatchString(str) {
return nil
}
// No match.
return ErrorSkip
}
}
func SetFileHooks(fileHook ...FilterFileHookFunc) Options {
return func(watcher *Watcher) {
watcher.filterHooks = fileHook
}
}
// An Event describes an event that is received when files or directory
// changes occur. It includes the os.FileInfo of the changed file or
// directory and the type of event that's occurred and the full path of the file.
type Event struct {
Op
Path string
OldPath string
os.FileInfo
Type string // type of event, http, grpc, etc...
}
type Watcher struct {
Event chan Event
errors chan error
close chan struct{}
Closed chan struct{}
mu *sync.Mutex
//wg *sync.WaitGroup
filterHooks []FilterFileHookFunc
started bool // indicates is walker started or not
workingDir string
maxFileWatchEvents int
operations map[Op]struct{} // Op filtering.
files map[string]os.FileInfo //files by service, http, grpc, etc..
ignored map[string]string //ignored files or directories
}
// Options is used to set Watcher Options
type Options func(*Watcher)
func NewWatcher(options ...Options) (*Watcher, error) {
dir, err := os.Getwd()
if err != nil {
return nil, err
}
w := &Watcher{
Event: make(chan Event),
mu: &sync.Mutex{},
//wg: &sync.WaitGroup{},
Closed: make(chan struct{}),
close: make(chan struct{}),
workingDir: dir,
operations: make(map[Op]struct{}),
files: make(map[string]os.FileInfo),
ignored: make(map[string]string),
}
for _, option := range options {
option(w)
}
// dir --> /home/valery/Projects/opensource/roadrunner
return w, nil
}
// https://en.wikipedia.org/wiki/Inotify
// SetMaxFileEvents sets max file notify events for Watcher
// In case of file watch errors, this value can be increased system-wide
// For linux: set --> fs.inotify.max_user_watches = 600000 (under /etc/<choose_name_here>.conf)
// Add apply: sudo sysctl -p --system
func SetMaxFileEvents(events int) Options {
return func(watcher *Watcher) {
watcher.maxFileWatchEvents = events
}
}
// SetDefaultRootPath is used to set own root path for adding files
func SetDefaultRootPath(path string) Options {
return func(watcher *Watcher) {
watcher.workingDir = path
}
}
// Add
// name will be current working dir
func (w *Watcher) AddSingle(name string) error {
name, err := filepath.Abs(name)
if err != nil {
}
// Ignored files
// map is to have O(1) when search for file
_, ignored := w.ignored[name]
if ignored {
return nil
}
// small optimization for smallvector
//fileList := make(map[string]os.FileInfo, 10)
fileList, err := w.retrieveSingleDirectoryContent(name)
if err != nil {
return err
}
for k, v := range fileList {
w.files[k] = v
}
return nil
}
func (w *Watcher) AddRecursive(name string) error {
name, err := filepath.Abs(name)
if err != nil {
return err
}
filesList := make(map[string]os.FileInfo, 10)
err = w.retrieveFilesRecursive(name, filesList)
if err != nil {
return err
}
for k, v := range filesList {
w.files[k] = v
}
return nil
}
// pass map from outside
func (w *Watcher) retrieveSingleDirectoryContent(path string) (map[string]os.FileInfo, error) {
stat, err := os.Stat(path)
if err != nil {
return nil, err
}
filesList := make(map[string]os.FileInfo, 10)
filesList[path] = stat
// if it's not a dir, return
if !stat.IsDir() {
return filesList, nil
}
//err = filepath.Walk(name, func(path string, info os.FileInfo, err error) error {
// if info.IsDir() {
// return nil
// }
//
// fileList[path] = info
//
// return nil
//})
//
//if err != nil {
// return err
//}
fileInfoList, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
// recursive calls are slow in compare to goto
// so, we will add files with goto pattern
outer:
for i := 0; i < len(fileInfoList); i++ {
var path string
// BCE check elimination
// https://go101.org/article/bounds-check-elimination.html
if len(fileInfoList) != 0 && len(fileInfoList) >= i {
path = filepath.Join(path, fileInfoList[i].Name())
} else {
return nil, errors.New("file info list len")
}
// if file in ignored --> continue
if _, ignored := w.ignored[path]; ignored {
continue
}
for _, fh := range w.filterHooks {
err := fh(fileInfoList[i], path)
if err != nil {
// if err is not nil, move to the start of the cycle since the path not match the hook
continue outer
}
}
filesList[path] = fileInfoList[i]
}
return filesList, nil
}
func (w *Watcher) StartPolling(duration time.Duration) error {
if duration < time.Second {
return errors.New("too short duration, please use at least 1 second")
}
w.mu.Lock()
if w.started {
w.mu.Unlock()
return errors.New("already started")
}
w.started = true
w.mu.Unlock()
//w.wg.Done()
return w.waitEvent(duration)
}
// this is blocking operation
func (w *Watcher) waitEvent(d time.Duration) error {
for {
// done lets the inner polling cycle loop know when the
// current cycle's method has finished executing.
//done := make(chan struct{})
// Any events that are found are first piped to evt before
// being sent to the main Event channel.
//evt := make(chan Event)
// Retrieve the file list for all watched file's and dirs.
//fileList := w.files
// cancel can be used to cancel the current event polling function.
cancel := make(chan struct{})
// Look for events.
//go func() {
// w.pollEvents(w.files, evt, cancel)
// done <- struct{}{}
//}()
// numEvents holds the number of events for the current cycle.
//numEvents := 0
ticker := time.NewTicker(d)
for {
select {
case <-w.close:
close(cancel)
close(w.Closed)
return nil
case <-ticker.C:
//fileList := make(map[string]os.FileInfo, 100)
//w.mu.Lock()
fileList, _ := w.retrieveFileList(w.workingDir, false)
w.pollEvents(fileList, cancel)
//w.mu.Unlock()
default:
}
}
ticker.Stop()
//inner:
// for {
// select {
// case <-w.close:
// close(cancel)
// close(w.Closed)
// return nil
// case event := <-evt:
// //if len(w.operations) > 0 { // Filter Ops.
// // _, found := w.operations[event.Op]
// // if !found {
// // continue
// // }
// //}
// //numEvents++
// //if w.maxFileWatchEvents > 0 && numEvents > w.maxFileWatchEvents {
// // close(cancel)
// // break inner
// //}
// w.Event <- event
// case <-done: // Current cycle is finished.
// break inner
// }
// }
//// Update the file's list.
//w.mu.Lock()
//w.files = fileList
//w.mu.Unlock()
//time.Sleep(d)
//sleepLoop:
// for {
// select {
// case <-w.close:
// close(cancel)
// return nil
// case <-time.After(d):
// break sleepLoop
// }
// } //end Sleep for
}
}
func (w *Watcher) retrieveFileList(path string, recursive bool) (map[string]os.FileInfo, error) {
//fileList := make(map[string]os.FileInfo)
//list := make(map[string]os.FileInfo, 100)
//var err error
if recursive {
//fileList, err := w.retrieveFilesRecursive(path)
//if err != nil {
//if os.IsNotExist(err) {
// w.mu.Unlock()
// // todo path error
// _, ok := err.(*os.PathError)
// if ok {
// w.RemoveRecursive(path)
// }
// w.mu.Lock()
//} else {
// w.errors <- err
//}
//}
//for k, v := range fileList {
// fileList[k] = v
//}
//return fileList, nil
return nil, nil
} else {
fileList, err := w.retrieveSingleDirectoryContent(path)
if err != nil {
//if os.IsNotExist(err) {
// w.mu.Unlock()
// _, ok := err.(*os.PathError)
// if ok {
// w.RemoveRecursive(path)
// }
// w.mu.Lock()
//} else {
// w.errors <- err
//}
}
for k, v := range fileList {
fileList[k] = v
}
return fileList, nil
}
// Add the file's to the file list.
//return nil
}
// RemoveRecursive removes either a single file or a directory recursively from
// the file's list.
func (w *Watcher) RemoveRecursive(name string) (err error) {
w.mu.Lock()
defer w.mu.Unlock()
name, err = filepath.Abs(name)
if err != nil {
return err
}
// If name is a single file, remove it and return.
info, found := w.files[name]
if !found {
return nil // Doesn't exist, just return.
}
if !info.IsDir() {
delete(w.files, name)
return nil
}
// If it's a directory, delete all of it's contents recursively
// from w.files.
for path := range w.files {
if strings.HasPrefix(path, name) {
delete(w.files, path)
}
}
return nil
}
func (w *Watcher) retrieveFilesRecursive(name string, fileList map[string]os.FileInfo) error {
return filepath.Walk(name, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
for _, f := range w.filterHooks {
err := f(info, path)
if err == ErrorSkip {
return nil
}
if err != nil {
return err
}
}
// If path is ignored and it's a directory, skip the directory. If it's
// ignored and it's a single file, skip the file.
_, ignored := w.ignored[path]
if ignored {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
// Add the path and it's info to the file list.
fileList[path] = info
return nil
})
}
// Wait blocks until the watcher is started.
//func (w *Watcher) Wait() {
// w.wg.Wait()
//}
func (w *Watcher) pollEvents(files map[string]os.FileInfo, cancel chan struct{}) {
w.mu.Lock()
defer w.mu.Unlock()
// Store create and remove events for use to check for rename events.
creates := make(map[string]os.FileInfo)
removes := make(map[string]os.FileInfo)
// Check for removed files.
for path, info := range w.files {
if _, found := files[path]; !found {
removes[path] = info
}
}
// Check for created files, writes and chmods.
for path, info := range files {
oldInfo, found := w.files[path]
if !found {
// A file was created.
creates[path] = info
continue
}
if oldInfo.ModTime() != info.ModTime() {
w.files[path] = info
select {
case <-cancel:
return
case w.Event <- Event{Write, path, path, info, "http"}:
}
}
if oldInfo.Mode() != info.Mode() {
select {
case <-cancel:
return
case w.Event <- Event{Chmod, path, path, info, "http"}:
}
}
}
// Check for renames and moves.
for path1, info1 := range removes {
for path2, info2 := range creates {
if sameFile(info1, info2) {
e := Event{
Op: Move,
Path: path2,
OldPath: path1,
FileInfo: info1,
}
// If they are from the same directory, it's a rename
// instead of a move event.
if filepath.Dir(path1) == filepath.Dir(path2) {
e.Op = Rename
}
delete(removes, path1)
delete(creates, path2)
select {
case <-cancel:
return
case w.Event <- e:
}
}
}
}
// Send all the remaining create and remove events.
for path, info := range creates {
select {
case <-cancel:
return
case w.Event <- Event{Create, path, "", info, "http"}:
}
}
for path, info := range removes {
select {
case <-cancel:
return
case w.Event <- Event{Remove, path, path, info, "http"}:
}
}
}
|