Documentation
¶
Overview ¶
Package grpc provides gRPC server interceptor middleware for rate limiting. It supports unary and streaming RPC interceptors with IP spoofing protection and context metadata key extraction.
Index ¶
- func ExtractPeerIP(ctx context.Context, trustedProxies []string) string
- func StreamServerInterceptor(limiter *distlimit.Limiter, opts ...Option) grpc.StreamServerInterceptor
- func UnaryServerInterceptor(limiter *distlimit.Limiter, opts ...Option) grpc.UnaryServerInterceptor
- type KeyExtractor
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractPeerIP ¶
ExtractPeerIP securely extracts the client's peer network address from the gRPC context & incoming metadata. Header metadata (x-forwarded-for, x-real-ip) is parsed only if the peer IP originates from a trusted proxy.
func StreamServerInterceptor ¶
func StreamServerInterceptor(limiter *distlimit.Limiter, opts ...Option) grpc.StreamServerInterceptor
StreamServerInterceptor returns a grpc.StreamServerInterceptor that enforces rate limiting on incoming streaming gRPC calls. If the rate limit is exceeded, it rejects stream establishment with a codes.ResourceExhausted status error. If an internal rate limiter error occurs, it follows a fail-open policy and allows the stream to proceed.
func UnaryServerInterceptor ¶
func UnaryServerInterceptor(limiter *distlimit.Limiter, opts ...Option) grpc.UnaryServerInterceptor
UnaryServerInterceptor returns a grpc.UnaryServerInterceptor that enforces rate limiting on incoming unary gRPC calls. If the rate limit is exceeded, it returns a status error with code codes.ResourceExhausted. If an internal rate limiter error occurs, it follows a fail-open policy and allows the request to proceed.
Types ¶
type KeyExtractor ¶
KeyExtractor is a function type for extracting a rate limit key from a gRPC context.Context.
type Option ¶
type Option func(*config)
Option configures functional parameters for the gRPC rate limit interceptors.
func WithKeyExtractor ¶
func WithKeyExtractor(fn KeyExtractor) Option
WithKeyExtractor configures a custom key extraction strategy from gRPC context.
func WithRouteLabeling ¶ added in v1.2.0
WithRouteLabeling enables passing the gRPC FullMethod name (e.g. /package.Service/Method) as the route label in telemetry events.
func WithTrustedProxies ¶
WithTrustedProxies configures a list of trusted proxy IPs or CIDR networks for safely parsing x-forwarded-for metadata.