Skip to content

Add result token pool (#137) #142

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

Merged
merged 3 commits into from
Apr 29, 2020
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
10 changes: 8 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ func Entry(resource string, opts ...EntryOption) (*base.SentinelEntry, *base.Blo
}

func entry(resource string, options *EntryOptions) (*base.SentinelEntry, *base.BlockError) {
var r *base.TokenResult
defer func() {
if r != nil {
base.RefurbishTokenResult(r)
}
}()

rw := base.NewResourceWrapper(resource, options.resourceType, options.entryType)
sc := options.slotChain

Expand All @@ -104,8 +111,7 @@ func entry(resource string, options *EntryOptions) (*base.SentinelEntry, *base.B
}

e := base.NewSentinelEntry(ctx, rw, sc)

r := sc.Entry(ctx)
r = sc.Entry(ctx)
if r == nil {
// This indicates internal error in some slots, so just pass
return e, nil
Expand Down
50 changes: 38 additions & 12 deletions core/base/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package base

import (
"fmt"
"sync"
)

type BlockType int32
Expand All @@ -13,6 +14,12 @@ const (
BlockTypeSystemFlow
)

var resultPool = sync.Pool{
New: func() interface{} {
return NewTokenResultEmpty()
},
}

func (t BlockType) String() string {
switch t {
case BlockTypeUnknown:
Expand Down Expand Up @@ -73,26 +80,45 @@ func (r *TokenResult) String() string {
return fmt.Sprintf("TokenResult{status=%d, blockErr=%s, waitMs=%d}", r.status, blockMsg, r.waitMs)
}

func NewTokenResultEmpty() *TokenResult {
return &TokenResult{
status: ResultStatusPass,
blockErr: nil,
waitMs: 0,
}
}

func NewTokenResultPass() *TokenResult {
return &TokenResult{status: ResultStatusPass, waitMs: 0}
return resultPool.Get().(*TokenResult)
}

func NewTokenResultBlocked(blockType BlockType, blockMsg string) *TokenResult {
return &TokenResult{
status: ResultStatusBlocked,
blockErr: NewBlockError(blockType, blockMsg),
waitMs: 0,
}
result := resultPool.Get().(*TokenResult)
result.status = ResultStatusBlocked
result.blockErr = NewBlockError(blockType, blockMsg)
return result
}

func NewTokenResultBlockedWithCause(blockType BlockType, blockMsg string, rule SentinelRule, snapshot interface{}) *TokenResult {
return &TokenResult{
status: ResultStatusBlocked,
blockErr: NewBlockErrorWithCause(blockType, blockMsg, rule, snapshot),
waitMs: 0,
}
result := resultPool.Get().(*TokenResult)
result.status = ResultStatusBlocked
result.blockErr = NewBlockErrorWithCause(blockType, blockMsg, rule, snapshot)
return result
}

func NewTokenResultShouldWait(waitMs uint64) *TokenResult {
return &TokenResult{status: ResultStatusShouldWait, waitMs: waitMs}
result := resultPool.Get().(*TokenResult)
result.status = ResultStatusShouldWait
result.waitMs = waitMs
return result
}

func RefurbishTokenResult(result *TokenResult) {
if result != nil {
result.status = ResultStatusPass
result.blockErr = nil
result.waitMs = 0

resultPool.Put(result)
}
}
29 changes: 29 additions & 0 deletions core/base/result_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package base

import (
"testing"
)

func BenchmarkWithPool(b *testing.B) {
var result *TokenResult
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for j := 0; j < 10000; j++ {
result = NewTokenResultPass()
RefurbishTokenResult(result)
}
}
}

func BenchmarkWithNoPool(b *testing.B) {
var result *TokenResult
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for j := 0; j < 10000; j++ {
result = NewTokenResultEmpty()
result.status = ResultStatusPass
result.blockErr = nil
result.waitMs = 0
}
}
}
12 changes: 9 additions & 3 deletions core/base/slot_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,26 @@ func (sc *SlotChain) Entry(ctx *EntryContext) *TokenResult {

// execute rule based checking slot
rcs := sc.ruleChecks
ruleCheckRet := NewTokenResultPass()
var ruleCheckRet *TokenResult
if len(rcs) > 0 {
for _, s := range rcs {
if ruleCheckRet != nil {
RefurbishTokenResult(ruleCheckRet)
}

sr := s.Check(ctx)
ctx.Output.LastResult = sr
ruleCheckRet = sr
// check slot result
if sr.status == ResultStatusBlocked {
ruleCheckRet = sr
break
}

// This slot passed, continue.
}
}
if ruleCheckRet == nil {
ruleCheckRet = NewTokenResultPass()
}
ctx.Output.LastResult = ruleCheckRet

// execute statistic slot
Expand Down
2 changes: 2 additions & 0 deletions core/circuit_breaker/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ func checkPass(ctx *base.EntryContext) *base.TokenResult {
// Handle waiting action.
time.Sleep(time.Duration(waitMs) * time.Millisecond)
}
base.RefurbishTokenResult(r)
continue
}
base.RefurbishTokenResult(r)
}
return base.NewTokenResultPass()
}
2 changes: 2 additions & 0 deletions core/flow/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ func (s *FlowSlot) Check(ctx *base.EntryContext) *base.TokenResult {
// Handle waiting action.
time.Sleep(time.Duration(waitMs) * time.Millisecond)
}
base.RefurbishTokenResult(r)
continue
}
base.RefurbishTokenResult(r)
}
return base.NewTokenResultPass()
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ require (
github.com/fsnotify/fsnotify v1.4.7
github.com/gin-gonic/gin v1.5.0
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/labstack/echo/v4 v4.1.15
github.com/google/uuid v1.1.1
github.com/labstack/echo/v4 v4.1.15
github.com/pkg/errors v0.8.1
github.com/shirou/gopsutil v2.19.12+incompatible
github.com/stretchr/testify v1.4.0
go.uber.org/multierr v1.5.0
golang.org/x/tools v0.0.0-20200428021058-7ae4988eb4d9 // indirect
google.golang.org/grpc v1.22.1
gopkg.in/yaml.v2 v2.2.2
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ github.com/valyala/fasttemplate v1.1.0 h1:RZqt0yGBsps8NGvLSGW804QQqCUYYLsaOjTVHy
github.com/valyala/fasttemplate v1.1.0/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
github.com/vmware/govmomi v0.18.0/go.mod h1:URlwyTFZX72RmxtxuaFL2Uj3fD1JTvZdx59bHWk6aFU=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/zouyx/agollo v0.0.0-20191114083447-dde9fc9f35b8/go.mod h1:S1cAa98KMFv4Sa8SbJ6ZtvOmf0VlgH0QJ1gXI0lBfBY=
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/etcd v3.3.13+incompatible/go.mod h1:yaeTdrJi5lOmYerz05bd8+V7KubZs8YSFZfzsF9A6aI=
Expand All @@ -360,13 +361,16 @@ golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d h1:1ZiEyfaQIg3Qh0EoqpwAakHVhecoE5wlSg5GjnafJGw=
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand All @@ -388,6 +392,7 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -422,7 +427,13 @@ golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgw
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5 h1:hKsoRgsbwY1NafxrwTs+k64bikrLBkAgPir1TNCj3Zs=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200428021058-7ae4988eb4d9 h1:nSSuQTxk9hjYf2koTs9mHZtYm2pu7Yt8WuIeKOrCWNI=
golang.org/x/tools v0.0.0-20200428021058-7ae4988eb4d9/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.0.0-20180829000535-087779f1d2c9/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 h1:Nw54tB0rB7hY/N0NQvRW8DG4Yk3Q6T9cu9RcFQDu1tc=
Expand Down