componenta / cqrs-retry
Retry middleware for Componenta CQRS commands
Requires
- php: >=8.4
- componenta/config: ^3.0.0
- componenta/cqrs: ^4.0.0
- psr/container: ^2.0
Requires (Dev)
- pestphp/pest: ^4.0
- phpstan/phpstan: ^2.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-13 23:22:41 UTC
README
Retry middleware for CQRS v4 commands marked with #[Componenta\CQRS\Retry\Attribute\Retry].
composer require componenta/cqrs-retry
Register the CQRS and retry providers and add RetryMiddleware to the command middleware chain where transient failures may be retried.
return [ new Componenta\CQRS\ConfigProvider(), new Componenta\CQRS\Retry\ConfigProvider(), ];
The provider registers RetryMiddleware. The middleware reads Retry through the core CommandMetadataProviderInterface. Its default Reflection implementation creates fresh metadata on each call, independently of handler maps and application environment.
Middleware ordering is controlled by the application. With transaction middleware, the two common compositions have different semantics:
RetryMiddleware
TransactionMiddleware
handler
creates a fresh transaction for every retry attempt. By contrast:
TransactionMiddleware
RetryMiddleware
handler
keeps all attempts inside one surrounding transaction. The package does not reject either topology; choose the one that matches the desired transaction semantics.
RetryableExceptionInterface marks transient exceptions that may be retried. Additional throwable classes can be configured explicitly in the middleware constructor. Retry attempts, delay, multiplier, maximum delay, and jitter are controlled by #[Retry] and middleware configuration.
For controlled waiting and randomness, the constructor also accepts optional closures:
sleep(int $milliseconds): voidwaits between attempts.randomInt(int $min, int $max): intreturns an integer within the inclusive range.
The defaults use PHP's usleep() and random_int(). The registered factory uses these defaults. Supplying the closures supports deterministic tests of actual retry attempts and delays without sleeping or calling private methods.