2 Unstar Star 0 Fork 0

JUMEI_ARCH / go-pluginsGoApache-2.0

Clone or download
uber.go
Copy Edit Raw Normal History
Asim   uber rate limit wrappers 2016-10-27
package ratelimitimport (	"github.com/micro/go-micro/client"	"github.com/micro/go-micro/server"	"go.uber.org/ratelimit"
Asim Aslam   switch to stdlib context 2018-03-03
	"context"
Asim   uber rate limit wrappers 2016-10-27
)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 limiterfunc 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 limiterfunc 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.14.1

Search