Go教程

聊聊dubbo-go-proxy的apiFilter

本文主要是介绍聊聊dubbo-go-proxy的apiFilter,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

本文主要研究一下dubbo-go-proxy的apiFilter

apiFilter

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

func Init() {
    extension.SetFilterFunc(constant.HTTPApiFilter, apiFilterFunc())
}
Init方法往extension设置了名为dgp.filters.http.api的apiFilterFunc

apiFilterFunc

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

// apiFilterFunc url match api
func apiFilterFunc() context.FilterFunc {
    return func(c context.Context) {
        url := c.GetUrl()
        method := c.GetMethod()
        // [williamfeng323]TO-DO: get the API details from router which saved in constant.LocalMemoryApiDiscoveryService
        if api, ok := api.EmptyApi.FindApi(url); ok {
            if !api.MatchMethod(method) {
                c.WriteWithStatus(http.StatusMethodNotAllowed, constant.Default405Body)
                c.AddHeader(constant.HeaderKeyContextType, constant.HeaderValueTextPlain)
                c.Abort()
                return
            }

            if !api.IsOk(api.Name) {
                c.WriteWithStatus(http.StatusNotAcceptable, constant.Default406Body)
                c.AddHeader(constant.HeaderKeyContextType, constant.HeaderValueTextPlain)
                c.Abort()
                return
            }
            // [williamfeng323]TO-DO: the c.Api method need to be updated to use the newest API definition
            c.Api(api)
            c.Next()
        } else {
            c.WriteWithStatus(http.StatusNotFound, constant.Default404Body)
            c.AddHeader(constant.HeaderKeyContextType, constant.HeaderValueTextPlain)
            c.Abort()
        }
    }
}
apiFilterFunc首先通过api.EmptyApi.FindApi(url)来查找对应的api,找不到则返回404;找到的话则先通过api.MatchMethod判断method是否匹配,不匹配的话返回405;之后通过api.IsOk判断该api的status是否up,不是的话返回406;最后执行c.Api(api)及c.Next()。

dubbo-go-proxy-filter

dubbo-go-proxy-filter@v0.1.0-rc1.0.20210120132524-c63f4eb13725/pkg/api/api.go

func (a *Api) FindApi(name string) (*Api, bool) {
    if v, ok := CacheApi.Load(name); ok {
        return v.(*Api), true
    }

    return nil, false
}

// MatchMethod
func (a *Api) MatchMethod(method string) bool {
    i := RequestMethodValue[method]
    if a.RequestMethod == RequestMethod(i) {
        return true
    }

    return false
}

// IsOk api status equals Up
func (a *Api) IsOk(name string) bool {
    if v, ok := CacheApi.Load(name); ok {
        return v.(*Api).Status == Up
    }

    return false
}
api.go提供了FindApi、MatchMethod、IsOk方法

小结

apiFilterFunc首先通过api.EmptyApi.FindApi(url)来查找对应的api,找不到则返回404;找到的话则先通过api.MatchMethod判断method是否匹配,不匹配的话返回405;之后通过api.IsOk判断该api的status是否up,不是的话返回406;最后执行c.Api(api)及c.Next()。

doc

  • dubbo-go-proxy
这篇关于聊聊dubbo-go-proxy的apiFilter的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!