乐趣区

关于golang:聊聊dubbogoproxy的hostFilter

本文次要钻研一下 dubbo-go-proxy 的 hostFilter

hostFilter

dubbo-go-proxy/pkg/filter/host/host.go

// hostFilter is a filter for host.
type hostFilter struct {host string}

// New create host filter.
func New(host string) filter.Filter {return &hostFilter{host: host}
}

hostFilter 的 New 依据 host 创立 hostFilter

Do

dubbo-go-proxy/pkg/filter/host/host.go

func (f hostFilter) Do() fc.FilterFunc {return func(c fc.Context) {f.doHostFilter(c.(*contexthttp.HttpContext))
    }
}

func (f hostFilter) doHostFilter(c *contexthttp.HttpContext) {
    c.Request.Host = f.host
    c.Next()}

Do 办法执行 doHostFilter 办法,该办法将 f.host 设置给 c.Request.Host,而后执行 c.Next()

httpFilter

dubbo-go-proxy/pkg/proxy/listener.go

func httpFilter(ctx *h.HttpContext, request fc.IntegrationRequest) {if len(request.Host) != 0 {ctx.AppendFilterFunc(host.New(request.Host).Do())
    }
    if len(request.Path) != 0 {ctx.AppendFilterFunc(replacepath.New(request.Path).Do())
    }
}

httpFilter 办法通过 AppendFilterFunc 将 host.New(request.Host) 追加到 Filters 中

小结

dubbo-go-proxy 的 hostFilter 将 f.host 设置给 c.Request.Host,而后执行 c.Next()。

doc

  • dubbo-go-proxy
退出移动版