From 9de6994011e9ab8df0735b876e6a3b192632346b Mon Sep 17 00:00:00 2001 From: lampScript Date: Fri, 15 Nov 2019 11:59:36 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E9=83=A8?= =?UTF-8?q?=E5=88=86=E6=B3=A8=E9=87=8A=E7=9A=84=E6=8B=BC=E5=86=99=EF=BC=8C?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E6=8E=89switch=E4=B8=AD=E5=A4=9A=E4=BD=99?= =?UTF-8?q?=E7=9A=84break?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/proxy/proxy.go | 2 +- pkg/route/parser.go | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/pkg/proxy/proxy.go b/pkg/proxy/proxy.go index 0afd6c02..be4fbe04 100644 --- a/pkg/proxy/proxy.go +++ b/pkg/proxy/proxy.go @@ -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", diff --git a/pkg/route/parser.go b/pkg/route/parser.go index 5c7f7388..68b22544 100644 --- a/pkg/route/parser.go +++ b/pkg/route/parser.go @@ -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() @@ -137,7 +135,6 @@ func (p *parser) parse() ([]node, error) { p.lexer.ScanString() } - break case tokenVertical: prevNode := p.prevNode() @@ -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 @@ -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{ From 363cdaefed8a42496184893b5f9aff72892bc382 Mon Sep 17 00:00:00 2001 From: lampScript Date: Thu, 21 Nov 2019 13:44:30 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0buffer=E7=9A=84=E5=9F=BA?= =?UTF-8?q?=E5=87=86=E6=B5=8B=E8=AF=95=E5=92=8Cjwt=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E7=9A=84=E5=8F=82=E6=95=B0=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/proxy/filter_jwt.go | 17 ++++++++++++----- pkg/util/buffer_string_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 pkg/util/buffer_string_test.go diff --git a/pkg/proxy/filter_jwt.go b/pkg/proxy/filter_jwt.go index d442aeab..4c867cc4 100644 --- a/pkg/proxy/filter_jwt.go +++ b/pkg/proxy/filter_jwt.go @@ -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" @@ -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 } @@ -190,8 +193,11 @@ 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": @@ -199,6 +205,7 @@ func (f *JWTFilter) initTokenLookup() { case "cookie": f.getter = jwtFromCookie(parts[1]) } + return nil } func (f *JWTFilter) initActions() error { diff --git a/pkg/util/buffer_string_test.go b/pkg/util/buffer_string_test.go new file mode 100644 index 00000000..bfb89520 --- /dev/null +++ b/pkg/util/buffer_string_test.go @@ -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() + } +} From 19c5503b0bbba6283dde98252e1d5ed56e34f364 Mon Sep 17 00:00:00 2001 From: lampScript Date: Thu, 21 Nov 2019 22:59:53 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E8=B0=83=E6=95=B4=E9=99=90=E6=B5=81?= =?UTF-8?q?=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/proxy/dispatcher_event.go | 25 ++++++++++++------------- pkg/proxy/rate_limit.go | 7 +++++-- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/pkg/proxy/dispatcher_event.go b/pkg/proxy/dispatcher_event.go index 48f09096..41f9a2ee 100644 --- a/pkg/proxy/dispatcher_event.go +++ b/pkg/proxy/dispatcher_event.go @@ -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) } } diff --git a/pkg/proxy/rate_limit.go b/pkg/proxy/rate_limit.go index dfa0b630..0153cdb6 100644 --- a/pkg/proxy/rate_limit.go +++ b/pkg/proxy/rate_limit.go @@ -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 }