Go教程

聊聊dubbo-go-proxy的hostFilter

本文主要是介绍聊聊dubbo-go-proxy的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
这篇关于聊聊dubbo-go-proxy的hostFilter的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!