3

聊聊dubbo-go-proxy的apiFilter

 3 years ago
source link: https://studygolang.com/articles/33325
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

聊聊dubbo-go-proxy的apiFilter

mb601a5e014a36c · 大约5小时之前 · 18 次点击 · 预计阅读时间 2 分钟 · 不到1分钟之前 开始浏览    

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

[email protected]/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()。

  • dubbo-go-proxy

有疑问加站长微信联系(非本文作者)

280

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:1006366459


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK