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

[ISSUES 111]Flow rule cache/check #253

Closed
wants to merge 10 commits into from
2 changes: 1 addition & 1 deletion adapter/micro/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestServerLimiter(t *testing.T) {
_, err = flow.LoadRules([]*flow.Rule{
{
Resource: req.Method(),
Threshold: 1,
Threshold: 0,
TokenCalculateStrategy: flow.Direct,
ControlBehavior: flow.Reject,
},
Expand Down
18 changes: 13 additions & 5 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var entryOptsPool = sync.Pool{
return &EntryOptions{
resourceType: base.ResTypeCommon,
entryType: base.Outbound,
acquireCount: 1,
batchCount: 1,
flag: 0,
slotChain: nil,
args: nil,
Expand All @@ -24,7 +24,7 @@ var entryOptsPool = sync.Pool{
type EntryOptions struct {
resourceType base.ResourceType
entryType base.TrafficType
acquireCount uint32
batchCount uint32
flag int32
slotChain *base.SlotChain
args []interface{}
Expand All @@ -34,7 +34,7 @@ type EntryOptions struct {
func (o *EntryOptions) Reset() {
o.resourceType = base.ResTypeCommon
o.entryType = base.Outbound
o.acquireCount = 1
o.batchCount = 1
o.flag = 0
o.slotChain = nil
o.args = nil
Expand All @@ -57,10 +57,18 @@ func WithTrafficType(entryType base.TrafficType) EntryOption {
}
}

// DEPRECATED: use WithBatchCount instead.
// WithAcquireCount sets the resource entry with the given batch count (by default 1).
func WithAcquireCount(acquireCount uint32) EntryOption {
return func(opts *EntryOptions) {
opts.acquireCount = acquireCount
opts.batchCount = acquireCount
}
}

// WithBatchCount sets the resource entry with the given batch count (by default 1).
func WithBatchCount(batchCount uint32) EntryOption {
return func(opts *EntryOptions) {
opts.batchCount = batchCount
}
}

Expand Down Expand Up @@ -129,7 +137,7 @@ func entry(resource string, options *EntryOptions) (*base.SentinelEntry, *base.B
// Get context from pool.
ctx := sc.GetPooledContext()
ctx.Resource = rw
ctx.Input.AcquireCount = options.acquireCount
ctx.Input.BatchCount = options.batchCount
ctx.Input.Flag = options.flag
if len(options.args) != 0 {
ctx.Input.Args = options.args
Expand Down
4 changes: 2 additions & 2 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Test_entryWithArgsAndChainPass(t *testing.T) {
entry, b := entry("abc", &EntryOptions{
resourceType: base.ResTypeCommon,
entryType: base.Inbound,
acquireCount: 1,
batchCount: 1,
flag: 0,
slotChain: sc,
})
Expand Down Expand Up @@ -112,7 +112,7 @@ func Test_entryWithArgsAndChainBlock(t *testing.T) {
entry, b := entry("abc", &EntryOptions{
resourceType: base.ResTypeCommon,
entryType: base.Inbound,
acquireCount: 1,
batchCount: 1,
flag: 0,
slotChain: sc,
})
Expand Down
8 changes: 4 additions & 4 deletions core/base/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ func NewEmptyEntryContext() *EntryContext {

// The input data of sentinel
type SentinelInput struct {
AcquireCount uint32
Flag int32
Args []interface{}
BatchCount uint32
Flag int32
Args []interface{}
// store some values in this context when calling context in slot.
Attachments map[interface{}]interface{}
}

func (i *SentinelInput) reset() {
i.AcquireCount = 1
i.BatchCount = 1
i.Flag = 0
if len(i.Args) != 0 {
i.Args = make([]interface{}, 0)
Expand Down
8 changes: 4 additions & 4 deletions core/base/slot_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ func NewSlotChain() *SlotChain {
ctx.RuleCheckResult = NewTokenResultPass()
ctx.Data = make(map[interface{}]interface{})
ctx.Input = &SentinelInput{
AcquireCount: 1,
Flag: 0,
Args: make([]interface{}, 0),
Attachments: make(map[interface{}]interface{}),
BatchCount: 1,
Flag: 0,
Args: make([]interface{}, 0),
Attachments: make(map[interface{}]interface{}),
}
return ctx
},
Expand Down
24 changes: 12 additions & 12 deletions core/base/slot_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ func TestSlotChain_Entry_Pass_And_Exit(t *testing.T) {
ctx.SetEntry(NewSentinelEntry(ctx, rw, sc))
ctx.StatNode = &StatNodeMock{}
ctx.Input = &SentinelInput{
AcquireCount: 1,
Flag: 0,
Args: nil,
Attachments: nil,
BatchCount: 1,
Flag: 0,
Args: nil,
Attachments: nil,
}

ps1 := &prepareSlotMock{}
Expand Down Expand Up @@ -266,10 +266,10 @@ func TestSlotChain_Entry_Block(t *testing.T) {
ctx.Resource = rw
ctx.StatNode = &StatNodeMock{}
ctx.Input = &SentinelInput{
AcquireCount: 1,
Flag: 0,
Args: nil,
Attachments: nil,
BatchCount: 1,
Flag: 0,
Args: nil,
Attachments: nil,
}

rbs := &prepareSlotMock{}
Expand Down Expand Up @@ -324,10 +324,10 @@ func TestSlotChain_Entry_With_Panic(t *testing.T) {
statNodeMock.On("AddErrorRequest", mock.Anything).Return()
ctx.StatNode = statNodeMock
ctx.Input = &SentinelInput{
AcquireCount: 1,
Flag: 0,
Args: nil,
Attachments: nil,
BatchCount: 1,
Flag: 0,
Args: nil,
Attachments: nil,
}

rbs := &badPrepareSlotMock{}
Expand Down
Loading