Dependency Injection¶
Depends¶
Depends
¶
Marker for dependency injection.
Usage
async def get_db() -> Session: return Session()
@broker.subscriber(queue="orders") async def handle( order: Order, db: Annotated[Session, Depends(get_db)], ) -> None: ...
Per-message lifetime: a fresh dependency graph is resolved for each
message. A generator (or async generator) dependency is supported too —
yield the value; the code after yield runs as teardown once the
handler completes (see :class:~rabbitkit.di.resolver.DependencyScope).
Source code in src/rabbitkit/di/depends.py
Header / Path / Context¶
Header
¶
Marker for AMQP header extraction.
Usage
@broker.subscriber(queue="orders") async def handle( order: Order, tenant: Annotated[str, Header("x-tenant")], ) -> None: ...
H10 — optional values: pass default= to make a missing header
resolve to that value instead of raising, or simply give the parameter
itself a Python default (tenant: Annotated[str | None,
Header("x-tenant")] = None) — the resolver falls back to the parameter
default when the marker has none. With neither, a missing header raises
MissingDependencyError (PERMANENT — settles straight to the DLQ,
matching the KeyError this replaces).
Source code in src/rabbitkit/di/context.py
Path
¶
Marker for topic wildcard segment extraction.
Usage
@broker.subscriber(queue="events", routing_key="events.*.#") async def handle( event: Event, level: Annotated[str, Path("level")], ) -> None: ...
H10 — optional values: pass default= to make a missing path segment
resolve to that value instead of raising, or simply give the parameter
itself a Python default (level: Annotated[str | None, Path("level")]
= None) — the resolver falls back to the parameter default when the
marker has none. With neither, a missing segment raises
MissingDependencyError (PERMANENT — settles straight to the DLQ,
matching the KeyError this replaces).
Source code in src/rabbitkit/di/context.py
Context
¶
Marker for context value injection.
Usage
@broker.subscriber(queue="orders") async def handle( order: Order, app_name: Annotated[str, Context("app")], ) -> None: ...
H10 — optional values: pass default= to make a missing context key
resolve to that value instead of raising, or simply give the parameter
itself a Python default (app_name: Annotated[str | None, Context("app")]
= None) — the resolver falls back to the parameter default when the
marker has none. With neither, a missing key raises
MissingDependencyError (PERMANENT — settles straight to the DLQ,
matching the KeyError this replaces).
Source code in src/rabbitkit/di/context.py
ContextRepo¶
ContextRepo
¶
Context repository for global and per-request values.
Global values are shared across all messages (thread-safe via a lock).
Local values use contextvars.ContextVar for correct isolation across
both sync threads AND async coroutines on the same event loop —
threading.local() would bleed context between concurrent coroutines
sharing one OS thread in an async transport.
Source code in src/rabbitkit/di/context.py
Methods:¶
set_global(key: str, value: Any) -> None
¶
set_local(key: str, value: Any) -> None
¶
Set a per-request context value.
Uses ContextVar.set with an immutable copy so that each
coroutine/task gets its own isolated snapshot (contextvars
copy-on-write semantics).
Source code in src/rabbitkit/di/context.py
get(key: str, default: Any = None) -> Any
¶
Get a context value. Local overrides global.
Source code in src/rabbitkit/di/context.py
clear_local() -> None
¶
has(key: str) -> bool
¶
Check if a key exists in either local or global context.
DIResolver¶
DIResolver
¶
Resolves handler parameters.
Resolution rules (Contract 4): 1. Annotated with DI marker (Depends/Header/Path/Context) → resolve via marker 2. Type is RabbitMessage → inject the message object 3. Remaining unannotated parameters → ONE body-bound parameter allowed 4. Multiple body-like parameters → ConfigurationError at registration time 5. Parameters with defaults → use default if no other resolution applies
Constraint: At most one body-bound parameter per handler.
Source code in src/rabbitkit/di/resolver.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 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 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 503 | |
Methods:¶
validate_handler(handler: Callable[..., Any]) -> None
¶
Validate handler signature at registration time.
Raises ConfigurationError for:
- args or *kwargs
- Multiple body-like parameters
- An annotation that looks like an unresolved DI marker (L11) — see
_looks_like_unresolved_di_marker
Source code in src/rabbitkit/di/resolver.py
resolve(handler: Callable[..., Any], message: RabbitMessage, context_repo: ContextRepo | None, body: Any, scope: DependencyScope | None = None) -> dict[str, Any]
¶
Resolve all handler parameters at message processing time.
Source code in src/rabbitkit/di/resolver.py
resolve_async(handler: Callable[..., Any], message: RabbitMessage, context_repo: ContextRepo | None, body: Any, scope: DependencyScope | None = None) -> dict[str, Any]
async
¶
Resolve all handler parameters, supporting async generator dependencies.
Source code in src/rabbitkit/di/resolver.py
DependencyScope¶
DependencyScope
¶
Tracks generator dependencies for cleanup after handler completes.
Source code in src/rabbitkit/di/resolver.py
Methods:¶
cleanup() -> None
¶
Close all sync generators (in reverse order).
Each generator's teardown is isolated: a raising teardown is logged
and skipped so one misbehaving generator does not leak the rest or
prevent clear() from running.
Source code in src/rabbitkit/di/resolver.py
cleanup_async() -> None
async
¶
Close all generators (async generators + sync generators, in reverse order).
Each generator's teardown is isolated: a raising async teardown is
logged and skipped, and the sync-generator pass always runs even if an
async generator raised. clear() always runs.