本文主要研究一下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


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...