-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
217 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package refreshable_file | ||
|
||
import ( | ||
"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" | ||
"io/ioutil" | ||
"os" | ||
"time" | ||
) | ||
|
||
var ( | ||
logger = logging.GetDefaultLogger() | ||
) | ||
|
||
type RefreshableFileDataSource struct { | ||
sourceFilePath string | ||
handlers []datasource.PropertyHandler | ||
} | ||
|
||
func FileDataSourceStarter(sourceFilePath string, handlers ...datasource.PropertyHandler) (*RefreshableFileDataSource, error) { | ||
ds := &RefreshableFileDataSource{ | ||
sourceFilePath: sourceFilePath, | ||
handlers: handlers, | ||
} | ||
return ds, ds.Initialize() | ||
} | ||
|
||
// return idx if existed, else return -1 | ||
func (s *RefreshableFileDataSource) indexOfHandler(h datasource.PropertyHandler) int { | ||
for idx, handler := range s.handlers { | ||
if handler == h { | ||
return idx | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
func (s *RefreshableFileDataSource) AddPropertyHandler(h datasource.PropertyHandler) { | ||
if s.indexOfHandler(h) < 0 { | ||
return | ||
} | ||
s.handlers = append(s.handlers, h) | ||
} | ||
|
||
func (s *RefreshableFileDataSource) RemovePropertyHandler(h datasource.PropertyHandler) { | ||
idx := s.indexOfHandler(h) | ||
if idx < 0 { | ||
return | ||
} | ||
s.handlers = append(s.handlers[:idx], s.handlers[idx+1:]...) | ||
} | ||
|
||
func (s *RefreshableFileDataSource) ReadSource() ([]byte, error) { | ||
f, err := os.Open(s.sourceFilePath) | ||
defer f.Close() | ||
|
||
if err != nil { | ||
return nil, errors.Errorf("The rules file is not existed, err:%+v.", errors.WithStack(err)) | ||
} | ||
src, err := ioutil.ReadAll(f) | ||
if err != nil { | ||
return nil, errors.Errorf("Fail to read file, err: %+v.", errors.WithStack(err)) | ||
} | ||
return src, nil | ||
} | ||
|
||
func (s *RefreshableFileDataSource) Initialize() error { | ||
s.doUpdate() | ||
|
||
watcher, err := fsnotify.NewWatcher() | ||
if err != nil { | ||
return errors.Errorf("Fail to new a watcher of fsnotify, err:%+v", err) | ||
} | ||
err = watcher.Add(s.sourceFilePath) | ||
if err != nil { | ||
return errors.Errorf("Fail add a watcher on file(%s), err:%+v", s.sourceFilePath, err) | ||
} | ||
|
||
go util.RunWithRecover(func() { | ||
defer watcher.Close() | ||
for { | ||
select { | ||
case ev := <-watcher.Events: | ||
if ev.Op&fsnotify.Write == fsnotify.Write { | ||
s.doUpdate() | ||
} | ||
|
||
if ev.Op&fsnotify.Remove == fsnotify.Remove || ev.Op&fsnotify.Rename == fsnotify.Rename { | ||
logger.Errorf("The file source(%s) was removed or renamed.", s.sourceFilePath) | ||
for _, h := range s.handlers { | ||
err := h.Handle(nil) | ||
if err != nil { | ||
logger.Errorf("RefreshableFileDataSource fail to publish rules, handle:%+v; err=%+v.", h, err) | ||
} | ||
} | ||
} | ||
case err := <-watcher.Errors: | ||
logger.Errorf("Watch err on file(%s), err:", s.sourceFilePath, err) | ||
time.Sleep(time.Second * 3) | ||
} | ||
} | ||
}, logger) | ||
return nil | ||
} | ||
|
||
func (s *RefreshableFileDataSource) doUpdate() { | ||
src, err := s.ReadSource() | ||
if err != nil { | ||
logger.Errorf("%+v", err) | ||
return | ||
} | ||
for _, h := range s.handlers { | ||
err := h.Handle(src) | ||
if err != nil { | ||
logger.Errorf("RefreshableFileDataSource fail to publish rules, handle:%+v; err=%+v.", h, err) | ||
} | ||
} | ||
} | ||
|
||
func (s *RefreshableFileDataSource) Close() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package refreshable_file | ||
|
||
import ( | ||
"github.com/alibaba/sentinel-golang/ext/datasource" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestNewFileDataSource_FlowRule(t *testing.T) { | ||
ds, _ := FileDataSourceStarter("../../../tests/testdata/extension/refreshable_file/FlowRule.json", datasource.NewFlowRulesHandler(datasource.FlowRulesJsonConverter)) | ||
time.Sleep(5 * time.Second) | ||
ds.Close() | ||
} | ||
|
||
func TestNewFileDataSource_SystemRule(t *testing.T) { | ||
ds, _ := FileDataSourceStarter("../../../tests/testdata/extension/refreshable_file/SystemRule.json", datasource.NewSystemRulesHandler(datasource.SystemRulesJsonConverter)) | ||
time.Sleep(5 * time.Second) | ||
ds.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
[ | ||
{ | ||
"id": 0, | ||
"resource": "abc0", | ||
"limitApp": "default", | ||
"grade": 1, | ||
"strategy": 0, | ||
"controlBehavior": 0, | ||
"refResource": "refDefault", | ||
"warmUpPeriodSec": 10, | ||
"maxQueueingTimeMs": 1000, | ||
"clusterMode": false, | ||
"clusterConfig": { | ||
"thresholdType": 0 | ||
} | ||
}, | ||
{ | ||
"id": 1, | ||
"resource": "abc1", | ||
"limitApp": "default", | ||
"grade": 1, | ||
"strategy": 0, | ||
"controlBehavior": 0, | ||
"refResource": "refDefault", | ||
"warmUpPeriodSec": 10, | ||
"maxQueueingTimeMs": 1000, | ||
"clusterMode": false, | ||
"clusterConfig": { | ||
"thresholdType": 0 | ||
} | ||
}, | ||
{ | ||
"id": 2, | ||
"resource": "abc2", | ||
"limitApp": "default", | ||
"grade": 1, | ||
"strategy": 0, | ||
"controlBehavior": 0, | ||
"refResource": "refDefault", | ||
"warmUpPeriodSec": 10, | ||
"maxQueueingTimeMs": 1000, | ||
"clusterMode": false, | ||
"clusterConfig": { | ||
"thresholdType": 0 | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[ | ||
{ | ||
"id": 0, | ||
"metricType": 0, | ||
"adaptiveStrategy": 0 | ||
}, | ||
{ | ||
"id": 1, | ||
"metricType": 0, | ||
"adaptiveStrategy": 0 | ||
}, | ||
{ | ||
"id": 2, | ||
"metricType": 0, | ||
"adaptiveStrategy": 0 | ||
} | ||
] |