当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
2 Star 0 Fork 1

JUMEI_ARCH/go-plugins
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
endpoint.go 1.65 KB
一键复制 编辑 原始数据 按行查看 历史
Asim Aslam 提交于 2017-01-01 20:29 . Add package comments
// Package endpoint provides a wrapper that executes other wrappers for specific methods
package endpoint
import (
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/server"
"golang.org/x/net/context"
)
type clientWrapper struct {
endpoints map[string]bool // endpoints to execute on
wrapper client.Wrapper // the original wrapper
client.Client
}
func (c *clientWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
// execute client wrapper?
if !c.endpoints[req.Method()] {
// no
return c.Client.Call(ctx, req, rsp, opts...)
}
// yes
return c.wrapper(c.Client).Call(ctx, req, rsp, opts...)
}
// NewClientWrapper wraps another client wrapper but only executes when the endpoints specified are executed.
func NewClientWrapper(cw client.Wrapper, eps ...string) client.Wrapper {
// create map of endpoints
endpoints := make(map[string]bool)
for _, ep := range eps {
endpoints[ep] = true
}
return func(c client.Client) client.Client {
return &clientWrapper{endpoints, cw, c}
}
}
// NewHandlerWrapper wraps another handler wrapper but only executes when the endpoints specified are executed.
func NewHandlerWrapper(hw server.HandlerWrapper, eps ...string) server.HandlerWrapper {
// create map of endpoints
endpoints := make(map[string]bool)
for _, ep := range eps {
endpoints[ep] = true
}
return func(h server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
// execute the handler wrapper?
if !endpoints[req.Method()] {
// no
return h(ctx, req, rsp)
}
// yes
return hw(h)(ctx, req, rsp)
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/JMArch/go-plugins.git
git@gitee.com:JMArch/go-plugins.git
JMArch
go-plugins
go-plugins
v0.5.0

搜索帮助

0d507c66 1850385 C8b1a773 1850385