Skip to content

Commit

Permalink
refine naming
Browse files Browse the repository at this point in the history
  • Loading branch information
louyuting committed Sep 7, 2020
1 parent e439882 commit 3110e48
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 85 deletions.
8 changes: 4 additions & 4 deletions adapter/echo/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ func initSentinel(t *testing.T) {
MetricType: flow.QPS,
Count: 1,
ControlStrategy: flow.ControlStrategy{
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
},
{
Resource: "/api/:uid",
MetricType: flow.QPS,
Count: 0,
ControlStrategy: flow.ControlStrategy{
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand Down
8 changes: 4 additions & 4 deletions adapter/gin/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ func initSentinel(t *testing.T) {
MetricType: flow.QPS,
Count: 1,
ControlStrategy: flow.ControlStrategy{
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
},
{
Resource: "/api/users/:id",
MetricType: flow.QPS,
Count: 0,
ControlStrategy: flow.ControlStrategy{
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand Down
16 changes: 8 additions & 8 deletions adapter/grpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func TestUnaryClientIntercept(t *testing.T) {
MetricType: flow.QPS,
Count: 1,
ControlStrategy: flow.ControlStrategy{
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand All @@ -50,8 +50,8 @@ func TestUnaryClientIntercept(t *testing.T) {
MetricType: flow.QPS,
Count: 0,
ControlStrategy: flow.ControlStrategy{
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand Down Expand Up @@ -79,8 +79,8 @@ func TestStreamClientIntercept(t *testing.T) {
MetricType: flow.QPS,
Count: 1,
ControlStrategy: flow.ControlStrategy{
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand All @@ -102,8 +102,8 @@ func TestStreamClientIntercept(t *testing.T) {
MetricType: flow.QPS,
Count: 0,
ControlStrategy: flow.ControlStrategy{
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand Down
16 changes: 8 additions & 8 deletions adapter/grpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func TestStreamServerIntercept(t *testing.T) {
MetricType: flow.QPS,
Count: 1,
ControlStrategy: flow.ControlStrategy{
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand All @@ -56,8 +56,8 @@ func TestStreamServerIntercept(t *testing.T) {
MetricType: flow.QPS,
Count: 0,
ControlStrategy: flow.ControlStrategy{
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand All @@ -83,8 +83,8 @@ func TestUnaryServerIntercept(t *testing.T) {
MetricType: flow.QPS,
Count: 1,
ControlStrategy: flow.ControlStrategy{
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand Down Expand Up @@ -114,8 +114,8 @@ func TestUnaryServerIntercept(t *testing.T) {
MetricType: flow.QPS,
Count: 0,
ControlStrategy: flow.ControlStrategy{
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand Down
4 changes: 2 additions & 2 deletions adapter/micro/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func TestClientLimiter(t *testing.T) {
MetricType: flow.QPS,
Count: 1,
ControlStrategy: flow.ControlStrategy{
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand Down
8 changes: 4 additions & 4 deletions adapter/micro/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func TestServerLimiter(t *testing.T) {
MetricType: flow.QPS,
Count: 1,
ControlStrategy: flow.ControlStrategy{
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand All @@ -78,8 +78,8 @@ func TestServerLimiter(t *testing.T) {
MetricType: flow.QPS,
Count: 1,
ControlStrategy: flow.ControlStrategy{
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand Down
12 changes: 6 additions & 6 deletions core/flow/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ func (s RelationStrategy) String() string {
}
}

type TokenAllocation int32
type TokenCalculateStrategy int32

const (
Direct TokenAllocation = iota
Direct TokenCalculateStrategy = iota
WarmUp
)

func (s TokenAllocation) String() string {
func (s TokenCalculateStrategy) String() string {
switch s {
case Direct:
return "Direct"
Expand Down Expand Up @@ -93,12 +93,12 @@ func (s ControlBehavior) String() string {
}

type ControlStrategy struct {
TokenAllocation TokenAllocation `json:"tokenAllocation"`
ControlBehavior ControlBehavior `json:"controlBehavior"`
TokenCalculateStrategy TokenCalculateStrategy `json:"tokenCalculateStrategy"`
ControlBehavior ControlBehavior `json:"controlBehavior"`
}

func (s ControlStrategy) String() string {
return fmt.Sprintf("{TokenAllocation: %s, ControlBehavior: %s}", s.TokenAllocation, s.ControlBehavior)
return fmt.Sprintf("{TokenCalculateStrategy: %s, ControlBehavior: %s}", s.TokenCalculateStrategy, s.ControlBehavior)
}

// Rule describes the strategy of flow control.
Expand Down
24 changes: 12 additions & 12 deletions core/flow/rule_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ var (
func init() {
// Initialize the traffic shaping controller generator map for existing control behaviors.
tcGenFuncMap[ControlStrategy{
TokenAllocation: Direct,
ControlBehavior: Reject,
TokenCalculateStrategy: Direct,
ControlBehavior: Reject,
}] = func(rule *Rule) *TrafficShapingController {
return NewTrafficShapingController(NewDirectTrafficShapingCalculator(rule.Count), NewDefaultTrafficShapingChecker(rule), rule)
}
tcGenFuncMap[ControlStrategy{
TokenAllocation: Direct,
ControlBehavior: Throttling,
TokenCalculateStrategy: Direct,
ControlBehavior: Throttling,
}] = func(rule *Rule) *TrafficShapingController {
return NewTrafficShapingController(NewDirectTrafficShapingCalculator(rule.Count), NewThrottlingChecker(rule.MaxQueueingTimeMs), rule)
}
tcGenFuncMap[ControlStrategy{
TokenAllocation: WarmUp,
ControlBehavior: Reject,
TokenCalculateStrategy: WarmUp,
ControlBehavior: Reject,
}] = func(rule *Rule) *TrafficShapingController {
return NewTrafficShapingController(NewWarmUpTrafficShapingCalculator(rule), NewDefaultTrafficShapingChecker(rule), rule)
}
tcGenFuncMap[ControlStrategy{
TokenAllocation: WarmUp,
ControlBehavior: Throttling,
TokenCalculateStrategy: WarmUp,
ControlBehavior: Throttling,
}] = func(rule *Rule) *TrafficShapingController {
return NewTrafficShapingController(NewWarmUpTrafficShapingCalculator(rule), NewThrottlingChecker(rule.MaxQueueingTimeMs), rule)
}
Expand Down Expand Up @@ -171,7 +171,7 @@ func SetTrafficShapingGenerator(cs ControlStrategy, generator TrafficControllerG
if generator == nil {
return errors.New("nil generator")
}
if cs.TokenAllocation >= Direct && cs.TokenAllocation <= WarmUp {
if cs.TokenCalculateStrategy >= Direct && cs.TokenCalculateStrategy <= WarmUp {
return errors.New("not allowed to replace the generator for default control strategy")
}
if cs.ControlBehavior >= Reject && cs.ControlBehavior <= Throttling {
Expand All @@ -185,7 +185,7 @@ func SetTrafficShapingGenerator(cs ControlStrategy, generator TrafficControllerG
}

func RemoveTrafficShapingGenerator(cs ControlStrategy) error {
if cs.TokenAllocation >= Direct && cs.TokenAllocation <= WarmUp {
if cs.TokenCalculateStrategy >= Direct && cs.TokenCalculateStrategy <= WarmUp {
return errors.New("not allowed to replace the generator for default control strategy")
}
if cs.ControlBehavior >= Reject && cs.ControlBehavior <= Throttling {
Expand Down Expand Up @@ -258,7 +258,7 @@ func IsValidRule(rule *Rule) error {
if rule.RelationStrategy < 0 {
return errors.New("invalid relation strategy")
}
if rule.ControlStrategy.TokenAllocation < 0 || rule.ControlStrategy.ControlBehavior < 0 {
if rule.ControlStrategy.TokenCalculateStrategy < 0 || rule.ControlStrategy.ControlBehavior < 0 {
return errors.New("invalid control strategy")
}

Expand All @@ -270,7 +270,7 @@ func IsValidRule(rule *Rule) error {
}

func checkControlStrategyField(rule *Rule) error {
switch rule.ControlStrategy.TokenAllocation {
switch rule.ControlStrategy.TokenCalculateStrategy {
case WarmUp:
if rule.WarmUpPeriodSec <= 0 {
return errors.New("invalid warmUpPeriodSec")
Expand Down
32 changes: 16 additions & 16 deletions core/flow/rule_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ func TestSetAndRemoveTrafficShapingGenerator(t *testing.T) {
tsc := &TrafficShapingController{}

err := SetTrafficShapingGenerator(ControlStrategy{
TokenAllocation: Direct,
ControlBehavior: Reject,
TokenCalculateStrategy: Direct,
ControlBehavior: Reject,
}, func(_ *Rule) *TrafficShapingController {
return tsc
})
assert.Error(t, err, "default control behaviors are not allowed to be modified")
err = RemoveTrafficShapingGenerator(ControlStrategy{
TokenAllocation: Direct,
ControlBehavior: Reject,
TokenCalculateStrategy: Direct,
ControlBehavior: Reject,
})
assert.Error(t, err, "default control behaviors are not allowed to be removed")

cs := ControlStrategy{
TokenAllocation: TokenAllocation(111),
ControlBehavior: ControlBehavior(112),
TokenCalculateStrategy: TokenCalculateStrategy(111),
ControlBehavior: ControlBehavior(112),
}
err = SetTrafficShapingGenerator(cs, func(_ *Rule) *TrafficShapingController {
return tsc
Expand Down Expand Up @@ -57,8 +57,8 @@ func TestSetAndRemoveTrafficShapingGenerator(t *testing.T) {
func TestIsValidFlowRule(t *testing.T) {
badRule1 := &Rule{ID: 1, Count: 1, MetricType: QPS, Resource: ""}
badRule2 := &Rule{ID: 1, Count: -1.9, MetricType: QPS, Resource: "test"}
badRule3 := &Rule{Count: 5, MetricType: QPS, Resource: "test", ControlStrategy: ControlStrategy{TokenAllocation: WarmUp, ControlBehavior: Reject}}
goodRule1 := &Rule{Count: 10, MetricType: QPS, Resource: "test", ControlStrategy: ControlStrategy{TokenAllocation: WarmUp, ControlBehavior: Throttling}, WarmUpPeriodSec: 10}
badRule3 := &Rule{Count: 5, MetricType: QPS, Resource: "test", ControlStrategy: ControlStrategy{TokenCalculateStrategy: WarmUp, ControlBehavior: Reject}}
goodRule1 := &Rule{Count: 10, MetricType: QPS, Resource: "test", ControlStrategy: ControlStrategy{TokenCalculateStrategy: WarmUp, ControlBehavior: Throttling}, WarmUpPeriodSec: 10}

assert.Error(t, IsValidRule(badRule1))
assert.Error(t, IsValidRule(badRule2))
Expand All @@ -79,8 +79,8 @@ func TestGetRules(t *testing.T) {
Count: 0,
RelationStrategy: 0,
ControlStrategy: ControlStrategy{
TokenAllocation: Direct,
ControlBehavior: Reject,
TokenCalculateStrategy: Direct,
ControlBehavior: Reject,
},
RefResource: "",
WarmUpPeriodSec: 0,
Expand All @@ -94,8 +94,8 @@ func TestGetRules(t *testing.T) {
Count: 0,
RelationStrategy: 0,
ControlStrategy: ControlStrategy{
TokenAllocation: Direct,
ControlBehavior: Throttling,
TokenCalculateStrategy: Direct,
ControlBehavior: Throttling,
},
RefResource: "",
WarmUpPeriodSec: 0,
Expand Down Expand Up @@ -131,8 +131,8 @@ func TestGetRules(t *testing.T) {
Count: 0,
RelationStrategy: 0,
ControlStrategy: ControlStrategy{
TokenAllocation: Direct,
ControlBehavior: Reject,
TokenCalculateStrategy: Direct,
ControlBehavior: Reject,
},
RefResource: "",
WarmUpPeriodSec: 0,
Expand All @@ -146,8 +146,8 @@ func TestGetRules(t *testing.T) {
Count: 0,
RelationStrategy: 0,
ControlStrategy: ControlStrategy{
TokenAllocation: Direct,
ControlBehavior: Throttling,
TokenCalculateStrategy: Direct,
ControlBehavior: Throttling,
},
RefResource: "",
WarmUpPeriodSec: 0,
Expand Down
4 changes: 2 additions & 2 deletions example/qps/qps_limit_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func main() {
MetricType: flow.QPS,
Count: 10,
ControlStrategy: flow.ControlStrategy{
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand Down
4 changes: 2 additions & 2 deletions example/qps/qps_limit_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func doTest() {
MetricType: flow.QPS,
Count: 100,
ControlStrategy: flow.ControlStrategy{
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand Down
4 changes: 2 additions & 2 deletions example/warm_up/qps_warm_up_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func main() {
MetricType: flow.QPS,
Count: 100,
ControlStrategy: flow.ControlStrategy{
TokenAllocation: flow.WarmUp,
ControlBehavior: flow.Reject,
TokenCalculateStrategy: flow.WarmUp,
ControlBehavior: flow.Reject,
},
WarmUpPeriodSec: 10,
WarmUpColdFactor: 3,
Expand Down
4 changes: 2 additions & 2 deletions example/warm_up/qps_warm_up_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func doTest() {
MetricType: flow.QPS,
Count: 100,
ControlStrategy: flow.ControlStrategy{
TokenAllocation: flow.WarmUp,
ControlBehavior: flow.Reject,
TokenCalculateStrategy: flow.WarmUp,
ControlBehavior: flow.Reject,
},
WarmUpPeriodSec: 10,
},
Expand Down
Loading

0 comments on commit 3110e48

Please sign in to comment.