Skip to content

Commit

Permalink
Lock: Add automatic lock system.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Aug 1, 2021
1 parent 5f87315 commit 286f458
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ You can always choose your own strategy for generating numbers:
$generator->setStrategy();
```

Protection duplicate number generation
--------------------------------------

This tool automatically protects you from generating a duplicate number. To protect you, an automatic lock (see the `baraja-core/lock` library for more information) is used, which allows only one number to be generated at a time, while competing processes in other threads are suspended in the meantime.

📄 License
-----------

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
}
],
"require": {
"php": "^8.0"
"php": "^8.0",
"baraja-core/lock": "^1.0"
},
"require-dev": {
"phpstan/phpstan": "^0.12.74",
Expand Down
5 changes: 4 additions & 1 deletion src/VariableGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Baraja\VariableGenerator;


use Baraja\Lock\Lock;
use Baraja\VariableGenerator\Order\DefaultOrderVariableLoader;
use Baraja\VariableGenerator\Order\OrderEntity;
use Baraja\VariableGenerator\Strategy\FormatStrategy;
Expand Down Expand Up @@ -35,8 +36,10 @@ public function __construct(
* Generate new variable symbol by last variable.
* In case of invalid last symbol or init, use first valid symbol by specific strategy.
*/
public function generate(?string $last = null): int
public function generate(?string $last = null, string $transactionName = 'variable-generator'): int
{
Lock::wait($transactionName);
Lock::startTransaction($transactionName);
$last ??= $this->variableLoader->getCurrent();
$new = $last === null
? $this->strategy->getFirst()
Expand Down

0 comments on commit 286f458

Please sign in to comment.