Skip to content

Commit

Permalink
Rename flow.Rule's control startegy
Browse files Browse the repository at this point in the history
  • Loading branch information
louyuting committed Sep 6, 2020
1 parent fbf4a3f commit c05d483
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 101 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{
AllocationStrategy: flow.Default,
ControlEffect: flow.Reject,
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
},
},
{
Resource: "/api/:uid",
MetricType: flow.QPS,
Count: 0,
ControlStrategy: flow.ControlStrategy{
AllocationStrategy: flow.Default,
ControlEffect: flow.Reject,
TokenAllocation: 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{
AllocationStrategy: flow.Default,
ControlEffect: flow.Reject,
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
},
},
{
Resource: "/api/users/:id",
MetricType: flow.QPS,
Count: 0,
ControlStrategy: flow.ControlStrategy{
AllocationStrategy: flow.Default,
ControlEffect: flow.Reject,
TokenAllocation: 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{
AllocationStrategy: flow.Default,
ControlEffect: flow.Reject,
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand All @@ -50,8 +50,8 @@ func TestUnaryClientIntercept(t *testing.T) {
MetricType: flow.QPS,
Count: 0,
ControlStrategy: flow.ControlStrategy{
AllocationStrategy: flow.Default,
ControlEffect: flow.Reject,
TokenAllocation: 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{
AllocationStrategy: flow.Default,
ControlEffect: flow.Reject,
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand All @@ -102,8 +102,8 @@ func TestStreamClientIntercept(t *testing.T) {
MetricType: flow.QPS,
Count: 0,
ControlStrategy: flow.ControlStrategy{
AllocationStrategy: flow.Default,
ControlEffect: flow.Reject,
TokenAllocation: 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{
AllocationStrategy: flow.Default,
ControlEffect: flow.Reject,
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand All @@ -56,8 +56,8 @@ func TestStreamServerIntercept(t *testing.T) {
MetricType: flow.QPS,
Count: 0,
ControlStrategy: flow.ControlStrategy{
AllocationStrategy: flow.Default,
ControlEffect: flow.Reject,
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand All @@ -83,8 +83,8 @@ func TestUnaryServerIntercept(t *testing.T) {
MetricType: flow.QPS,
Count: 1,
ControlStrategy: flow.ControlStrategy{
AllocationStrategy: flow.Default,
ControlEffect: flow.Reject,
TokenAllocation: 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{
AllocationStrategy: flow.Default,
ControlEffect: flow.Reject,
TokenAllocation: 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{
AllocationStrategy: flow.Default,
ControlEffect: flow.Reject,
TokenAllocation: 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{
AllocationStrategy: flow.Default,
ControlEffect: flow.Reject,
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand All @@ -78,8 +78,8 @@ func TestServerLimiter(t *testing.T) {
MetricType: flow.QPS,
Count: 1,
ControlStrategy: flow.ControlStrategy{
AllocationStrategy: flow.Default,
ControlEffect: flow.Reject,
TokenAllocation: flow.Direct,
ControlBehavior: flow.Reject,
},
},
})
Expand Down
30 changes: 15 additions & 15 deletions core/flow/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,49 +39,49 @@ func (s MetricType) String() string {
type RelationStrategy int32

const (
// Direct means flow control by current resource directly.
Direct RelationStrategy = iota
// DirectResource means flow control by current resource directly.
DirectResource RelationStrategy = iota
// AssociatedResource means flow control by the associated resource rather than current resource.
AssociatedResource
)

func (s RelationStrategy) String() string {
switch s {
case Direct:
return "Direct"
case DirectResource:
return "DirectResource"
case AssociatedResource:
return "AssociatedResource"
default:
return "Undefined"
}
}

type AllocationStrategy int32
type TokenAllocation int32

const (
Default AllocationStrategy = iota
Direct TokenAllocation = iota
WarmUp
)

func (s AllocationStrategy) String() string {
func (s TokenAllocation) String() string {
switch s {
case Default:
return "Default"
case Direct:
return "Direct"
case WarmUp:
return "WarmUp"
default:
return "Undefined"
}
}

type ControlEffect int32
type ControlBehavior int32

const (
Reject ControlEffect = iota
Reject ControlBehavior = iota
Throttling
)

func (s ControlEffect) String() string {
func (s ControlBehavior) String() string {
switch s {
case Reject:
return "Reject"
Expand All @@ -93,12 +93,12 @@ func (s ControlEffect) String() string {
}

type ControlStrategy struct {
AllocationStrategy AllocationStrategy `json:"allocationStrategy"`
ControlEffect ControlEffect `json:"controlEffect"`
TokenAllocation TokenAllocation `json:"tokenAllocation"`
ControlBehavior ControlBehavior `json:"controlBehavior"`
}

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

// Rule describes the strategy of flow control.
Expand Down
28 changes: 14 additions & 14 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{
AllocationStrategy: Default,
ControlEffect: Reject,
TokenAllocation: Direct,
ControlBehavior: Reject,
}] = func(rule *Rule) *TrafficShapingController {
return NewTrafficShapingController(NewDefaultTrafficShapingCalculator(rule.Count), NewDefaultTrafficShapingChecker(rule), rule)
}
tcGenFuncMap[ControlStrategy{
AllocationStrategy: Default,
ControlEffect: Throttling,
TokenAllocation: Direct,
ControlBehavior: Throttling,
}] = func(rule *Rule) *TrafficShapingController {
return NewTrafficShapingController(NewDefaultTrafficShapingCalculator(rule.Count), NewThrottlingChecker(rule.MaxQueueingTimeMs), rule)
}
tcGenFuncMap[ControlStrategy{
AllocationStrategy: WarmUp,
ControlEffect: Reject,
TokenAllocation: WarmUp,
ControlBehavior: Reject,
}] = func(rule *Rule) *TrafficShapingController {
return NewTrafficShapingController(NewWarmUpTrafficShapingCalculator(rule), NewDefaultTrafficShapingChecker(rule), rule)
}
tcGenFuncMap[ControlStrategy{
AllocationStrategy: WarmUp,
ControlEffect: Throttling,
TokenAllocation: WarmUp,
ControlBehavior: Throttling,
}] = func(rule *Rule) *TrafficShapingController {
return NewTrafficShapingController(NewWarmUpTrafficShapingCalculator(rule), NewThrottlingChecker(rule.MaxQueueingTimeMs), rule)
}
Expand Down Expand Up @@ -171,10 +171,10 @@ func SetTrafficShapingGenerator(cs ControlStrategy, generator TrafficControllerG
if generator == nil {
return errors.New("nil generator")
}
if cs.AllocationStrategy >= Default && cs.AllocationStrategy <= WarmUp {
if cs.TokenAllocation >= Direct && cs.TokenAllocation <= WarmUp {
return errors.New("not allowed to replace the generator for default control strategy")
}
if cs.ControlEffect >= Reject && cs.ControlEffect <= Throttling {
if cs.ControlBehavior >= Reject && cs.ControlBehavior <= Throttling {
return errors.New("not allowed to replace the generator for default control strategy")
}
tcMux.Lock()
Expand All @@ -185,10 +185,10 @@ func SetTrafficShapingGenerator(cs ControlStrategy, generator TrafficControllerG
}

func RemoveTrafficShapingGenerator(cs ControlStrategy) error {
if cs.AllocationStrategy >= Default && cs.AllocationStrategy <= WarmUp {
if cs.TokenAllocation >= Direct && cs.TokenAllocation <= WarmUp {
return errors.New("not allowed to replace the generator for default control strategy")
}
if cs.ControlEffect >= Reject && cs.ControlEffect <= Throttling {
if cs.ControlBehavior >= Reject && cs.ControlBehavior <= Throttling {
return errors.New("not allowed to replace the generator for default control strategy")
}
tcMux.Lock()
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.AllocationStrategy < 0 || rule.ControlStrategy.ControlEffect < 0 {
if rule.ControlStrategy.TokenAllocation < 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.AllocationStrategy {
switch rule.ControlStrategy.TokenAllocation {
case WarmUp:
if rule.WarmUpPeriodSec <= 0 {
return errors.New("invalid warmUpPeriodSec")
Expand Down
Loading

0 comments on commit c05d483

Please sign in to comment.