聊聊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

本文来自:Segmentfault

感谢作者:.container .card .information strong

查看原文:聊聊dubbo-go-proxy的apiFilter

相关阅读 >>

arts #5

Golang字符串国际化

Golang fyne 使用中文并打包进二进制文件

rtmp协议视频平台easydss编译过程中Go语言异步信息处理设计与实现

手撸Golang 基本数据结构与算法 数组

Go 1.15 版本的优化清单【总结】

[concurrent-map]-并发map在Go中的使用

Golang中main中panic和后续panic处理,以及新开协程的影响

[Go] Go语言实战-为博客园增加Gofly在线客服功能

手撸Golang 行为型设计模式 访问者模式

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...