StreamServerInterceptor is a grpc.StreamServerInterceptor that rate limits new gRPC requests and messages sent by the client. If the X-Real-IP header is not set, it is assumed that the gRPC request originates from the cluster, and no rate limits are enforced.
(limiter Interface)
| 79 | // StreamServerInterceptor is a grpc.StreamServerInterceptor that rate limits new gRPC requests and messages sent by the client. |
| 80 | // If the X-Real-IP header is not set, it is assumed that the gRPC request originates from the cluster, and no rate limits are enforced. |
| 81 | func StreamServerInterceptor(limiter Interface) grpc.StreamServerInterceptor { |
| 82 | return func(srv any, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { |
| 83 | ctx := stream.Context() |
| 84 | |
| 85 | if grpcIsClusterAuthCall(ctx) { |
| 86 | return handler(ctx, stream) |
| 87 | } |
| 88 | |
| 89 | acceptResource := grpcStreamAcceptResource(ctx, info.FullMethod) |
| 90 | limit, result := limiter.RateLimit(acceptResource) |
| 91 | stream.SetHeader(result.GRPCHeaders()) // nolint:errcheck |
| 92 | if limit { |
| 93 | return errRateLimitExceeded.WithAttributes("key", acceptResource.Key(), "rate", result.Limit) |
| 94 | } |
| 95 | |
| 96 | return handler(srv, &rateLimitedServerStream{ |
| 97 | ServerStream: stream, |
| 98 | limiter: limiter, |
| 99 | resource: grpcStreamUpResource(ctx, info.FullMethod), |
| 100 | }) |
| 101 | } |
| 102 | } |