Repository navigation
Open
Description
Description
Symfony 8.2 is introducing Locking controllers
https://symfony.com/blog/new-in-symfony-8-2-locking-controllers
I wonder if such similar feature wouldn't be useful for Api Operation too with Provider/Processor
Maybe by adding a lock option to the Operation ?
An operation option mirroring Symfony's attribute:
new Post(
uriTemplate: '/foo/{uuid}/finalize',
lock: new Lock('finalize-foo-{uuid}', ttl: 180), // could use new Expression() for the key
processor: MyProcessor::class,
)
- Key:
{name}placeholders resolved from URI variables / request attributes, or anExpressionwithrequest. A key shared across operations serializes different operations on the same resource. Payload- or object-based keys are out of scope: the lock is taken before the object exists, and those cases stay in the processor. - Acquire before the provider (in both modes), release after the processor returns, so after the flush/commit, before serialization. On any exception between the two (403, 422, 500), release in a
kernel.exception/kernel.terminatesafety net. - Not acquired: a dedicated exception mapped to
409 Conflictby default, overridable throughexception_to_status.blocking: truewaits instead. - Same caveats as Symfony, worth documenting: the TTL is not refreshed automatically (the default 30s is short for an operation calling a slow third party), and the lock only serializes HTTP requests. A worker or webhook writing the same rows still needs a DB lock.
Example
Some use case:
- Avoid duplicate entity creations
- Avoid patching the same entity at the same time
- For custom routes, simplify the way we have to handle concurrency (with lock or transaction in our processor)
I know this can be handle "easyly" with a custom Processor which inject the lockFactory or use the entityManager::wrapInTransaction method. But since I'm having lot of custom Processor which need to prevent concurrency, I feel like a generic feature might be useful.
WDYT @soyuka ?
Reacted by Jules Rabus and Nozarashi
Metadata
Metadata
Assignees
Labels
No labels
Activity