Skip to content

Commit 217aa0c

Browse files
committed
add logic of mixed rule_type which is used to adapt to shared mode of OpensergoClient.
1 parent d205c38 commit 217aa0c

File tree

5 files changed

+95
-6
lines changed

5 files changed

+95
-6
lines changed

Diff for: pkg/datasource/opensergo/demo/datasource_opensergo_example.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type Counter struct {
4747

4848
func main() {
4949

50-
handler := datasource.NewFlowRulesHandler(datasource.FlowRuleJsonArrayParser)
50+
handler := datasource.NewDefaultPropertyHandler(opensergo.MixedPropertyJsonArrayParser, opensergo.MixedPropertyUpdater)
5151
openSergoDataSource, _ := opensergo.NewOpenSergoDataSource(host, port, namespace, app, handler)
5252
openSergoDataSource.Initialize()
5353
openSergoDataSource.Start()

Diff for: pkg/datasource/opensergo/mixed_property_adaptor.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package opensergo
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/alibaba/sentinel-golang/ext/datasource"
7+
)
8+
9+
// MixedPropertyJsonArrayParser provide JSON as the default serialization for MixedRule
10+
func MixedPropertyJsonArrayParser(src []byte) (interface{}, error) {
11+
mixedRules := new(MixedRule)
12+
if err := json.Unmarshal(src, mixedRules); err != nil {
13+
desc := fmt.Sprintf("Fail to convert source bytes to []*opensergo.MixedRule, err: %s", err.Error())
14+
return nil, datasource.NewError(datasource.ConvertSourceError, desc)
15+
}
16+
return mixedRules, nil
17+
}
18+
19+
// MixedPropertyUpdater load the newest MixedRule to downstream flow component.
20+
func MixedPropertyUpdater(data interface{}) error {
21+
mixedRule := data.(*MixedRule)
22+
23+
var errSlice []error
24+
flowRules := mixedRule.FlowRule
25+
if flowRules != nil {
26+
if err := datasource.FlowRulesUpdater(flowRules); err != nil {
27+
errSlice = append(errSlice, err)
28+
}
29+
}
30+
31+
hotSpotParamFlowRule := mixedRule.HotSpotParamFlowRule
32+
if hotSpotParamFlowRule != nil {
33+
if err := datasource.HotSpotParamRulesUpdater(hotSpotParamFlowRule); err != nil {
34+
errSlice = append(errSlice, err)
35+
}
36+
}
37+
38+
circuitBreakerRule := mixedRule.CircuitBreakerRule
39+
if circuitBreakerRule != nil {
40+
if err := datasource.CircuitBreakerRulesUpdater(circuitBreakerRule); err != nil {
41+
errSlice = append(errSlice, err)
42+
}
43+
}
44+
45+
systemRules := mixedRule.SystemRule
46+
if systemRules != nil {
47+
if err := datasource.SystemRulesUpdater(systemRules); err != nil {
48+
errSlice = append(errSlice, err)
49+
}
50+
}
51+
52+
isolationRule := mixedRule.IsolationRule
53+
if isolationRule != nil {
54+
if err := datasource.IsolationRulesUpdater(isolationRule); err != nil {
55+
errSlice = append(errSlice, err)
56+
}
57+
}
58+
59+
if errSlice == nil || len(errSlice) == 0 {
60+
return nil
61+
}
62+
63+
var errStr string
64+
for _, err := range errSlice {
65+
errStr = fmt.Sprintf(" | ") + fmt.Sprintf("%+v", err)
66+
}
67+
return datasource.NewError(
68+
datasource.UpdatePropertyError,
69+
fmt.Sprintf(errStr),
70+
)
71+
}

Diff for: pkg/datasource/opensergo/mixed_rule.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package opensergo
2+
3+
import (
4+
"github.com/alibaba/sentinel-golang/core/circuitbreaker"
5+
"github.com/alibaba/sentinel-golang/core/flow"
6+
"github.com/alibaba/sentinel-golang/core/hotspot"
7+
"github.com/alibaba/sentinel-golang/core/isolation"
8+
"github.com/alibaba/sentinel-golang/core/system"
9+
)
10+
11+
type MixedRule struct {
12+
FlowRule []flow.Rule
13+
HotSpotParamFlowRule []hotspot.Rule
14+
CircuitBreakerRule []circuitbreaker.Rule
15+
SystemRule []system.Rule
16+
IsolationRule []isolation.Rule
17+
}

Diff for: pkg/datasource/opensergo/opensergo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (ds OpenSergoDataSource) doUpdate() {
8686
func (ds OpenSergoDataSource) ReadSource() ([]byte, error) {
8787
dataMap := ds.opensergoRuleAggregator.dataMap
8888
logging.Info("[OpenSergo] Succeed to read source", "namespace", ds.namespace, "app", ds.app, "content", dataMap)
89-
bytes, err := json.Marshal(dataMap[RuleType_FlowRule])
89+
bytes, err := json.Marshal(dataMap)
9090
if err != nil {
9191
return nil, err
9292
}

Diff for: pkg/datasource/opensergo/opensergo_const.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
package opensergo
1616

1717
const (
18-
RuleType_FlowRule string = "FlowRule"
19-
RuleType_CircuitBreakerRule string = "DegradeRule"
20-
RuleType_SystemAdaptiveRule string = "SystemRule"
21-
RuleType_ParamFlowRule string = "ParamFlowRule"
18+
RuleType_FlowRule string = "FlowRule"
19+
RuleType_CircuitBreakerRule string = "CircuitBreakerRule"
20+
RuleType_SystemAdaptiveRule string = "SystemRule"
21+
RuleType_HotSpotParamFlowRule string = "HotSpotParamFlowRule"
22+
RuleType_IsolationRule string = "IsolationRule"
2223
)

0 commit comments

Comments
 (0)