Skip to content

Commit

Permalink
Merge pull request fagongzi#180 from LampScript/master
Browse files Browse the repository at this point in the history
代码细节微调
  • Loading branch information
zhangxu19830126 authored Nov 13, 2019
2 parents 46498b1 + 86b412c commit 3c16a21
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
15 changes: 6 additions & 9 deletions pkg/proxy/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ type copyReq struct {
func (req *copyReq) prepare() {
if req.needRewrite() {
// if not use rewrite, it only change uri path and query string
realPath := req.rewiteURL()
realPath := req.rewriteURL()
if "" != realPath {
req.origin.SetRequestURI(realPath)
req.origin.SetHost(req.to.meta.Addr)

log.Infof("%s: dipatch node %d rewrite url to %s for copy",
log.Infof("%s: dispatch node %d rewrite url to %s for copy",
req.requestTag,
req.idx,
realPath)
Expand All @@ -48,7 +48,7 @@ func (req *copyReq) needRewrite() bool {
return req.node.meta.URLRewrite != ""
}

func (req *copyReq) rewiteURL() string {
func (req *copyReq) rewriteURL() string {
ctx := &expr.Ctx{}
ctx.Origin = req.origin
ctx.Params = req.params
Expand Down Expand Up @@ -78,13 +78,10 @@ func (dn *dispatchNode) setHost(forwardReq *fasthttp.Request) {
switch dn.node.meta.HostType {
case metapb.HostOrigin:
forwardReq.SetHostBytes(dn.ctx.Request.Host())
break
case metapb.HostServerAddress:
forwardReq.SetHost(dn.dest.meta.Addr)
break
case metapb.HostCustom:
forwardReq.SetHost(dn.node.meta.CustemHost)
break
}
}

Expand Down Expand Up @@ -244,15 +241,15 @@ func newDispatcher(cnf *Cfg, db store.Store, runner *task.Runner, jsEngineFunc f
}

func (r *dispatcher) dispatch(reqCtx *fasthttp.RequestCtx, requestTag string) (*apiRuntime, []*dispatchNode, *expr.Ctx) {
req:=&reqCtx.Request
route := r.route
req := &reqCtx.Request
dispatcherRoute := r.route
var targetAPI *apiRuntime
var dispatches []*dispatchNode

exprCtx := acquireExprCtx()
exprCtx.Origin = req

id, ok := route.Find(req.URI().Path(), hack.SliceToString(req.Header.Method()), exprCtx.AddParam)
id, ok := dispatcherRoute.Find(req.URI().Path(), hack.SliceToString(req.Header.Method()), exprCtx.AddParam)
if ok {
if api, ok := r.apis[id]; ok && api.matches(req) {
targetAPI = api
Expand Down
36 changes: 18 additions & 18 deletions pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func (p *Proxy) ServeFastHTTP(ctx *fasthttp.RequestCtx) {
dn.rd = rd
dn.ctx = ctx
if dn.copyTo != nil {
log.Infof("%s: dipatch node %d copy to %s",
log.Infof("%s: dispatch node %d copy to %s",
requestTag,
idx,
dn.copyTo.meta.Addr)
Expand Down Expand Up @@ -336,14 +336,14 @@ func (p *Proxy) doCopy(req *copyReq) {

req.prepare()

log.Infof("%s: dipatch node %d copy to %s",
log.Infof("%s: dispatch node %d copy to %s",
req.requestTag,
req.idx,
req.to.meta.Addr)

res, err := p.client.Do(req.origin, svr.meta.Addr, nil)
if err != nil {
log.Errorf("%s: dipatch node %d copy to %s with error %s",
log.Errorf("%s: dispatch node %d copy to %s with error %s",
req.requestTag,
req.idx,
req.to.meta.Addr,
Expand All @@ -362,7 +362,7 @@ func (p *Proxy) doCopy(req *copyReq) {
func (p *Proxy) doProxy(dn *dispatchNode, adjustH func(*proxyContext)) {
if dn.node.meta.UseDefault {
dn.maybeDone()
log.Infof("%s: dipatch node %d force using default",
log.Infof("%s: dispatch node %d force using default",
dn.requestTag,
dn.idx)
return
Expand All @@ -374,13 +374,13 @@ func (p *Proxy) doProxy(dn *dispatchNode, adjustH func(*proxyContext)) {
dn.err = ErrNoServer
dn.code = fasthttp.StatusServiceUnavailable
dn.maybeDone()
log.Infof("%s: dipatch node %d has no server, return with 503",
log.Infof("%s: dispatch node %d has no server, return with 503",
dn.requestTag,
dn.idx)
return
}

log.Debugf("%s: dipatch node %d to server %d",
log.Debugf("%s: dispatch node %d to server %d",
dn.requestTag,
dn.idx,
svr.id)
Expand All @@ -392,7 +392,7 @@ func (p *Proxy) doProxy(dn *dispatchNode, adjustH func(*proxyContext)) {
// if not use rewrite, it only change uri path and query string
realPath := expr.Exec(dn.exprCtx, dn.node.parsedExprs...)
if len(realPath) != 0 {
log.Infof("%s: dipatch node %d rewrite url to %s",
log.Infof("%s: dispatch node %d rewrite url to %s",
dn.requestTag,
dn.idx,
hack.SliceToString(realPath))
Expand All @@ -403,7 +403,7 @@ func (p *Proxy) doProxy(dn *dispatchNode, adjustH func(*proxyContext)) {
dn.code = fasthttp.StatusBadRequest
dn.maybeDone()

log.Warnf("%s: dipatch node %d rewrite not match, return with 400",
log.Warnf("%s: dispatch node %d rewrite not match, return with 400",
dn.requestTag,
dn.idx)
return
Expand All @@ -426,7 +426,7 @@ func (p *Proxy) doProxy(dn *dispatchNode, adjustH func(*proxyContext)) {
dn.maybeDone()
releaseContext(c)

log.Errorf("%s: dipatch node %d call filter %s pre failed with error %s",
log.Errorf("%s: dispatch node %d call filter %s pre failed with error %s",
dn.requestTag,
dn.idx,
filterName,
Expand All @@ -443,7 +443,7 @@ func (p *Proxy) doProxy(dn *dispatchNode, adjustH func(*proxyContext)) {
dn.maybeDone()
releaseContext(c)

log.Errorf("%s: dipatch node %d using response attr with error %s",
log.Errorf("%s: dispatch node %d using response attr with error %s",
dn.requestTag,
dn.idx,
dn.err)
Expand All @@ -454,7 +454,7 @@ func (p *Proxy) doProxy(dn *dispatchNode, adjustH func(*proxyContext)) {
dn.maybeDone()
releaseContext(c)

log.Infof("%s: dipatch node %d using response attr",
log.Infof("%s: dispatch node %d using response attr",
dn.requestTag,
dn.idx)
return
Expand All @@ -466,7 +466,7 @@ func (p *Proxy) doProxy(dn *dispatchNode, adjustH func(*proxyContext)) {
dn.maybeDone()
releaseContext(c)

log.Infof("%s: dipatch node %d using cache",
log.Infof("%s: dispatch node %d using cache",
dn.requestTag,
dn.idx)
return
Expand Down Expand Up @@ -509,7 +509,7 @@ func (p *Proxy) doProxy(dn *dispatchNode, adjustH func(*proxyContext)) {
// retry with strategiess
retry := dn.retryStrategy()
if times >= retry.MaxTimes {
log.Infof("%s: dipatch node %d sent times over the max %d",
log.Infof("%s: dispatch node %d sent times over the max %d",
dn.requestTag,
dn.idx,
retry.MaxTimes)
Expand All @@ -529,7 +529,7 @@ func (p *Proxy) doProxy(dn *dispatchNode, adjustH func(*proxyContext)) {
dn.code = fasthttp.StatusServiceUnavailable
dn.maybeDone()

log.Infof("%s: dipatch node %d has no server, return with 503",
log.Infof("%s: dispatch node %d has no server, return with 503",
dn.requestTag,
dn.idx)
return
Expand All @@ -541,13 +541,13 @@ func (p *Proxy) doProxy(dn *dispatchNode, adjustH func(*proxyContext)) {
resCode := fasthttp.StatusInternalServerError

if nil != err {
log.Errorf("%s: dipatch node %d failed with error %s",
log.Errorf("%s: dispatch node %d failed with error %s",
dn.requestTag,
dn.idx,
err)
} else {
resCode = res.StatusCode()
log.Errorf("%s: dipatch node %d failed with error code %d",
log.Errorf("%s: dispatch node %d failed with error code %d",
dn.requestTag,
dn.idx,
resCode)
Expand All @@ -563,7 +563,7 @@ func (p *Proxy) doProxy(dn *dispatchNode, adjustH func(*proxyContext)) {
}

if log.DebugEnabled() {
log.Debugf("%s: dipatch node %d return by %s with code %d, body <%s>",
log.Debugf("%s: dispatch node %d return by %s with code %d, body <%s>",
dn.requestTag,
dn.idx,
svr.meta.Addr,
Expand All @@ -574,7 +574,7 @@ func (p *Proxy) doProxy(dn *dispatchNode, adjustH func(*proxyContext)) {
// post filters
filterName, code, err = p.doPostFilters(dn.requestTag, c, filters...)
if nil != err {
log.Errorf("%s: dipatch node %d call filter %s post failed with error %s",
log.Errorf("%s: dispatch node %d call filter %s post failed with error %s",
dn.requestTag,
dn.idx,
filterName,
Expand Down

0 comments on commit 3c16a21

Please sign in to comment.