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

增加hack包的基准测试方法,优化部分if-else代码,调整了限流部分令牌获取的顺序 #186

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 12 additions & 13 deletions pkg/proxy/dispatcher_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,32 @@ func (r *dispatcher) watch() {

go r.readyToReceiveWatchEvent()
err := r.store.Watch(r.watchEventC, r.watchStopC)
log.Errorf("router watch failed, errors:\n%+v",
err)
log.Errorf("router watch failed, errors:\n%+v", err)
}

func (r *dispatcher) readyToReceiveWatchEvent() {
for {
evt := <-r.watchEventC

if evt.Src == store.EventSrcCluster {
switch evt.Src {
case store.EventSrcCluster:
r.doClusterEvent(evt)
} else if evt.Src == store.EventSrcServer {
case store.EventSrcServer:
r.doServerEvent(evt)
} else if evt.Src == store.EventSrcBind {
case store.EventSrcBind:
r.doBindEvent(evt)
} else if evt.Src == store.EventSrcAPI {
case store.EventSrcAPI:
r.doAPIEvent(evt)
} else if evt.Src == store.EventSrcRouting {
case store.EventSrcRouting:
r.doRoutingEvent(evt)
} else if evt.Src == store.EventSrcProxy {
case store.EventSrcProxy:
r.doProxyEvent(evt)
} else if evt.Src == store.EventSrcPlugin {
case store.EventSrcPlugin:
r.doPluginEvent(evt)
} else if evt.Src == store.EventSrcApplyPlugin {
case store.EventSrcApplyPlugin:
r.doApplyPluginEvent(evt)
} else if evt.Src == eventSrcStatusChanged {
case eventSrcStatusChanged:
r.doStatusChangedEvent(evt)
} else {
default:
log.Warnf("unknown event <%+v>", evt)
}
}
Expand Down
17 changes: 12 additions & 5 deletions pkg/proxy/filter_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (

const (
// besides checking token is legitimate or not, it checks whether token exists in redis
actionTokenInRedis string = "token_in_redis"
actionTokenInRedis string = "token_in_redis"
// update token's TTL
actionRenewByRaw string = "renew_by_raw"
actionRenewByRaw string = "renew_by_raw"
// update token's TTL and in the same time put new token in redis, previous token invalid
actionRenewByRedis string = "renew_by_redis"
actionRenewByRedis string = "renew_by_redis"
// fetch fields from token and put them in header which is redirected to a backend server who is unbeknownst to JWT
actionFetchToHeader string = "fetch_to_header"
actionFetchToCookie string = "fetch_to_cookie"
Expand Down Expand Up @@ -96,7 +96,10 @@ func newJWTFilter(file string) (filter.Filter, error) {
}

f.initRedisPool()
f.initTokenLookup()
err = f.initTokenLookup()
if err != nil {
return nil, err
}
return f, nil
}

Expand Down Expand Up @@ -190,15 +193,19 @@ func (f *JWTFilter) initSigningMethod() error {
return nil
}

func (f *JWTFilter) initTokenLookup() {
func (f *JWTFilter) initTokenLookup() error {
parts := strings.Split(f.cfg.TokenLookup, ":")
if len(parts) < 2 {
return fmt.Errorf("TokenLookup should contain : ")
}
f.getter = jwtFromHeader(parts[1], f.cfg.AuthSchema)
switch parts[0] {
case "query":
f.getter = jwtFromQuery(parts[1])
case "cookie":
f.getter = jwtFromCookie(parts[1])
}
return nil
}

func (f *JWTFilter) initActions() error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ func (p *Proxy) doProxy(dn *dispatchNode, adjustH func(*proxyContext)) {
break
}

// retry with strategiess
// retry with strategies
retry := dn.retryStrategy()
if times >= retry.MaxTimes {
log.Infof("%s: dispatch node %d sent times over the max %d",
Expand Down
7 changes: 5 additions & 2 deletions pkg/proxy/rate_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ func newRateLimiter(max int64, option metapb.RateLimitOption) *rateLimiter {
}

func (l *rateLimiter) do(count int64) bool {
if l.limiter.TakeAvailable(count) > 0 {
return true
}

if l.option == metapb.Wait {
l.limiter.Wait(count)
return true
}

return l.limiter.TakeAvailable(count) > 0
return false
}
5 changes: 0 additions & 5 deletions pkg/route/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,12 @@ func (p *parser) parse() ([]node, error) {
nt: slashType,
value: slashValue,
})
break
case tokenLParen:
if prev != tokenSlash {
return nil, fmt.Errorf("syntax error: ( must after /")
}

p.lexer.ScanString()
break
case tokenColon:
if prev == tokenLParen {
value := p.lexer.ScanString()
Expand All @@ -137,7 +135,6 @@ func (p *parser) parse() ([]node, error) {
p.lexer.ScanString()
}

break
case tokenVertical:
prevNode := p.prevNode()

Expand All @@ -149,7 +146,6 @@ func (p *parser) parse() ([]node, error) {
return nil, fmt.Errorf("syntax error: missing : with enum type")
}

break
case tokenRParen:
if prev == tokenLParen {
var nt nodeType
Expand Down Expand Up @@ -177,7 +173,6 @@ func (p *parser) parse() ([]node, error) {
return nil, fmt.Errorf("syntax error: missing (")
}

break
case tokenEOF:
if prev == tokenSlash {
p.nodes = append(p.nodes, node{
Expand Down
29 changes: 29 additions & 0 deletions pkg/util/buffer_string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package util

import (
"bytes"
"github.com/fagongzi/util/hack"
"testing"
)

func BenchmarkHackToString(b *testing.B) {
var buf bytes.Buffer
buf.WriteString("[")
buf.Write([]byte("GET"))
buf.WriteString("]")
buf.Write([]byte("api"))
for n := 0; n < b.N; n++ {
hack.SliceToString(buf.Bytes())
}
}

func BenchmarkBufferToString(b *testing.B) {
var buf bytes.Buffer
buf.WriteString("[")
buf.Write([]byte("GET"))
buf.WriteString("]")
buf.Write([]byte("api"))
for n := 0; n < b.N; n++ {
buf.String()
}
}