Skip to content
Merged
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
9 changes: 6 additions & 3 deletions docs/sequence-providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ Each algorithm class using ``GetSequence`` provides these static methods:

- ``setSequenceProvider(SequenceProviderInterface $provider)``
- ``resetSequenceProvider()``
- ``useFilesystemSequenceProvider(?string $baseDirectory = null, int $waitTime = 100, int $maxAttempts = 10)``
- ``useFilesystemSequenceProvider(?string $baseDirectory = null, int $waitTime = 1000, int $maxAttempts = 1000)``
- ``useInMemorySequenceProvider()``
- ``useSimpleCacheSequenceProvider(CacheInterface $cache, string $prefix = 'uid:seq:', int $waitTime = 100, int $maxAttempts = 10)``
- ``useSimpleCacheSequenceProvider(CacheInterface $cache, string $prefix = 'uid:seq:', int $waitTime = 1000, int $maxAttempts = 1000)``
- ``useSequenceCallback(callable $callback)``

Defaults are tuned for better contention tolerance in parallel workloads.
If you prefer faster fail behavior, set lower ``waitTime``/``maxAttempts`` explicitly.

Example: Process-Local In-Memory
--------------------------------

Expand Down Expand Up @@ -80,4 +83,4 @@ Implement:

public function next(string $type, int $machineId, int $timestamp): int;

The return value must be a non-negative integer sequence.
The return value should be a positive integer sequence.
4 changes: 2 additions & 2 deletions src/Sequence/FilesystemSequenceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

public function __construct(
?string $baseDirectory = null,
private int $waitTime = 100,
private int $maxAttempts = 10,
private int $waitTime = 1_000,
private int $maxAttempts = 1_000,
) {
$this->baseDirectory = $baseDirectory ?: sys_get_temp_dir();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Sequence/PsrSimpleCacheSequenceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
public function __construct(
private CacheInterface $cache,
private string $prefix = 'uid:seq:',
private int $waitTime = 100,
private int $maxAttempts = 10,
private int $waitTime = 1_000,
private int $maxAttempts = 1_000,
?callable $synchronizer = null,
) {
$this->synchronizer = $synchronizer ? $synchronizer(...) : null;
Expand Down
3 changes: 3 additions & 0 deletions src/Support/FileLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public static function acquire(
string $openErrorMessage,
string $lockErrorMessage,
) {
$waitTime = max(100, $waitTime);
$maxAttempts = max(1, $maxAttempts);

($handle = fopen($path, 'c+')) || throw new FileLockException($openErrorMessage);

for ($attempt = 0; $attempt < $maxAttempts; $attempt++) {
Expand Down
8 changes: 4 additions & 4 deletions src/Support/GetSequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public static function setSequenceProvider(SequenceProviderInterface $provider):
*/
public static function useFilesystemSequenceProvider(
?string $baseDirectory = null,
int $waitTime = 100,
int $maxAttempts = 10,
int $waitTime = 1_000,
int $maxAttempts = 1_000,
): void {
self::$sequenceProvider = new FilesystemSequenceProvider($baseDirectory, $waitTime, $maxAttempts);
}
Expand Down Expand Up @@ -66,8 +66,8 @@ public static function useSequenceCallback(callable $callback): void
public static function useSimpleCacheSequenceProvider(
CacheInterface $cache,
string $prefix = 'uid:seq:',
int $waitTime = 100,
int $maxAttempts = 10,
int $waitTime = 1_000,
int $maxAttempts = 1_000,
): void {
self::$sequenceProvider = new PsrSimpleCacheSequenceProvider($cache, $prefix, $waitTime, $maxAttempts);
}
Expand Down