Causation
The core of the command dispatcher.
Note
Middleware uses ContextVar to store state per thread or async
tasks.
Usage
Tracks the requests that trigger other request. The middleware will add a causation and correlation identifier to every message.
The causation identifier relates to the request that triggered this request.
The correlation identifier relates to the original request that triggered all parent requests. The request passed to the message bus.
This middleware relies on the Identity context added by the
IdentityMiddleware.
Registration
Add the middleware to your bus. This middleware should come after the
IdentityMiddleware.
bus = (
banshee.builder()
.with_middleware(banshee.IdentityMiddleware())
.with_middleware(banshee.CausationMiddleware())
.with_locator(registry)
.build()
)
Context
A Causation context instance stores the causation and correlation
identifiers for the message.
causation_id = message[banshee.Causation].causation_id
correlation_id = message[banshee.Causation].correlation_id
Reference
- class banshee.Causation(causation_id, correlation_id)
Bases:
objectCausation context.
Holds the identity of the request that created this request, and the root request for correlating related events.
- causation_id: UUID
parent identifier
- correlation_id: UUID
root identifier
- class banshee.CausationMiddleware
Bases:
MiddlewareCausation middleware.
Add causation and correlation identifiers to messages based on the messages
Identitycontext.The
correlation_idwill be set based on the first message in a chainsunique_id, and thecausation_idwill be set based on the previous message in a chainsunique_id.When no
Identitycontext is present this handler will pass the message unchanged.- async __call__(message, handle)
Handle message.
Add causation and correlation identifiers to the message and forward it to the next handler in the chain.
- Parameters:
message (Message[T]) – message to process
handle (HandleMessage) – next middleware invoker
- Returns:
processed message
- Return type:
Message[T]