Skip to content
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

Break down flow.ControlBehavior to flow.TokenCalculateStrategy and flow.ControlBehavior #223

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions adapter/echo/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ func initSentinel(t *testing.T) {

_, err = flow.LoadRules([]*flow.Rule{
{
Resource: "GET:/ping",
MetricType: flow.QPS,
Count: 1,
ControlBehavior: flow.Reject,
Resource: "GET:/ping",
MetricType: flow.QPS,
Count: 1,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
{
Resource: "/api/:uid",
MetricType: flow.QPS,
Count: 0,
ControlBehavior: flow.Reject,
Resource: "/api/:uid",
MetricType: flow.QPS,
Count: 0,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
})
if err != nil {
Expand Down
18 changes: 10 additions & 8 deletions adapter/gin/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ func initSentinel(t *testing.T) {

_, err = flow.LoadRules([]*flow.Rule{
{
Resource: "GET:/ping",
MetricType: flow.QPS,
Count: 1,
ControlBehavior: flow.Reject,
Resource: "GET:/ping",
MetricType: flow.QPS,
Count: 1,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
{
Resource: "/api/users/:id",
MetricType: flow.QPS,
Count: 0,
ControlBehavior: flow.Reject,
Resource: "/api/users/:id",
MetricType: flow.QPS,
Count: 0,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
})
if err != nil {
Expand Down
36 changes: 20 additions & 16 deletions adapter/grpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ func TestUnaryClientIntercept(t *testing.T) {
t.Run("success", func(t *testing.T) {
var _, err = flow.LoadRules([]*flow.Rule{
{
Resource: "client:" + method,
MetricType: flow.QPS,
Count: 1,
ControlBehavior: flow.Reject,
Resource: "client:" + method,
MetricType: flow.QPS,
Count: 1,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
})
assert.Nil(t, err)
Expand All @@ -43,10 +44,11 @@ func TestUnaryClientIntercept(t *testing.T) {
t.Run("fail", func(t *testing.T) {
var _, err = flow.LoadRules([]*flow.Rule{
{
Resource: "client:" + method,
MetricType: flow.QPS,
Count: 0,
ControlBehavior: flow.Reject,
Resource: "client:" + method,
MetricType: flow.QPS,
Count: 0,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
})
assert.Nil(t, err)
Expand All @@ -69,10 +71,11 @@ func TestStreamClientIntercept(t *testing.T) {
t.Run("success", func(t *testing.T) {
var _, err = flow.LoadRules([]*flow.Rule{
{
Resource: "client:/grpc.testing.TestService/StreamingOutputCall",
MetricType: flow.QPS,
Count: 1,
ControlBehavior: flow.Reject,
Resource: "client:/grpc.testing.TestService/StreamingOutputCall",
MetricType: flow.QPS,
Count: 1,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
})
assert.Nil(t, err)
Expand All @@ -89,10 +92,11 @@ func TestStreamClientIntercept(t *testing.T) {
t.Run("fail", func(t *testing.T) {
var _, err = flow.LoadRules([]*flow.Rule{
{
Resource: "client:/grpc.testing.TestService/StreamingOutputCall",
MetricType: flow.QPS,
Count: 0,
ControlBehavior: flow.Reject,
Resource: "client:/grpc.testing.TestService/StreamingOutputCall",
MetricType: flow.QPS,
Count: 0,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
})
assert.Nil(t, err)
Expand Down
36 changes: 20 additions & 16 deletions adapter/grpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ func TestStreamServerIntercept(t *testing.T) {
t.Run("success", func(t *testing.T) {
var _, err = flow.LoadRules([]*flow.Rule{
{
Resource: "/grpc.testing.TestService/StreamingInputCall",
MetricType: flow.QPS,
Count: 1,
ControlBehavior: flow.Reject,
Resource: "/grpc.testing.TestService/StreamingInputCall",
MetricType: flow.QPS,
Count: 1,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
})
assert.Nil(t, err)
Expand All @@ -49,10 +50,11 @@ func TestStreamServerIntercept(t *testing.T) {
t.Run("fail", func(t *testing.T) {
var _, err = flow.LoadRules([]*flow.Rule{
{
Resource: "/grpc.testing.TestService/StreamingInputCall",
MetricType: flow.QPS,
Count: 0,
ControlBehavior: flow.Reject,
Resource: "/grpc.testing.TestService/StreamingInputCall",
MetricType: flow.QPS,
Count: 0,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
})
assert.Nil(t, err)
Expand All @@ -73,10 +75,11 @@ func TestUnaryServerIntercept(t *testing.T) {
t.Run("success", func(t *testing.T) {
var _, err = flow.LoadRules([]*flow.Rule{
{
Resource: "/grpc.testing.TestService/UnaryCall",
MetricType: flow.QPS,
Count: 1,
ControlBehavior: flow.Reject,
Resource: "/grpc.testing.TestService/UnaryCall",
MetricType: flow.QPS,
Count: 1,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
})
assert.Nil(t, err)
Expand All @@ -101,10 +104,11 @@ func TestUnaryServerIntercept(t *testing.T) {
t.Run("fail", func(t *testing.T) {
var _, err = flow.LoadRules([]*flow.Rule{
{
Resource: "/grpc.testing.TestService/UnaryCall",
MetricType: flow.QPS,
Count: 0,
ControlBehavior: flow.Reject,
Resource: "/grpc.testing.TestService/UnaryCall",
MetricType: flow.QPS,
Count: 0,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
})
assert.Nil(t, err)
Expand Down
9 changes: 5 additions & 4 deletions adapter/micro/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ func TestClientLimiter(t *testing.T) {
t.Run("success", func(t *testing.T) {
var _, err = flow.LoadRules([]*flow.Rule{
{
Resource: req.Method(),
MetricType: flow.QPS,
Count: 1,
ControlBehavior: flow.Reject,
Resource: req.Method(),
MetricType: flow.QPS,
Count: 1,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
})
assert.Nil(t, err)
Expand Down
18 changes: 10 additions & 8 deletions adapter/micro/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ func TestServerLimiter(t *testing.T) {

_, err = flow.LoadRules([]*flow.Rule{
{
Resource: req.Method(),
MetricType: flow.QPS,
Count: 1,
ControlBehavior: flow.Reject,
Resource: req.Method(),
MetricType: flow.QPS,
Count: 1,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
})

Expand All @@ -71,10 +72,11 @@ func TestServerLimiter(t *testing.T) {
t.Run("success", func(t *testing.T) {
var _, err = flow.LoadRules([]*flow.Rule{
{
Resource: req.Method(),
MetricType: flow.QPS,
Count: 1,
ControlBehavior: flow.Reject,
Resource: req.Method(),
MetricType: flow.QPS,
Count: 1,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
})
assert.Nil(t, err)
Expand Down
92 changes: 70 additions & 22 deletions core/flow/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,103 @@ const (
QPS
)

func (s MetricType) String() string {
switch s {
case Concurrency:
return "Concurrency"
case QPS:
return "QPS"
default:
return "Undefined"
}
}

// RelationStrategy indicates the flow control strategy based on the relation of invocations.
type RelationStrategy int32

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

// ControlBehavior indicates the traffic shaping behaviour.
func (s RelationStrategy) String() string {
switch s {
case CurrentResource:
return "CurrentResource"
case AssociatedResource:
return "AssociatedResource"
default:
return "Undefined"
}
}

type TokenCalculateStrategy int32

const (
Direct TokenCalculateStrategy = iota
WarmUp
)

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

type ControlBehavior int32

const (
Reject ControlBehavior = iota
WarmUp
Throttling
WarmUpThrottling
)

func (s ControlBehavior) String() string {
switch s {
case Reject:
return "Reject"
case Throttling:
return "Throttling"
default:
return "Undefined"
}
}

// Rule describes the strategy of flow control.
type Rule struct {
// ID represents the unique ID of the rule (optional).
ID uint64 `json:"id,omitempty"`

// Resource represents the resource name.
Resource string `json:"resource"`
MetricType MetricType `json:"metricType"`
// Count represents the threshold.
Count float64 `json:"count"`
ControlBehavior ControlBehavior `json:"controlBehavior"`

RelationStrategy RelationStrategy `json:"relationStrategy"`
RefResource string `json:"refResource"`
MaxQueueingTimeMs uint32 `json:"maxQueueingTimeMs"`
WarmUpPeriodSec uint32 `json:"warmUpPeriodSec"`
WarmUpColdFactor uint32 `json:"warmUpColdFactor"`
Resource string `json:"resource"`
MetricType MetricType `json:"metricType"`
TokenCalculateStrategy TokenCalculateStrategy `json:"tokenCalculateStrategy"`
ControlBehavior ControlBehavior `json:"controlBehavior"`
Count float64 `json:"count"`
RelationStrategy RelationStrategy `json:"relationStrategy"`
RefResource string `json:"refResource"`
MaxQueueingTimeMs uint32 `json:"maxQueueingTimeMs"`
WarmUpPeriodSec uint32 `json:"warmUpPeriodSec"`
WarmUpColdFactor uint32 `json:"warmUpColdFactor"`
}

func (f *Rule) String() string {
b, err := json.Marshal(f)
func (r *Rule) String() string {
b, err := json.Marshal(r)
if err != nil {
// Return the fallback string
return fmt.Sprintf("Rule{resource=%s, id=%d, metricType=%d, threshold=%.2f}",
f.Resource, f.ID, f.MetricType, f.Count)
return fmt.Sprintf("Rule{Resource=%s, MetricType=%s, TokenCalculateStrategy=%s, ControlBehavior=%s, "+
"Count=%.2f, RelationStrategy=%s, WarmUpPeriodSec=%d, WarmUpColdFactor=%d, MaxQueueingTimeMs=%d}",
r.Resource, r.MetricType, r.TokenCalculateStrategy, r.ControlBehavior, r.Count, r.RelationStrategy, r.WarmUpPeriodSec, r.WarmUpColdFactor, r.MaxQueueingTimeMs)
}
return string(b)
}

func (f *Rule) ResourceName() string {
return f.Resource
func (r *Rule) ResourceName() string {
return r.Resource
}
Loading