package user import ( "context" "errors" "github.com/go-kit/kit/endpoint" "golang.org/x/time/rate" ) var ErrLimitExceed = errors.New("Rate limit exceed!") // 使用x/time/rate创建限流中间件 func NewTokenBucketLimitterWithBuildIn(bkt *rate.Limiter) endpoint.Middleware { return func(next endpoint.Endpoint) endpoint.Endpoint { return func(ctx context.Context, request interface{}) (response interface{}, err error) { if !bkt.Allow() { return nil, ErrLimitExceed } return next(ctx, request) } } }