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

JUMEI_ARCH / go-plugins
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
uber.go 955 Bytes
一键复制 编辑 原始数据 按行查看 历史
Asim 提交于 2016-10-27 12:58 . uber rate limit wrappers
package ratelimit
import (
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/server"
"go.uber.org/ratelimit"
"golang.org/x/net/context"
)
type clientWrapper struct {
r ratelimit.Limiter
client.Client
}
func (c *clientWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
c.r.Take()
return c.Client.Call(ctx, req, rsp, opts...)
}
// NewClientWrapper creates a blocking side rate limiter
func NewClientWrapper(rate int) client.Wrapper {
r := ratelimit.New(rate)
return func(c client.Client) client.Client {
return &clientWrapper{r, c}
}
}
// NewHandlerWrapper creates a blocking server side rate limiter
func NewHandlerWrapper(rate int) server.HandlerWrapper {
r := ratelimit.New(rate)
return func(h server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
r.Take()
return h(ctx, req, rsp)
}
}
}
Go
1
https://gitee.com/JMArch/go-plugins.git
git@gitee.com:JMArch/go-plugins.git
JMArch
go-plugins
go-plugins
v0.5.0

搜索帮助