MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / StreamServerInterceptor

Function StreamServerInterceptor

pkg/ratelimit/grpc.go:81–102  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
81func 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}

Callers 2

NewFunction · 0.92
TestGRPCFunction · 0.92

Calls 10

grpcIsClusterAuthCallFunction · 0.85
handlerFunction · 0.85
grpcStreamAcceptResourceFunction · 0.85
grpcStreamUpResourceFunction · 0.85
GRPCHeadersMethod · 0.80
ContextMethod · 0.65
RateLimitMethod · 0.65
KeyMethod · 0.65
SetHeaderMethod · 0.45
WithAttributesMethod · 0.45

Tested by 1

TestGRPCFunction · 0.74