newRateLimiter creates a UnaryClientInterceptor for client side rate limiting.
(cfg *grpcConfig)
| 714 | |
| 715 | // newRateLimiter creates a UnaryClientInterceptor for client side rate limiting. |
| 716 | func newRateLimiter(cfg *grpcConfig) grpc.UnaryClientInterceptor { |
| 717 | burst := cfg.RateLimitBurst |
| 718 | if burst == 0 { |
| 719 | burst = int(cfg.RateLimit) |
| 720 | } |
| 721 | limiter := rate.NewLimiter(rate.Limit(cfg.RateLimit), burst) |
| 722 | return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { |
| 723 | err := limiter.Wait(ctx) |
| 724 | if err != nil { |
| 725 | return status.Error(codes.ResourceExhausted, err.Error()) |
| 726 | } |
| 727 | return invoker(ctx, method, req, reply, cc, opts...) |
| 728 | } |
| 729 | } |
no test coverage detected