-
Notifications
You must be signed in to change notification settings - Fork 448
Dynamic datasource framework init #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,21 @@ | ||
| package datasource | ||
|
|
||
| import ( | ||
| "io" | ||
| ) | ||
|
|
||
| // The generic interface to describe the datasource | ||
| // Each DataSource instance listen in one property type. | ||
| type DataSource interface { | ||
| // Add specified property handler in current datasource | ||
| AddPropertyHandler(h PropertyHandler) | ||
| // Remove specified property handler in current datasource | ||
| RemovePropertyHandler(h PropertyHandler) | ||
| // Read original data from the data source. | ||
| ReadSource() []byte | ||
| // Initialize, init datasource and load initial rules | ||
| // start listener | ||
| Initialize() | ||
| // Close the data source. | ||
| io.Closer | ||
| } |
This file contains hidden or 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,58 @@ | ||
| package datasource | ||
|
|
||
| import ( | ||
| "reflect" | ||
| ) | ||
|
|
||
| // PropertyConvert func is to convert source message bytes to the specific property. | ||
| type PropertyConvert func(src []byte) interface{} | ||
|
|
||
| // PropertyUpdate func is to update the specific properties to downstream. | ||
| type PropertyUpdate func(data interface{}) error | ||
|
|
||
| // abstract interface to | ||
| type PropertyHandler interface { | ||
| // check whether the current src is consistent with last update property | ||
| isPropertyConsistent(src interface{}) bool | ||
| // handle the current property | ||
| Handle(src []byte) error | ||
| } | ||
|
|
||
| // DefaultPropertyHandler encapsulate the Converter and updater of property. | ||
| // One DefaultPropertyHandler instance is to handle one property type. | ||
| // DefaultPropertyHandler should check whether current property is consistent with last update property | ||
| // converter convert the message to the specific property | ||
| // updater update the specific property to downstream. | ||
| type DefaultPropertyHandler struct { | ||
| lastUpdateProperty interface{} | ||
|
|
||
| convert PropertyConvert | ||
| update PropertyUpdate | ||
| } | ||
|
|
||
| func (h *DefaultPropertyHandler) isPropertyConsistent(src interface{}) bool { | ||
| isConsistent := reflect.DeepEqual(src, h.lastUpdateProperty) | ||
| if isConsistent { | ||
| return true | ||
| } else { | ||
| h.lastUpdateProperty = src | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| func (h *DefaultPropertyHandler) Handle(src []byte) error { | ||
| // convert to target property | ||
| realProperty := h.convert(src) | ||
| isConsistent := h.isPropertyConsistent(realProperty) | ||
| if isConsistent { | ||
| return nil | ||
| } | ||
| return h.update(realProperty) | ||
| } | ||
|
|
||
| func NewSinglePropertyHandler(convert PropertyConvert, update PropertyUpdate) *DefaultPropertyHandler { | ||
| return &DefaultPropertyHandler{ | ||
| convert: convert, | ||
| update: update, | ||
| } | ||
| } | ||
This file contains hidden or 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,74 @@ | ||
| package datasource | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "github.com/alibaba/sentinel-golang/core/system" | ||
| "github.com/stretchr/testify/assert" | ||
| "io/ioutil" | ||
| "testing" | ||
| ) | ||
|
|
||
| func MockSystemRulesConvert(src []byte) interface{} { | ||
| ret := make([]system.SystemRule, 0) | ||
| _ = json.Unmarshal(src, &ret) | ||
| return ret | ||
| } | ||
| func MockSystemRulesConvertReturnNil(src []byte) interface{} { | ||
| return nil | ||
| } | ||
| func MockSystemRulesUpdateReturnNil(data interface{}) error { | ||
| return nil | ||
| } | ||
| func MockSystemRulesUpdateReturnError(data interface{}) error { | ||
| return errors.New("MockSystemRulesUpdateReturnError") | ||
| } | ||
|
|
||
| func TestNewSinglePropertyHandler(t *testing.T) { | ||
| got := NewSinglePropertyHandler(MockSystemRulesConvert, MockSystemRulesUpdateReturnNil) | ||
| assert.Truef(t, got.lastUpdateProperty == nil, "lastUpdatePropertyHash:%d, expect nil", got.lastUpdateProperty) | ||
| } | ||
|
|
||
| func TestSinglePropertyHandler_Handle(t *testing.T) { | ||
| h1 := NewSinglePropertyHandler(MockSystemRulesConvertReturnNil, MockSystemRulesUpdateReturnNil) | ||
| r1 := h1.Handle(nil) | ||
| assert.True(t, r1 == nil, "Fail to execute Handle func.") | ||
|
|
||
| h2 := NewSinglePropertyHandler(MockSystemRulesConvert, MockSystemRulesUpdateReturnError) | ||
| src, err := ioutil.ReadFile("../../tests/testdata/extension/SystemRule.json") | ||
| if err != nil { | ||
| t.Errorf("Fail to get source file, err:%+v", err) | ||
| } | ||
| r2 := h2.Handle(src) | ||
| assert.True(t, r2 != nil&&r2.Error()=="MockSystemRulesUpdateReturnError", "Fail to execute Handle func.") | ||
| } | ||
|
|
||
| func TestSinglePropertyHandler_isPropertyConsistent(t *testing.T) { | ||
| h := NewSinglePropertyHandler(MockSystemRulesConvert, MockSystemRulesUpdateReturnNil) | ||
| src, err := ioutil.ReadFile("../../tests/testdata/extension/SystemRule.json") | ||
| if err != nil { | ||
| t.Errorf("Fail to get source file, err:%+v", err) | ||
| } | ||
| ret1 := make([]system.SystemRule, 0) | ||
| _ = json.Unmarshal(src, &ret1) | ||
| isConsistent := h.isPropertyConsistent(ret1) | ||
| assert.True(t, isConsistent == false, "Fail to execute isPropertyConsistent.") | ||
|
|
||
| src2, err := ioutil.ReadFile("../../tests/testdata/extension/SystemRule2.json") | ||
| if err != nil { | ||
| t.Errorf("Fail to get source file, err:%+v", err) | ||
| } | ||
| ret2 := make([]system.SystemRule, 0) | ||
| _ = json.Unmarshal(src2, &ret2) | ||
| isConsistent = h.isPropertyConsistent(ret2) | ||
| assert.True(t, isConsistent == true, "Fail to execute isPropertyConsistent.") | ||
|
|
||
| src3, err := ioutil.ReadFile("../../tests/testdata/extension/SystemRule3.json") | ||
| if err != nil { | ||
| t.Errorf("Fail to get source file, err:%+v", err) | ||
| } | ||
| ret3 := make([]system.SystemRule, 0) | ||
| _ = json.Unmarshal(src3, &ret3) | ||
| isConsistent = h.isPropertyConsistent(ret3) | ||
| assert.True(t, isConsistent == false, "Fail to execute isPropertyConsistent.") | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 | ||
| } | ||
| ] |
This file contains hidden or 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 | ||
| } | ||
| ] |
This file contains hidden or 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,12 @@ | ||
| [ | ||
| { | ||
| "id": 0, | ||
| "metricType": 0, | ||
| "adaptiveStrategy": 0 | ||
| }, | ||
| { | ||
| "id": 1, | ||
| "metricType": 0, | ||
| "adaptiveStrategy": 0 | ||
| } | ||
| ] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.