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
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 163 | |
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseGrpcClientInterceptor.intercept
abstractmethod
¶
intercept(
method: Callable[
[TRequest, ClientCallDetails], TResult
],
request_or_iterator: TRequest,
call_details: ClientCallDetails,
) -> TResult
Intercepts a gRPC client call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The continuation function to call. |
required |
request_or_iterator
|
TRequest
|
The request or request iterator. |
required |
call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
TResult |
TResult
|
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], _TResult
],
client_call_details: ClientCallDetails,
request: _TRequest,
) -> _TResult
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
|
_TRequest
|
The request object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
_TResult |
_TResult
|
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], _TResult
],
client_call_details: ClientCallDetails,
request: _TRequest,
) -> _TResult
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
|
_TRequest
|
The request object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
_TResult |
_TResult
|
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]], _TResult
],
client_call_details: ClientCallDetails,
request_iterator: Iterator[_TRequest],
) -> _TResult
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[_TRequest]
|
The request iterator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
_TResult |
_TResult
|
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]], _TResult
],
client_call_details: ClientCallDetails,
request_iterator: Iterator[_TRequest],
) -> _TResult
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[_TRequest]
|
The request iterator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
_TResult |
_TResult
|
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
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 294 295 296 297 298 299 300 | |
archipy.helpers.interceptors.grpc.base.client_interceptor.BaseAsyncGrpcClientInterceptor.intercept
abstractmethod
async
¶
intercept(
method: Callable[
[TRequest, ClientCallDetails], Awaitable[TResult]
],
request_or_iterator: TRequest,
call_details: ClientCallDetails,
) -> TResult
Intercepts an asynchronous gRPC client call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The continuation function to call. |
required |
request_or_iterator
|
TRequest
|
The request or request iterator. |
required |
call_details
|
ClientCallDetails
|
Details of the RPC call. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
TResult |
TResult
|
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], Awaitable[_TResult]
],
client_call_details: ClientCallDetails,
request: _TRequest,
) -> _TResult
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
|
_TRequest
|
The request object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
_TResult |
_TResult
|
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], Awaitable[_TResult]
],
client_call_details: ClientCallDetails,
request: _TRequest,
) -> _TResult
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
|
_TRequest
|
The request object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
_TResult |
_TResult
|
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],
],
Awaitable[_TResult],
],
client_call_details: ClientCallDetails,
request_iterator: AsyncIterable[_TRequest]
| Iterable[_TRequest],
) -> _TResult
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[_TRequest]
|
The request iterator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
_TResult |
_TResult
|
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],
],
Awaitable[_TResult],
],
client_call_details: ClientCallDetails,
request_iterator: AsyncIterable[_TRequest]
| Iterable[_TRequest],
) -> _TResult
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[_TRequest]
|
The request iterator. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
_TResult |
_TResult
|
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[..., Any],
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
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 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 | |
archipy.helpers.interceptors.grpc.base.server_interceptor.BaseAsyncGrpcServerInterceptor.intercept
abstractmethod
async
¶
intercept(
method: Callable[..., Any],
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_stream
async
¶
intercept_stream(
method: Callable[..., Any],
request: object,
context: ServicerContext,
method_name_model: MethodName,
) -> AsyncIterator[object]
Intercepts an async gRPC call whose response is a stream.
Response-streaming handlers must stay async generators, so they cannot go through
the coroutine-based intercept. The default runs intercept around the handler
invocation (enough for pre-call checks) and then relays the produced stream.
Override to wrap the iteration itself (e.g. for error handling or metrics).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The streaming method to be intercepted. |
required |
request
|
object
|
The request object (or async iterator for stream requests). |
required |
context
|
ServicerContext
|
The context of the RPC call. |
required |
method_name_model
|
MethodName
|
The parsed method name. |
required |
Yields:
| Name | Type | Description |
|---|---|---|
object |
AsyncIterator[object]
|
Each response message. |
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.iterate_stream_result
async
¶
Iterate the outcome of invoking a response-streaming async gRPC handler.
Handlers are either async generators (yield responses) or coroutines that push
responses through context.write and return None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
object
|
The value returned by calling the handler. |
required |
Yields:
| Name | Type | Description |
|---|---|---|
object |
AsyncIterator[object]
|
Each response message produced by the 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¶
The gRPC exception interceptor lives in archipy.helpers.errors.grpc; see Errors.
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.logger
module-attribute
¶
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
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 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 | |
archipy.helpers.interceptors.grpc.rate_limit.grpc_rate_limit_interceptor.GrpcServerRateLimitInterceptor.intercept ¶
intercept(
method: Callable[..., Any],
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
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 | |
archipy.helpers.interceptors.grpc.rate_limit.grpc_rate_limit_interceptor.AsyncGrpcServerRateLimitInterceptor.intercept
async
¶
intercept(
method: Callable[..., Any],
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_stream
async
¶
intercept_stream(
method: Callable[..., Any],
request: object,
context: ServicerContext,
method_name_model: MethodName,
) -> AsyncIterator[object]
Intercepts an async gRPC call whose response is a stream.
Response-streaming handlers must stay async generators, so they cannot go through
the coroutine-based intercept. The default runs intercept around the handler
invocation (enough for pre-call checks) and then relays the produced stream.
Override to wrap the iteration itself (e.g. for error handling or metrics).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Callable
|
The streaming method to be intercepted. |
required |
request
|
object
|
The request object (or async iterator for stream requests). |
required |
context
|
ServicerContext
|
The context of the RPC call. |
required |
method_name_model
|
MethodName
|
The parsed method name. |
required |
Yields:
| Name | Type | Description |
|---|---|---|
object |
AsyncIterator[object]
|
Each response message. |
Source code in archipy/helpers/interceptors/grpc/base/server_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[object]
|
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[object]
|
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[object],
*,
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[object]
|
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