grpc

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 31, 2026 License: MIT Imports: 10 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractPeerIP

func ExtractPeerIP(ctx context.Context, trustedProxies []string) string

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

type KeyExtractor func(ctx context.Context) string

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

func WithRouteLabeling(enable bool) Option

WithRouteLabeling enables passing the gRPC FullMethod name (e.g. /package.Service/Method) as the route label in telemetry events.

func WithTrustedProxies

func WithTrustedProxies(proxies []string) Option

WithTrustedProxies configures a list of trusted proxy IPs or CIDR networks for safely parsing x-forwarded-for metadata.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL