-
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
8 changed files
with
302 additions
and
0 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,45 @@ | ||
package plugin | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/alibaba/sentinel-golang/core/flow" | ||
"github.com/alibaba/sentinel-golang/ext/datasource" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func FlowRulesJsonConverter(src []byte) (interface{}, error) { | ||
if len(src) == 0 { | ||
return nil, nil | ||
} | ||
ret := make([]flow.FlowRule, 0) | ||
err := json.Unmarshal(src, &ret) | ||
if err != nil { | ||
return nil, errors.Errorf("Fail to unmarshal source:%+v to []system.SystemRule, err:%+v", src, err) | ||
} | ||
return ret, nil | ||
} | ||
|
||
func FlowRulesUpdater(data interface{}) error { | ||
rules := make([]*flow.FlowRule, 0) | ||
if data == nil { | ||
rules = nil | ||
} else { | ||
val, ok := data.([]flow.FlowRule) | ||
if !ok { | ||
return errors.New(fmt.Sprintf("Fail to type assert data to []*flow.FlowRule, in fact, data: %+v", data)) | ||
} | ||
for _, v := range val { | ||
rules = append(rules, &v) | ||
} | ||
} | ||
succ, err := flow.LoadRules(rules) | ||
if succ { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
func NewFlowRulesHandler() datasource.PropertyHandler { | ||
return datasource.NewDefaultPropertyHandler(FlowRulesJsonConverter, FlowRulesUpdater) | ||
} |
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,45 @@ | ||
package plugin | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/alibaba/sentinel-golang/core/system" | ||
"github.com/alibaba/sentinel-golang/ext/datasource" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func SystemRulesJsonConvert(src []byte) (interface{}, error) { | ||
if len(src) == 0 { | ||
return nil, nil | ||
} | ||
ret := make([]system.SystemRule, 0) | ||
err := json.Unmarshal(src, &ret) | ||
if err != nil { | ||
return nil, errors.Errorf("Fail to unmarshal source:%+v to []system.SystemRule, err:%+v", src, err) | ||
} | ||
return ret, nil | ||
} | ||
|
||
func SystemRulesUpdate(data interface{}) error { | ||
rules := make([]*system.SystemRule, 0) | ||
if data == nil { | ||
rules = nil | ||
} else { | ||
val, ok := data.([]system.SystemRule) | ||
if !ok { | ||
return errors.New(fmt.Sprintf("Fail to type assert data to []*flow.FlowRule, in fact, data: %+v", data)) | ||
} | ||
for _, v := range val { | ||
rules = append(rules, &v) | ||
} | ||
} | ||
succ, err := system.LoadRules(rules) | ||
if succ { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
func NewSystemRulesHandler() *datasource.DefaultPropertyHandler { | ||
return datasource.NewDefaultPropertyHandler(SystemRulesJsonConvert, SystemRulesUpdate) | ||
} |
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,126 @@ | ||
package refreshable_file | ||
|
||
import ( | ||
"fmt" | ||
"github.com/alibaba/sentinel-golang/ext/datasource" | ||
"github.com/alibaba/sentinel-golang/logging" | ||
"github.com/fsnotify/fsnotify" | ||
"github.com/pkg/errors" | ||
"io/ioutil" | ||
"os" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var ( | ||
logger = logging.GetDefaultLogger() | ||
) | ||
|
||
type RefreshableFileDataSource struct { | ||
sourceFilePath string | ||
handlers []datasource.PropertyHandler | ||
once sync.Once | ||
} | ||
|
||
func FileDataSourceStarter(sourceFilePath string, handlers ...datasource.PropertyHandler) *RefreshableFileDataSource { | ||
ds := &RefreshableFileDataSource{ | ||
sourceFilePath: sourceFilePath, | ||
handlers: handlers, | ||
} | ||
ds.Initialize() | ||
return ds | ||
} | ||
|
||
// 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() { | ||
s.doUpdate() | ||
// start watcher | ||
s.once.Do( | ||
func() { | ||
go func() { | ||
watcher, err := fsnotify.NewWatcher() | ||
defer watcher.Close() | ||
|
||
if err != nil { | ||
panic(fmt.Sprintf("Fail to new a watcher of fsnotify, err:%+v", err)) | ||
} | ||
err = watcher.Add(s.sourceFilePath) | ||
if err != nil { | ||
panic(fmt.Sprintf("Fail add a watcher on file(%s), err:%+v", s.sourceFilePath, err)) | ||
} | ||
|
||
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) | ||
return | ||
} | ||
case err := <-watcher.Errors: | ||
logger.Errorf("Watch err on file(%s), err:", s.sourceFilePath, err) | ||
time.Sleep(time.Second * 3) | ||
} | ||
} | ||
}() | ||
}) | ||
} | ||
|
||
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/plugin" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestNewFileDataSource_FlowRule(t *testing.T) { | ||
ds := FileDataSourceStarter("../../../tests/testdata/extension/refreshable_file/FlowRule.json", plugin.NewFlowRulesHandler()) | ||
time.Sleep(5 * time.Second) | ||
ds.Close() | ||
} | ||
|
||
func TestNewFileDataSource_SystemRule(t *testing.T) { | ||
ds := FileDataSourceStarter("../../../tests/testdata/extension/refreshable_file/SystemRule.json", plugin.NewSystemRulesHandler()) | ||
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 | ||
} | ||
] |