Nalix.Runtime.Middleware
On this page
Namespaces: Nalix.Runtime.Middleware, Nalix.Runtime.Middleware.Standard, Nalix.Runtime.Throttling
Assembly: Nalix.Runtime
Runtime middleware forms the packet execution pipeline between PacketContext<TPacket> initialization and [PacketHandler] invocation. It enforces fail-closed permissions, per-policy and per-endpoint rate limiting, handler timeouts, and outbound post-processing.
Source Mapping
src/Nalix.Abstractions/Middleware/IPacketMiddleware.cssrc/Nalix.Abstractions/Middleware/MiddlewareOrderAttribute.cssrc/Nalix.Abstractions/Middleware/MiddlewareStageAttribute.cssrc/Nalix.Runtime/Middleware/MiddlewarePipeline.cssrc/Nalix.Runtime/Middleware/Standard/PermissionMiddleware.cssrc/Nalix.Runtime/Middleware/Standard/RateLimitMiddleware.cssrc/Nalix.Runtime/Middleware/Standard/TimeoutMiddleware.cssrc/Nalix.Runtime/Throttling/PolicyRateLimiter.cssrc/Nalix.Runtime/Throttling/TokenBucketLimiter.cs
Execution Lifecycle
flowchart TD
Start(["ExecuteAsync(context, dispatch, descriptor, ct)"]) --> Inbound["Inbound Middleware (Ascending Order)"]
Inbound -->|Calls next(ct)| Handler["ExecuteTerminalHandler (CanExecute + Invoker)"]
Inbound -->|Short-circuits (no next)| End(["Pipeline Returns"])
Handler --> Always["OutboundAlways Middleware (Descending Order)"]
Always --> Decision{"!context.SkipOutbound && !ct.IsCancellationRequested?"}
Decision -->|Yes| Outbound["Outbound Middleware (Descending Order)"]
Decision -->|No| End
Outbound --> EndMiddleware Stages (MiddlewareStage)
| Stage | Attribute Declaration | Sort Order | Triggered When |
|---|---|---|---|
Inbound | [MiddlewareStage(MiddlewareStage.Inbound)] (default) | Ascending Order | Before the packet handler runs. Short-circuiting skips the handler and all outbound stages. |
OutboundAlways | [MiddlewareStage(MiddlewareStage.Outbound, AlwaysExecute = true)] | Descending Order | Always runs after the handler returns or cancels via OperationCanceledException. |
Outbound | [MiddlewareStage(MiddlewareStage.Outbound)] | Descending Order | Runs after OutboundAlways only when !context.SkipOutbound and !token.IsCancellationRequested. |
Both | [MiddlewareStage(MiddlewareStage.Both)] | Ascending (Inbound) & Descending (Outbound / OutboundAlways) | Registers the same middleware instance in both inbound and outbound lists. |
Built-in Standard Middleware (Nalix.Runtime.Middleware.Standard)
| Middleware | Order | Stage | Runtime Behavior |
|---|---|---|---|
| PermissionMiddleware | -50 | Inbound | Fail-closed [PacketPermission] check (MinimumLevel or StrictMatch). Emits Directive(FAIL, UNAUTHORIZED) gated by DirectiveGuard. |
RateLimitMiddleware | 50 | Inbound | Evaluates PolicyRateLimiter when [PacketRateLimit] is present, or global TokenBucketLimiter otherwise. Emits Directive(FAIL, RATE_LIMITED, RETRY). |
| TimeoutMiddleware | 75 | Inbound | Enforces [PacketTimeout(ms)] using pooled PooledCancellationTokenSource instances. Emits Directive(TIMEOUT, TIMEOUT, RETRY). |