forked from alibaba/sentinel-golang
-
Notifications
You must be signed in to change notification settings - Fork 2
/
refreshable_file.go
109 lines (96 loc) · 2.8 KB
/
refreshable_file.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
package file
import (
"io/ioutil"
"os"
"github.com/alibaba/sentinel-golang/ext/datasource"
"github.com/alibaba/sentinel-golang/logging"
"github.com/alibaba/sentinel-golang/util"
"github.com/fsnotify/fsnotify"
"github.com/pkg/errors"
)
type RefreshableFileDataSource struct {
datasource.Base
sourceFilePath string
isInitialized util.AtomicBool
closeChan chan struct{}
watcher *fsnotify.Watcher
}
func NewFileDataSource(sourceFilePath string, handlers ...datasource.PropertyHandler) *RefreshableFileDataSource {
var ds = &RefreshableFileDataSource{
sourceFilePath: sourceFilePath,
closeChan: make(chan struct{}),
}
for _, h := range handlers {
ds.AddPropertyHandler(h)
}
return ds
}
func (s *RefreshableFileDataSource) ReadSource() ([]byte, error) {
f, err := os.Open(s.sourceFilePath)
if err != nil {
return nil, errors.Errorf("RefreshableFileDataSource fail to open the property file, err: %+v.", err)
}
defer f.Close()
src, err := ioutil.ReadAll(f)
if err != nil {
return nil, errors.Errorf("RefreshableFileDataSource fail to read file, err: %+v.", err)
}
return src, nil
}
func (s *RefreshableFileDataSource) Initialize() error {
if !s.isInitialized.CompareAndSet(false, true) {
return nil
}
err := s.doReadAndUpdate()
if err != nil {
logging.Errorf("Fail to execute doReadAndUpdate, err: %+v", err)
}
w, err := fsnotify.NewWatcher()
if err != nil {
return errors.Errorf("Fail to new a watcher instance of fsnotify, err: %+v", err)
}
err = w.Add(s.sourceFilePath)
if err != nil {
return errors.Errorf("Fail add a watcher on file[%s], err: %+v", s.sourceFilePath, err)
}
s.watcher = w
go util.RunWithRecover(func() {
defer s.watcher.Close()
for {
select {
case ev := <-s.watcher.Events:
if ev.Op&fsnotify.Write == fsnotify.Write {
err := s.doReadAndUpdate()
if err != nil {
logging.Errorf("Fail to execute doReadAndUpdate, err: %+v", err)
}
}
if ev.Op&fsnotify.Remove == fsnotify.Remove || ev.Op&fsnotify.Rename == fsnotify.Rename {
logging.Warnf("The file source [%s] was removed or renamed.", s.sourceFilePath)
updateErr := s.Handle(nil)
if updateErr != nil {
logging.Errorf("Fail to update nil property, err: %+v", updateErr)
}
}
case err := <-s.watcher.Errors:
logging.Errorf("Watch err on file[%s], err: %+v", s.sourceFilePath, err)
case <-s.closeChan:
return
}
}
})
return nil
}
func (s *RefreshableFileDataSource) doReadAndUpdate() (err error) {
src, err := s.ReadSource()
if err != nil {
err = errors.Errorf("Fail to read source, err: %+v", err)
return err
}
return s.Handle(src)
}
func (s *RefreshableFileDataSource) Close() error {
s.closeChan <- struct{}{}
logging.Infof("The RefreshableFileDataSource for [%s] had been closed.", s.sourceFilePath)
return nil
}