2 Star 4 Fork 6

Jason的雷哥/micro-quickstart
关闭

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
set.go 3.92 KB
一键复制 编辑 原始数据 按行查看 历史
Jason的雷哥 提交于 2018-08-24 14:33 +08:00 . 在服务端endpoint实现了服务限流
package endpoint
import (
"context"
"time"
"gitee.com/jason_elva8325/micro-quickstart/service"
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/metrics"
"github.com/go-kit/kit/ratelimit"
"golang.org/x/time/rate"
)
// Set 服务端点集合定义
type Set struct {
SayHelloEndpoint endpoint.Endpoint
SayGoodbyEndpoint endpoint.Endpoint
}
// New 新建服务端点方法
func New(svc service.Service, logger log.Logger, duration metrics.Histogram, rateLimit int) Set {
var sayHelloEndpoint endpoint.Endpoint
{
sayHelloEndpoint = MakeSayHelloEndpoint(svc)
if rateLimit > 0 {
sayHelloEndpoint = ratelimit.NewErroringLimiter(rate.NewLimiter(rate.Every(time.Second), rateLimit))(sayHelloEndpoint) // 限流错误返回,服务直接返回超限错误
}
sayHelloEndpoint = LoggingMiddleware(log.With(logger, "method", "SayHello"))(sayHelloEndpoint)
sayHelloEndpoint = InstrumentingMiddleware(duration.With("method", "SayHello"))(sayHelloEndpoint)
}
var sayGoodbyEndpoint endpoint.Endpoint
{
sayGoodbyEndpoint = MakeSayGoodbyEndpoint(svc)
if rateLimit > 0 {
sayGoodbyEndpoint = ratelimit.NewDelayingLimiter(rate.NewLimiter(rate.Every(time.Second), rateLimit))(sayGoodbyEndpoint) // 限流延迟访问,服务延时返回结果
}
sayGoodbyEndpoint = LoggingMiddleware(log.With(logger, "method", "SayGoodby"))(sayGoodbyEndpoint)
sayGoodbyEndpoint = InstrumentingMiddleware(duration.With("method", "SayGoodby"))(sayGoodbyEndpoint)
}
return Set{
SayHelloEndpoint: sayHelloEndpoint,
SayGoodbyEndpoint: sayGoodbyEndpoint,
}
}
// SayHello 服务端点的业务方法定义,一般情况下直接同名透传到服务层
func (s Set) SayHello(ctx context.Context, lang, target string) (string, error) {
resp, err := s.SayHelloEndpoint(ctx, SayHelloRequest{lang, target})
if err != nil {
return "", err
}
response := resp.(SayHelloResponse)
return response.V, response.Err
}
// SayGoodby 服务端点的业务方法定义,一般情况下直接同名透传到服务层
func (s Set) SayGoodby(ctx context.Context, lang, target string) (string, error) {
resp, err := s.SayGoodbyEndpoint(ctx, SayGoodbyRequest{lang, target})
if err != nil {
return "", err
}
response := resp.(SayGoodbyResponse)
return response.V, response.Err
}
// MakeSayHelloEndpoint 创建服务端点方法
func MakeSayHelloEndpoint(svc service.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(SayHelloRequest)
result, err := svc.SayHello(ctx, req.Lang, req.Target)
return SayHelloResponse{V: result, Err: err}, nil
}
}
// MakeSayGoodbyEndpoint 创建服务端点方法
func MakeSayGoodbyEndpoint(svc service.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(SayGoodbyRequest)
result, err := svc.SayGoodby(ctx, req.Lang, req.Target)
return SayGoodbyResponse{V: result, Err: err}, nil
}
}
// 实现 endpoint.Failer 接口的响应类型断言
var (
_ endpoint.Failer = SayHelloResponse{}
_ endpoint.Failer = SayGoodbyResponse{}
)
// SayHelloRequest 服务端点请求对象定义
type SayHelloRequest struct {
Lang string
Target string
}
// SayHelloResponse 服务端点响应对象定义
type SayHelloResponse struct {
V string `json:"V"`
Err error `json:"-"` // 将由失败/错误编码器拦截
}
// Failed 服务端点的相应对象需要继承 endpoint.Failer 接口
func (r SayHelloResponse) Failed() error { return r.Err }
// SayGoodbyRequest 服务端点请求对象定义
type SayGoodbyRequest struct {
Lang string
Target string
}
// SayGoodbyResponse 服务端点响应对象定义
type SayGoodbyResponse struct {
V string `json:"V"`
Err error `json:"-"` // 将由失败/错误编码器拦截
}
// Failed 服务端点的相应对象需要继承 endpoint.Failer 接口
func (r SayGoodbyResponse) Failed() error { return r.Err }
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jason_elva8325/micro-quickstart.git
git@gitee.com:jason_elva8325/micro-quickstart.git
jason_elva8325
micro-quickstart
micro-quickstart
v0.4.0

搜索帮助