From 3fe683b5d646d78315d1d95b8feb93cbe3d8c5a3 Mon Sep 17 00:00:00 2001 From: "A. B. M. Mahmudul Hasan" Date: Sun, 10 May 2026 10:11:17 +0600 Subject: [PATCH] updated duration for better test scopes --- docs/sequence-providers.rst | 9 ++++++--- src/Sequence/FilesystemSequenceProvider.php | 4 ++-- src/Sequence/PsrSimpleCacheSequenceProvider.php | 4 ++-- src/Support/FileLock.php | 3 +++ src/Support/GetSequence.php | 8 ++++---- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/docs/sequence-providers.rst b/docs/sequence-providers.rst index c8d8f52..2a7c4f6 100644 --- a/docs/sequence-providers.rst +++ b/docs/sequence-providers.rst @@ -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 -------------------------------- @@ -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. diff --git a/src/Sequence/FilesystemSequenceProvider.php b/src/Sequence/FilesystemSequenceProvider.php index 4b8859d..8b7a5f0 100644 --- a/src/Sequence/FilesystemSequenceProvider.php +++ b/src/Sequence/FilesystemSequenceProvider.php @@ -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(); } diff --git a/src/Sequence/PsrSimpleCacheSequenceProvider.php b/src/Sequence/PsrSimpleCacheSequenceProvider.php index fe491d5..d6e0e4a 100644 --- a/src/Sequence/PsrSimpleCacheSequenceProvider.php +++ b/src/Sequence/PsrSimpleCacheSequenceProvider.php @@ -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; diff --git a/src/Support/FileLock.php b/src/Support/FileLock.php index 2afdbb0..4461f10 100644 --- a/src/Support/FileLock.php +++ b/src/Support/FileLock.php @@ -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++) { diff --git a/src/Support/GetSequence.php b/src/Support/GetSequence.php index 2cd280c..d7da0c3 100644 --- a/src/Support/GetSequence.php +++ b/src/Support/GetSequence.php @@ -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); } @@ -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); }