Interceptors¶
The helpers/interceptors subpackage provides gRPC server interceptors for exception handling
and rate limiting. FastAPI metrics middleware and gRPC metric/trace interceptors were removed in
5.0.0 — use OpenTelemetry via AppUtils / OtelUtils instead (see
Observability).
gRPC¶
base¶
Abstract base classes for gRPC client and server interceptors.
Base gRPC client interceptor.
archipy.helpers.interceptors.grpc.base.client_interceptor.ClientCallDetails ¶
Bases: _ClientCallDetailsFields, ClientCallDetails
Describes an RPC to be invoked.
This class extends grpc.ClientCallDetails and provides additional fields for RPC details.
See https://grpc.github.io/grpc/python/grpc.html#grpc.ClientCallDetails
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.ClientCallDetails.method
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.ClientCallDetails.timeout
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.ClientCallDetails.metadata
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.ClientCallDetails.credentials
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.ClientCallDetails.wait_for_ready
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.ClientCallDetails.compression
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseGrpcClientInterceptor ¶
Bases: UnaryUnaryClientInterceptor, UnaryStreamClientInterceptor, StreamUnaryClientInterceptor, StreamStreamClientInterceptor
Base class for gRPC client interceptors.
This class provides a base implementation for intercepting gRPC client calls. It supports unary-unary, unary-stream, stream-unary, and stream-stream RPCs.
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseGrpcClientInterceptor.intercept
abstractmethod
¶
Intercepts a gRPC client call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The continuation function to call. |
required |
request_or_iterator
|
Any
|
The request or request iterator. |
required |
call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseGrpcClientInterceptor.intercept_unary_unary ¶
intercept_unary_unary(
continuation: Callable[
[ClientCallDetails, _TRequest], Any
],
client_call_details: ClientCallDetails,
request: _TRequest,
) -> Any
Intercepts a unary-unary RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request
|
Any
|
The request object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseGrpcClientInterceptor.intercept_unary_stream ¶
intercept_unary_stream(
continuation: Callable[
[ClientCallDetails, _TRequest], Any
],
client_call_details: ClientCallDetails,
request: _TRequest,
) -> Any
Intercepts a unary-stream RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request
|
Any
|
The request object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseGrpcClientInterceptor.intercept_stream_unary ¶
intercept_stream_unary(
continuation: Callable[
[ClientCallDetails, Iterator[_TRequest]], Any
],
client_call_details: ClientCallDetails,
request_iterator: Iterator[_TRequest],
) -> Any
Intercepts a stream-unary RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request_iterator
|
Iterator[Any]
|
The request iterator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseGrpcClientInterceptor.intercept_stream_stream ¶
intercept_stream_stream(
continuation: Callable[
[ClientCallDetails, Iterator[_TRequest]], Any
],
client_call_details: ClientCallDetails,
request_iterator: Iterator[_TRequest],
) -> Any
Intercepts a stream-stream RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request_iterator
|
Iterator[Any]
|
The request iterator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.AsyncClientCallDetails ¶
Bases: _AsyncClientCallDetailsFields, ClientCallDetails
Describes an RPC to be invoked in an asynchronous context.
This class extends grpc.aio.ClientCallDetails and provides additional fields for RPC details.
See https://grpc.github.io/grpc/python/grpc.html#grpc.ClientCallDetails
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.AsyncClientCallDetails.method
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.AsyncClientCallDetails.timeout
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.AsyncClientCallDetails.metadata
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.AsyncClientCallDetails.credentials
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.AsyncClientCallDetails.wait_for_ready
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseAsyncGrpcClientInterceptor ¶
Bases: UnaryUnaryClientInterceptor, UnaryStreamClientInterceptor, StreamUnaryClientInterceptor, StreamStreamClientInterceptor
Base class for asynchronous gRPC client interceptors.
This class provides a base implementation for intercepting asynchronous gRPC client calls. It supports unary-unary, unary-stream, stream-unary, and stream-stream RPCs.
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | |
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseAsyncGrpcClientInterceptor.intercept
abstractmethod
async
¶
Intercepts an asynchronous gRPC client call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The continuation function to call. |
required |
request_or_iterator
|
Any
|
The request or request iterator. |
required |
call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseAsyncGrpcClientInterceptor.intercept_unary_unary
async
¶
intercept_unary_unary(
continuation: Callable[
[ClientCallDetails, _TRequest], Any
],
client_call_details: ClientCallDetails,
request: _TRequest,
) -> Any
Intercepts an asynchronous unary-unary RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request
|
Any
|
The request object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseAsyncGrpcClientInterceptor.intercept_unary_stream
async
¶
intercept_unary_stream(
continuation: Callable[
[ClientCallDetails, _TRequest], Any
],
client_call_details: ClientCallDetails,
request: _TRequest,
) -> Any
Intercepts an asynchronous unary-stream RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request
|
Any
|
The request object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseAsyncGrpcClientInterceptor.intercept_stream_unary
async
¶
intercept_stream_unary(
continuation: Callable[
[
ClientCallDetails,
AsyncIterable[_TRequest] | Iterable[_TRequest],
],
Any,
],
client_call_details: ClientCallDetails,
request_iterator: AsyncIterable[_TRequest]
| Iterable[_TRequest],
) -> Any
Intercepts an asynchronous stream-unary RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request_iterator
|
Iterator[Any]
|
The request iterator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseAsyncGrpcClientInterceptor.intercept_stream_stream
async
¶
intercept_stream_stream(
continuation: Callable[
[
ClientCallDetails,
AsyncIterable[_TRequest] | Iterable[_TRequest],
],
Any,
],
client_call_details: ClientCallDetails,
request_iterator: AsyncIterable[_TRequest]
| Iterable[_TRequest],
) -> Any
Intercepts an asynchronous stream-stream RPC call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable
|
The continuation function to call. |
required |
client_call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
request_iterator
|
Iterator[Any]
|
The request iterator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of the intercepted RPC call. |
Source code in archipy/helpers/interceptors/grpc/base/client_interceptor.py
options: show_root_toc_entry: false heading_level: 3
Base gRPC server interceptor.
archipy.helpers.interceptors.grpc.base.server_interceptor.MethodName ¶
Bases: BaseDTO
A data transfer object (DTO) representing the parsed method name of a gRPC call.
Attributes:
| Name | Type | Description |
|---|---|---|
full_name |
str
|
The full name of the method, including package, service, and method. |
package |
str
|
The package name. |
service |
str
|
The service name. |
method |
str
|
The method name. |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
archipy.helpers.interceptors.grpc.base.server_interceptor.MethodName.full_name
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.server_interceptor.MethodName.package
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.server_interceptor.MethodName.service
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.server_interceptor.MethodName.method
instance-attribute
¶
archipy.helpers.interceptors.grpc.base.server_interceptor.MethodName.model_config
class-attribute
instance-attribute
¶
model_config = ConfigDict(
extra="ignore",
validate_default=True,
from_attributes=True,
frozen=True,
str_strip_whitespace=True,
arbitrary_types_allowed=True,
)
archipy.helpers.interceptors.grpc.base.server_interceptor.BaseGrpcServerInterceptor ¶
Bases: ServerInterceptor
Base class for gRPC server interceptors.
This class provides a base implementation for intercepting gRPC server calls. It allows custom logic to be injected into the request/response flow.
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
archipy.helpers.interceptors.grpc.base.server_interceptor.BaseGrpcServerInterceptor.intercept
abstractmethod
¶
intercept(
method: Callable,
request: object,
context: ServicerContext,
method_name_model: MethodName,
) -> object
Intercepts a gRPC server call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The method to be intercepted. |
required |
request
|
object
|
The request object. |
required |
context
|
ServicerContext
|
The context of the RPC call. |
required |
method_name_model
|
str
|
The full method name (e.g., "/package.Service/Method"). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
object
|
The result of the intercepted method. |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
archipy.helpers.interceptors.grpc.base.server_interceptor.BaseGrpcServerInterceptor.intercept_service ¶
intercept_service(
continuation: Callable[
[HandlerCallDetails], RpcMethodHandler | None
],
handler_call_details: HandlerCallDetails,
) -> grpc.RpcMethodHandler | None
Intercepts the service call and wraps the handler with custom logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable[[HandlerCallDetails], RpcMethodHandler | None]
|
The continuation function to call. |
required |
handler_call_details
|
HandlerCallDetails
|
Details of the handler call. |
required |
Returns:
| Type | Description |
|---|---|
RpcMethodHandler | None
|
grpc.RpcMethodHandler: The wrapped RPC method handler. |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
archipy.helpers.interceptors.grpc.base.server_interceptor.BaseAsyncGrpcServerInterceptor ¶
Bases: ServerInterceptor
Base class for asynchronous gRPC server interceptors.
This class provides a simplified base implementation for intercepting async gRPC server calls. Unlike the synchronous version, async interceptors work differently and don't need the complex handler wrapping logic.
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
archipy.helpers.interceptors.grpc.base.server_interceptor.BaseAsyncGrpcServerInterceptor.intercept
abstractmethod
async
¶
intercept(
method: Callable,
request: object,
context: ServicerContext,
method_name_model: MethodName,
) -> object
Intercepts an async gRPC server call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The method to be intercepted. |
required |
request
|
object
|
The request object. |
required |
context
|
ServicerContext
|
The context of the RPC call. |
required |
method_name_model
|
MethodName
|
The parsed method name containing package, service, and method components. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
object
|
The result of the intercepted method. |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
archipy.helpers.interceptors.grpc.base.server_interceptor.BaseAsyncGrpcServerInterceptor.intercept_service
async
¶
intercept_service(
continuation: Callable[
[HandlerCallDetails],
Awaitable[RpcMethodHandler | None],
],
handler_call_details: HandlerCallDetails,
) -> grpc.RpcMethodHandler | None
Intercepts the service call using the simplified async pattern.
For async gRPC, we don't need the complex handler wrapping that sync interceptors require. Instead, we can use a much simpler pattern where we just await the continuation and then wrap the actual method call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable[[HandlerCallDetails], Awaitable[RpcMethodHandler | None]]
|
The continuation function to call. |
required |
handler_call_details
|
HandlerCallDetails
|
Details of the handler call. |
required |
Returns:
| Type | Description |
|---|---|
RpcMethodHandler | None
|
grpc.RpcMethodHandler: The wrapped RPC method handler. |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
archipy.helpers.interceptors.grpc.base.server_interceptor.parse_method_name ¶
Parses a gRPC method name into its components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method_name
|
str
|
The full method name (e.g., "/package.service/method"). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
MethodName |
MethodName
|
A |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
options: show_root_toc_entry: false heading_level: 3
exception¶
gRPC server interceptor that catches exceptions and converts them to gRPC status codes.
gRPC server interceptor for exception mapping.
archipy.helpers.interceptors.grpc.exception.server_interceptor.GrpcServerExceptionInterceptor ¶
Bases: BaseGrpcServerInterceptor
A sync gRPC server interceptor for centralized exception handling.
This interceptor catches all exceptions thrown by gRPC service methods and converts them to appropriate gRPC errors, eliminating the need for repetitive try-catch blocks in each service method.
Source code in archipy/helpers/interceptors/grpc/exception/server_interceptor.py
archipy.helpers.interceptors.grpc.exception.server_interceptor.GrpcServerExceptionInterceptor.intercept ¶
intercept(
method: Callable,
request: object,
context: ServicerContext,
method_name_model: MethodName,
) -> object
Intercepts a sync gRPC server call and handles exceptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The sync gRPC method being intercepted. |
required |
request
|
object
|
The request object passed to the method. |
required |
context
|
ServicerContext
|
The context of the sync gRPC call. |
required |
method_name_model
|
MethodName
|
The parsed method name containing package, service, and method components. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
object
|
The result of the intercepted gRPC method. |
Note
This method will not return anything if an exception is handled, as the exception handling will abort the gRPC context.
Source code in archipy/helpers/interceptors/grpc/exception/server_interceptor.py
archipy.helpers.interceptors.grpc.exception.server_interceptor.GrpcServerExceptionInterceptor.intercept_service ¶
intercept_service(
continuation: Callable[
[HandlerCallDetails], RpcMethodHandler | None
],
handler_call_details: HandlerCallDetails,
) -> grpc.RpcMethodHandler | None
Intercepts the service call and wraps the handler with custom logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable[[HandlerCallDetails], RpcMethodHandler | None]
|
The continuation function to call. |
required |
handler_call_details
|
HandlerCallDetails
|
Details of the handler call. |
required |
Returns:
| Type | Description |
|---|---|
RpcMethodHandler | None
|
grpc.RpcMethodHandler: The wrapped RPC method handler. |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
archipy.helpers.interceptors.grpc.exception.server_interceptor.AsyncGrpcServerExceptionInterceptor ¶
Bases: BaseAsyncGrpcServerInterceptor
An async gRPC server interceptor for centralized exception handling.
This interceptor catches all exceptions thrown by gRPC service methods and converts them to appropriate gRPC errors, eliminating the need for repetitive try-catch blocks in each service method.
Source code in archipy/helpers/interceptors/grpc/exception/server_interceptor.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
archipy.helpers.interceptors.grpc.exception.server_interceptor.AsyncGrpcServerExceptionInterceptor.intercept
async
¶
intercept(
method: Callable,
request: object,
context: ServicerContext,
method_name_model: MethodName,
) -> object
Intercepts an async gRPC server call and handles exceptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The async gRPC method being intercepted. |
required |
request
|
object
|
The request object passed to the method. |
required |
context
|
ServicerContext
|
The context of the async gRPC call. |
required |
method_name_model
|
MethodName
|
The parsed method name containing package, service, and method components. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
object
|
The result of the intercepted gRPC method. |
Note
This method will not return anything if an exception is handled, as the exception handling will abort the gRPC context.
Source code in archipy/helpers/interceptors/grpc/exception/server_interceptor.py
archipy.helpers.interceptors.grpc.exception.server_interceptor.AsyncGrpcServerExceptionInterceptor.intercept_service
async
¶
intercept_service(
continuation: Callable[
[HandlerCallDetails],
Awaitable[RpcMethodHandler | None],
],
handler_call_details: HandlerCallDetails,
) -> grpc.RpcMethodHandler | None
Intercepts the service call using the simplified async pattern.
For async gRPC, we don't need the complex handler wrapping that sync interceptors require. Instead, we can use a much simpler pattern where we just await the continuation and then wrap the actual method call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable[[HandlerCallDetails], Awaitable[RpcMethodHandler | None]]
|
The continuation function to call. |
required |
handler_call_details
|
HandlerCallDetails
|
Details of the handler call. |
required |
Returns:
| Type | Description |
|---|---|
RpcMethodHandler | None
|
grpc.RpcMethodHandler: The wrapped RPC method handler. |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
options: show_root_toc_entry: false heading_level: 3
rate_limit¶
gRPC server interceptors that enforce decorator-declared Redis rate limits on servicer methods.
gRPC server interceptors that enforce decorator-declared Redis rate limits.
archipy.helpers.interceptors.grpc.rate_limit.grpc_rate_limit_interceptor.GrpcServerRateLimitInterceptor ¶
Bases: _GrpcRateLimitInterceptorMixin, BaseGrpcServerInterceptor
Sync gRPC server interceptor for decorator-declared Redis rate limits.
Source code in archipy/helpers/interceptors/grpc/rate_limit/grpc_rate_limit_interceptor.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
archipy.helpers.interceptors.grpc.rate_limit.grpc_rate_limit_interceptor.GrpcServerRateLimitInterceptor.intercept ¶
intercept(
method: Callable,
request: object,
context: ServicerContext,
method_name_model: MethodName,
) -> object
Enforce stacked rate-limit windows before invoking the RPC handler.
Source code in archipy/helpers/interceptors/grpc/rate_limit/grpc_rate_limit_interceptor.py
archipy.helpers.interceptors.grpc.rate_limit.grpc_rate_limit_interceptor.GrpcServerRateLimitInterceptor.intercept_service ¶
intercept_service(
continuation: Callable[
[HandlerCallDetails], RpcMethodHandler | None
],
handler_call_details: HandlerCallDetails,
) -> grpc.RpcMethodHandler | None
Intercepts the service call and wraps the handler with custom logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable[[HandlerCallDetails], RpcMethodHandler | None]
|
The continuation function to call. |
required |
handler_call_details
|
HandlerCallDetails
|
Details of the handler call. |
required |
Returns:
| Type | Description |
|---|---|
RpcMethodHandler | None
|
grpc.RpcMethodHandler: The wrapped RPC method handler. |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
archipy.helpers.interceptors.grpc.rate_limit.grpc_rate_limit_interceptor.AsyncGrpcServerRateLimitInterceptor ¶
Bases: _GrpcRateLimitInterceptorMixin, BaseAsyncGrpcServerInterceptor
Async gRPC server interceptor for decorator-declared Redis rate limits.
Source code in archipy/helpers/interceptors/grpc/rate_limit/grpc_rate_limit_interceptor.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | |
archipy.helpers.interceptors.grpc.rate_limit.grpc_rate_limit_interceptor.AsyncGrpcServerRateLimitInterceptor.intercept
async
¶
intercept(
method: Callable,
request: object,
context: ServicerContext,
method_name_model: MethodName,
) -> object
Enforce stacked rate-limit windows before invoking the async RPC handler.
Source code in archipy/helpers/interceptors/grpc/rate_limit/grpc_rate_limit_interceptor.py
archipy.helpers.interceptors.grpc.rate_limit.grpc_rate_limit_interceptor.AsyncGrpcServerRateLimitInterceptor.intercept_service
async
¶
intercept_service(
continuation: Callable[
[HandlerCallDetails],
Awaitable[RpcMethodHandler | None],
],
handler_call_details: HandlerCallDetails,
) -> grpc.RpcMethodHandler | None
Intercepts the service call using the simplified async pattern.
For async gRPC, we don't need the complex handler wrapping that sync interceptors require. Instead, we can use a much simpler pattern where we just await the continuation and then wrap the actual method call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuation
|
Callable[[HandlerCallDetails], Awaitable[RpcMethodHandler | None]]
|
The continuation function to call. |
required |
handler_call_details
|
HandlerCallDetails
|
Details of the handler call. |
required |
Returns:
| Type | Description |
|---|---|
RpcMethodHandler | None
|
grpc.RpcMethodHandler: The wrapped RPC method handler. |
Source code in archipy/helpers/interceptors/grpc/base/server_interceptor.py
options: show_root_toc_entry: false heading_level: 3
Rate-limit identity helpers for gRPC server RPCs.
archipy.helpers.interceptors.grpc.rate_limit.identifiers.invocation_metadata_to_dict ¶
Normalize gRPC invocation metadata to a string dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata_items
|
Iterable[Any]
|
Items from |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Metadata keys mapped to decoded string values. |
Source code in archipy/helpers/interceptors/grpc/rate_limit/identifiers.py
archipy.helpers.interceptors.grpc.rate_limit.identifiers.extract_bearer_token_from_metadata ¶
Extract a Bearer token from gRPC invocation metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata_items
|
Iterable[Any]
|
Items from |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The raw JWT string, or None when metadata is missing or not Bearer. |
Source code in archipy/helpers/interceptors/grpc/rate_limit/identifiers.py
archipy.helpers.interceptors.grpc.rate_limit.identifiers.resolve_jwt_access_token_sub_from_metadata ¶
resolve_jwt_access_token_sub_from_metadata(
metadata_items: Iterable[Any],
*,
auth_config: AuthConfig | None = None,
) -> str | None
Resolve the user identity from a verified JWT in gRPC invocation metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata_items
|
Iterable[Any]
|
Items from |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. When None, uses global config. |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
The token |
Source code in archipy/helpers/interceptors/grpc/rate_limit/identifiers.py
options: show_root_toc_entry: false heading_level: 3