1 Star 0 Fork 0

larrypu / go-plugins

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
endpoint.go 1.64 KB
一键复制 编辑 原始数据 按行查看 历史
Asim Aslam 提交于 2019-01-10 22:12 . update breaks
// 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"
"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.Endpoint()] {
// 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.Endpoint()] {
// no
return h(ctx, req, rsp)
}
// yes
return hw(h)(ctx, req, rsp)
}
}
}
1
https://gitee.com/larrypu/go-plugins.git
git@gitee.com:larrypu/go-plugins.git
larrypu
go-plugins
go-plugins
v0.21.1

搜索帮助

53164aa7 5694891 3bd8fe86 5694891