MCPcopy
hub / github.com/grafana/dskit / NewRateLimiter

Function NewRateLimiter

grpcclient/ratelimit.go:13–26  ·  view source on GitHub ↗

NewRateLimiter creates a UnaryClientInterceptor for client side rate limiting.

(cfg *Config)

Source from the content-addressed store, hash-verified

11
12// NewRateLimiter creates a UnaryClientInterceptor for client side rate limiting.
13func NewRateLimiter(cfg *Config) grpc.UnaryClientInterceptor {
14 burst := cfg.RateLimitBurst
15 if burst == 0 {
16 burst = int(cfg.RateLimit)
17 }
18 limiter := rate.NewLimiter(rate.Limit(cfg.RateLimit), burst)
19 return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
20 err := limiter.Wait(ctx)
21 if err != nil {
22 return status.Error(codes.ResourceExhausted, err.Error())
23 }
24 return invoker(ctx, method, req, reply, cc, opts...)
25 }
26}

Calls 3

LimitMethod · 0.65
WaitMethod · 0.45
ErrorMethod · 0.45