Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/1-essentials/01-routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -760,14 +760,14 @@ final readonly class Auth implements RouteDecorator

## Idempotent routes

For operations like payment processing or order creation, retrying the same request should not produce duplicate side effects. Tempest provides the {b`Tempest\Idempotency\Attributes\Idempotent`} route decorator to handle this. Clients send an `Idempotency-Key` header; the first request executes normally and caches the response, while subsequent requests with the same key replay the cached response.
For operations like payment processing or order creation, retrying the same request should not produce duplicate side effects. Tempest provides the {b`Tempest\Idempotency\Idempotent`} route decorator to handle this. Clients send an `Idempotency-Key` header; the first request executes normally and caches the response, while subsequent requests with the same key replay the cached response.

```php app/OrderController.php
use Tempest\Router\Post;
use Tempest\Http\Response;
use Tempest\Http\GenericResponse;
use Tempest\Http\Status;
use Tempest\Idempotency\Attributes\Idempotent;
use Tempest\Idempotency\Idempotent;

final readonly class OrderController
{
Expand Down
10 changes: 5 additions & 5 deletions docs/2-features/10-command-bus.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ Note that async command handling is still an early feature, and will receive man

## Idempotent commands

Commands that should not be processed more than once—such as payment processing or invoice imports—can be marked with {b`Tempest\Idempotency\Attributes\Idempotent`}. The attribute can be placed on the command class or on the handler method. Duplicate dispatches with the same payload are silently skipped.
Commands that should not be processed more than once—such as payment processing or invoice imports—can be marked with {b`Tempest\Idempotency\Idempotent`}. The attribute can be placed on the command class or on the handler method. Duplicate dispatches with the same payload are silently skipped.

```php
// app/ImportInvoicesCommand.php

use Tempest\Idempotency\Attributes\Idempotent;
use Tempest\Idempotency\Idempotent;

#[Idempotent]
final readonly class ImportInvoicesCommand
Expand All @@ -157,7 +157,7 @@ Alternatively, the attribute can be placed on the handler method instead:
// app/ImportInvoicesHandler.php

use Tempest\CommandBus\CommandHandler;
use Tempest\Idempotency\Attributes\Idempotent;
use Tempest\Idempotency\Idempotent;

final class ImportInvoicesHandler
{
Expand All @@ -172,7 +172,7 @@ By default, the deduplication key is derived from the command's properties. Two
```php
// app/ProcessPaymentCommand.php

use Tempest\Idempotency\Attributes\Idempotent;
use Tempest\Idempotency\Idempotent;
use Tempest\Idempotency\HasIdempotencyKey;

#[Idempotent]
Expand All @@ -190,7 +190,7 @@ final readonly class ProcessPaymentCommand implements HasIdempotencyKey
}
```

When using explicit keys, the payload fingerprint is still verified. Dispatching the same key with a different payload throws {b`Tempest\Idempotency\Exceptions\IdempotencyKeyWasAlreadyUsed`}.
When using explicit keys, the payload fingerprint is still verified. Dispatching the same key with a different payload throws {b`Tempest\Idempotency\IdempotencyKeyWasAlreadyUsed`}.

:::info
Read the full [idempotency documentation](./19-idempotency.md) for details on configuration, TTL overrides, custom stores, and HTTP route idempotency.
Expand Down
34 changes: 17 additions & 17 deletions docs/2-features/19-idempotency.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ The `tempest/idempotency` package solves this by storing the result of the first

## Idempotent routes

Add the {b`Tempest\Idempotency\Attributes\Idempotent`} attribute to a controller method. Clients send an `{txt}{:hlvalueproperty:Idempotency-Key:}` header with a unique value (typically a UUID). The first request executes normally and caches the response. Subsequent requests with the same key replay the cached response without re-executing the handler.
Add the {b`Tempest\Idempotency\Idempotent`} attribute to a controller method. Clients send an `{txt}{:hlvalueproperty:Idempotency-Key:}` header with a unique value (typically a UUID). The first request executes normally and caches the response. Subsequent requests with the same key replay the cached response without re-executing the handler.

```php app/OrderController.php
use Tempest\Router\Post;
use Tempest\Http\Response;
use Tempest\Http\GenericResponse;
use Tempest\Http\Status;
use Tempest\Idempotency\Attributes\Idempotent;
use Tempest\Idempotency\Idempotent;

final readonly class OrderController
{
Expand Down Expand Up @@ -50,7 +50,7 @@ When a cached response is replayed, the response includes an `{:hl-property:idem

### Supported methods

Idempotency is only supported for `POST` and `PATCH` routes. Applying `#[Idempotent]` to a `GET`, `PUT`, `DELETE`, or other method will throw an {b`Tempest\Idempotency\Exceptions\IdempotencyMethodWasNotSupported`} exception. `GET` is inherently idempotent, `PUT` and `DELETE` are idempotent by definition in HTTP semantics, and only `POST` and `PATCH` produce non-idempotent side effects.
Idempotency is only supported for `POST` and `PATCH` routes. Applying `#[Idempotent]` to a `GET`, `PUT`, `DELETE`, or other method will throw an {b`Tempest\Idempotency\IdempotencyMethodWasNotSupported`} exception. `GET` is inherently idempotent, `PUT` and `DELETE` are idempotent by definition in HTTP semantics, and only `POST` and `PATCH` produce non-idempotent side effects.

### Scope resolver

Expand Down Expand Up @@ -81,13 +81,13 @@ A scope resolver is required. If no implementation of {b`Tempest\Idempotency\Ide

### Per-route overrides

The `#[Idempotent]` attribute accepts optional TTL parameters to override the global configuration on a per-route basis. For route-specific settings like key requirement and header name, use the {b`Tempest\Idempotency\Attributes\IdempotentRoute`} attribute alongside `#[Idempotent]`:
The `#[Idempotent]` attribute accepts optional TTL parameters to override the global configuration on a per-route basis. For route-specific settings like key requirement and header name, use the {b`Tempest\Idempotency\IdempotentRoute`} attribute alongside `#[Idempotent]`:

```php app/PaymentController.php
use Tempest\Router\Post;
use Tempest\Http\Response;
use Tempest\Idempotency\Attributes\Idempotent;
use Tempest\Idempotency\Attributes\IdempotentRoute;
use Tempest\Idempotency\Idempotent;
use Tempest\Idempotency\IdempotentRoute;

final readonly class PaymentController
{
Expand Down Expand Up @@ -125,7 +125,7 @@ The `#[Idempotent]` attribute can be applied at the class level to make all rout
use Tempest\Router\Post;
use Tempest\Router\Patch;
use Tempest\Http\Response;
use Tempest\Idempotency\Attributes\Idempotent;
use Tempest\Idempotency\Idempotent;

#[Idempotent]
final readonly class ApiOrderController
Expand Down Expand Up @@ -165,12 +165,12 @@ A heartbeat mechanism keeps pending records alive during long-running requests,

## Idempotent commands

Add the {b`Tempest\Idempotency\Attributes\Idempotent`} attribute to prevent duplicate dispatches. When the same command is dispatched more than once, the duplicate is silently skipped. The attribute can be placed on the command class or on the handler method.
Add the {b`Tempest\Idempotency\Idempotent`} attribute to prevent duplicate dispatches. When the same command is dispatched more than once, the duplicate is silently skipped. The attribute can be placed on the command class or on the handler method.

On the command class:

```php app/ImportInvoicesCommand.php
use Tempest\Idempotency\Attributes\Idempotent;
use Tempest\Idempotency\Idempotent;

#[Idempotent]
final readonly class ImportInvoicesCommand
Expand All @@ -186,7 +186,7 @@ Or on the handler method:

```php app/ImportInvoicesHandler.php
use Tempest\CommandBus\CommandHandler;
use Tempest\Idempotency\Attributes\Idempotent;
use Tempest\Idempotency\Idempotent;

final class ImportInvoicesHandler
{
Expand All @@ -209,7 +209,7 @@ By default, the idempotency key is derived from a fingerprint of the command's p
Commands can provide an explicit key by implementing the {b`Tempest\Idempotency\HasIdempotencyKey`} interface. This is useful when the deduplication key should be a specific business identifier rather than the full payload:

```php app/ProcessPaymentCommand.php
use Tempest\Idempotency\Attributes\Idempotent;
use Tempest\Idempotency\Idempotent;
use Tempest\Idempotency\HasIdempotencyKey;

#[Idempotent]
Expand All @@ -227,7 +227,7 @@ final readonly class ProcessPaymentCommand implements HasIdempotencyKey
}
```

When using explicit keys, the fingerprint of the command payload is still verified. If the same key is dispatched with a different payload, an {b`Tempest\Idempotency\Exceptions\IdempotencyKeyWasAlreadyUsed`} exception is thrown.
When using explicit keys, the fingerprint of the command payload is still verified. If the same key is dispatched with a different payload, an {b`Tempest\Idempotency\IdempotencyKeyWasAlreadyUsed`} exception is thrown.

### Per-command TTL overrides

Expand All @@ -248,7 +248,7 @@ final readonly class ProcessPaymentCommand { /* … */ }
The idempotency package is configured by creating an `idempotency.config.php` file. All settings have sensible defaults:

```php app/idempotency.config.php
use Tempest\Idempotency\Config\IdempotencyConfig;
use Tempest\Idempotency\IdempotencyConfig;

return new IdempotencyConfig(
header: 'Idempotency-Key',
Expand All @@ -266,14 +266,14 @@ return new IdempotencyConfig(
| `{:hl-property:ttlInSeconds:}` | `86400` (24h) | How long a completed response is cached. |
| `{:hl-property:pendingTtlInSeconds:}` | `60` | How long a pending record is considered active before it can be taken over. |
| `{:hl-property:cachePrefix:}` | `{:hl-value:idempotency:}` | Prefix for cache keys in the idempotency store. |
| `{:hl-property:storeClass:}` | `{:hl-type:CacheIdempotencyStore:}` | The {b`Tempest\Idempotency\Store\IdempotencyStore`} implementation to use. |
| `{:hl-property:storeClass:}` | `{:hl-type:CacheIdempotencyStore:}` | The {b`Tempest\Idempotency\IdempotencyStore`} implementation to use. |

### Custom stores

The default store uses Tempest's [cache](./06-cache.md) component. A custom store can be created by implementing the {b`Tempest\Idempotency\Store\IdempotencyStore`} interface and setting the `storeClass` in the configuration:
The default store uses Tempest's [cache](./06-cache.md) component. A custom store can be created by implementing the {b`Tempest\Idempotency\IdempotencyStore`} interface and setting the `storeClass` in the configuration:

```php app/idempotency.config.php
use Tempest\Idempotency\Config\IdempotencyConfig;
use Tempest\Idempotency\IdempotencyConfig;
use App\RedisIdempotencyStore;

return new IdempotencyConfig(
Expand All @@ -283,5 +283,5 @@ return new IdempotencyConfig(

## Limitations
- **Windows is not supported.** The heartbeat mechanism relies on `pcntl_alarm()` and
`pcntl_signal()`, which are not available on Windows. Attempting to use idempotency on Windows will throw an {b`Tempest\Idempotency\Exceptions\IdempotencyPlatformWasNotSupported`} exception.
`pcntl_signal()`, which are not available on Windows. Attempting to use idempotency on Windows will throw an {b`Tempest\Idempotency\IdempotencyPlatformWasNotSupported`} exception.
- **Stored responses must be serializable.** Response bodies are stored using PHP serialization or JSON encoding. Non-serializable bodies (such as generators or views) are stored as type name strings and will not reproduce the original output on replay.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tempest\Idempotency\Store;
namespace Tempest\Idempotency;

use Tempest\Cache\Cache;
use Tempest\DateTime\Duration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tempest\Idempotency\Fingerprint;
namespace Tempest\Idempotency;

interface CommandFingerprintGenerator
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tempest\Idempotency\Fingerprint;

use RuntimeException;
use Tempest\Idempotency\CommandFingerprintGenerator;
use UnitEnum;

use function Tempest\Support\Json\encode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tempest\Idempotency\Fingerprint;

use Tempest\Http\Request;
use Tempest\Idempotency\HttpFingerprintGenerator;

use function Tempest\Support\Json\encode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tempest\Idempotency\Fingerprint;
namespace Tempest\Idempotency;

use Tempest\Http\Request;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

declare(strict_types=1);

namespace Tempest\Idempotency\Config;

use Tempest\Idempotency\Store\CacheIdempotencyStore;
use Tempest\Idempotency\Store\IdempotencyStore;
namespace Tempest\Idempotency;

final class IdempotencyConfig
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tempest\Idempotency\Exceptions;
namespace Tempest\Idempotency;

use Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tempest\Idempotency\Exceptions;
namespace Tempest\Idempotency;

final class IdempotencyKeyWasAlreadyUsed extends IdempotencyException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

declare(strict_types=1);

namespace Tempest\Idempotency\Exceptions;
namespace Tempest\Idempotency;

use Tempest\Http\Method;
use Tempest\Idempotency\SupportedMethod;

final class IdempotencyMethodWasNotSupported extends IdempotencyException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tempest\Idempotency\Exceptions;
namespace Tempest\Idempotency;

final class IdempotencyPlatformWasNotSupported extends IdempotencyException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tempest\Idempotency\Store;
namespace Tempest\Idempotency;

final readonly class IdempotencyRecord
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tempest\Idempotency\Store;
namespace Tempest\Idempotency;

enum IdempotencyState: string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tempest\Idempotency\Store;
namespace Tempest\Idempotency;

interface IdempotencyStore
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tempest\Idempotency\Attributes;
namespace Tempest\Idempotency;

use Attribute;
use Tempest\Idempotency\Middleware\IdempotencyMiddleware;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tempest\Idempotency\Attributes;
namespace Tempest\Idempotency;

use Attribute;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Tempest\Container\Container;
use Tempest\Container\Initializer;
use Tempest\Container\Singleton;
use Tempest\Idempotency\Fingerprint\CommandFingerprintGenerator;
use Tempest\Idempotency\CommandFingerprintGenerator;
use Tempest\Idempotency\Fingerprint\ObjectFingerprintGenerator;

final readonly class CommandFingerprintGeneratorInitializer implements Initializer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Tempest\Container\Container;
use Tempest\Container\Initializer;
use Tempest\Container\Singleton;
use Tempest\Idempotency\Fingerprint\HttpFingerprintGenerator;
use Tempest\Idempotency\Fingerprint\RequestFingerprintGenerator;
use Tempest\Idempotency\HttpFingerprintGenerator;

final readonly class IdempotencyFingerprintGeneratorInitializer implements Initializer
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Tempest\Container\Container;
use Tempest\Container\Initializer;
use Tempest\Container\Singleton;
use Tempest\Idempotency\Config\IdempotencyConfig;
use Tempest\Idempotency\Store\IdempotencyStore;
use Tempest\Idempotency\IdempotencyConfig;
use Tempest\Idempotency\IdempotencyStore;

final readonly class IdempotencyInitializer implements Initializer
{
Expand Down
20 changes: 10 additions & 10 deletions packages/idempotency/src/Middleware/IdempotencyMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
use Tempest\Http\Request;
use Tempest\Http\Response;
use Tempest\Http\Status;
use Tempest\Idempotency\Attributes\Idempotent;
use Tempest\Idempotency\Attributes\IdempotentRoute;
use Tempest\Idempotency\Config\IdempotencyConfig;
use Tempest\Idempotency\Exceptions\IdempotencyMethodWasNotSupported;
use Tempest\Idempotency\Exceptions\IdempotencyPlatformWasNotSupported;
use Tempest\Idempotency\Fingerprint\HttpFingerprintGenerator;
use Tempest\Idempotency\HttpFingerprintGenerator;
use Tempest\Idempotency\IdempotencyConfig;
use Tempest\Idempotency\IdempotencyMethodWasNotSupported;
use Tempest\Idempotency\IdempotencyPlatformWasNotSupported;
use Tempest\Idempotency\IdempotencyRecord;
use Tempest\Idempotency\IdempotencyScopeResolver;
use Tempest\Idempotency\Store\IdempotencyRecord;
use Tempest\Idempotency\Store\IdempotencyState;
use Tempest\Idempotency\Store\IdempotencyStore;
use Tempest\Idempotency\Store\StoredResponse;
use Tempest\Idempotency\IdempotencyState;
use Tempest\Idempotency\IdempotencyStore;
use Tempest\Idempotency\Idempotent;
use Tempest\Idempotency\IdempotentRoute;
use Tempest\Idempotency\StoredResponse;
use Tempest\Idempotency\Support\HeartbeatRenewer;
use Tempest\Idempotency\Support\IdempotencyKeyResolver;
use Tempest\Idempotency\Support\ProcessingOwner;
Expand Down
Loading