From bbd1bcea20820fb8292cc9c15de2149d56c9eadb Mon Sep 17 00:00:00 2001 From: Reasno Date: Tue, 21 Apr 2020 14:05:42 +0800 Subject: [PATCH] use non-blocking locks. --- src/utils/src/Filesystem/Filesystem.php | 30 +++++++++++++++---- src/utils/tests/ArrTest.php | 1 - src/utils/tests/BackoffTest.php | 1 - src/utils/tests/CodeGen/ProjectTest.php | 1 - src/utils/tests/Codec/Base62Test.php | 1 - src/utils/tests/Codec/JsonTest.php | 1 - src/utils/tests/CollectionTest.php | 1 - src/utils/tests/ContextTest.php | 1 - .../tests/Coordinator/CoordinatorTest.php | 1 - src/utils/tests/Coroutine/ConcurrentTest.php | 1 - src/utils/tests/Exception/RetryException.php | 1 - src/utils/tests/FunctionTest.php | 1 - src/utils/tests/ParallelTest.php | 1 - .../Serializer/ExceptionNormalizerTest.php | 1 - .../Serializer/SymfonySerializerTest.php | 1 - src/utils/tests/Stub/Foo.php | 1 - src/utils/tests/Stub/FooException.php | 1 - .../tests/Stub/SerializableException.php | 1 - src/utils/tests/Traits/ContainerTest.php | 1 - src/utils/tests/WaitGroupTest.php | 1 - 20 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/utils/src/Filesystem/Filesystem.php b/src/utils/src/Filesystem/Filesystem.php index 2641c5b1d..ef065b6ee 100644 --- a/src/utils/src/Filesystem/Filesystem.php +++ b/src/utils/src/Filesystem/Filesystem.php @@ -56,13 +56,17 @@ class Filesystem $contents = ''; $handle = fopen($path, 'rb'); if ($handle) { + $wouldBlock = false; + flock($handle, LOCK_SH | LOCK_NB, $wouldBlock); + while ($wouldBlock) { + usleep(1000); + flock($handle, LOCK_SH | LOCK_NB, $wouldBlock); + } try { - if (flock($handle, LOCK_SH)) { - clearstatcache(true, $path); - $contents = fread($handle, $this->size($path) ?: 1); - flock($handle, LOCK_UN); - } + clearstatcache(true, $path); + $contents = fread($handle, $this->size($path) ?: 1); } finally { + flock($handle, LOCK_UN); fclose($handle); } } @@ -112,7 +116,21 @@ class Filesystem { if ($lock) { return $this->atomic($path, function ($path) use ($contents, $lock) { - return file_put_contents($path, $contents, LOCK_EX); + $handle = fopen($path, 'w+'); + if ($handle) { + $wouldBlock = false; + flock($handle, LOCK_EX | LOCK_NB, $wouldBlock); + while ($wouldBlock) { + usleep(1000); + flock($handle, LOCK_EX | LOCK_NB, $wouldBlock); + } + try { + fwrite($handle, $contents); + } finally { + flock($handle, LOCK_UN); + fclose($handle); + } + } }); } return file_put_contents($path, $contents); diff --git a/src/utils/tests/ArrTest.php b/src/utils/tests/ArrTest.php index 5b531257d..84edf4aa8 100644 --- a/src/utils/tests/ArrTest.php +++ b/src/utils/tests/ArrTest.php @@ -9,7 +9,6 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ - namespace HyperfTest\Utils; use Hyperf\Utils\Arr; diff --git a/src/utils/tests/BackoffTest.php b/src/utils/tests/BackoffTest.php index 2e7928603..a9625e358 100644 --- a/src/utils/tests/BackoffTest.php +++ b/src/utils/tests/BackoffTest.php @@ -9,7 +9,6 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ - namespace HyperfTest\Utils; use Hyperf\Utils\Backoff; diff --git a/src/utils/tests/CodeGen/ProjectTest.php b/src/utils/tests/CodeGen/ProjectTest.php index 26f90c33f..157a915c7 100644 --- a/src/utils/tests/CodeGen/ProjectTest.php +++ b/src/utils/tests/CodeGen/ProjectTest.php @@ -9,7 +9,6 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ - namespace HyperfTest\Utils\CodeGen; use Hyperf\Utils\CodeGen\Project; diff --git a/src/utils/tests/Codec/Base62Test.php b/src/utils/tests/Codec/Base62Test.php index 6a8ec8a0f..17a32a2a2 100644 --- a/src/utils/tests/Codec/Base62Test.php +++ b/src/utils/tests/Codec/Base62Test.php @@ -9,7 +9,6 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ - namespace HyperfTest\Utils\Codec; use Hyperf\Utils\Codec\Base62; diff --git a/src/utils/tests/Codec/JsonTest.php b/src/utils/tests/Codec/JsonTest.php index fcffe29ab..f85ceffd0 100644 --- a/src/utils/tests/Codec/JsonTest.php +++ b/src/utils/tests/Codec/JsonTest.php @@ -9,7 +9,6 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ - namespace HyperfTest\Utils\Codec; use Hyperf\Utils\Codec\Json; diff --git a/src/utils/tests/CollectionTest.php b/src/utils/tests/CollectionTest.php index aec15340f..7ae67fdbe 100644 --- a/src/utils/tests/CollectionTest.php +++ b/src/utils/tests/CollectionTest.php @@ -9,7 +9,6 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ - namespace HyperfTest\Utils; use Hyperf\Utils\Collection; diff --git a/src/utils/tests/ContextTest.php b/src/utils/tests/ContextTest.php index c5206868f..150cf77bd 100644 --- a/src/utils/tests/ContextTest.php +++ b/src/utils/tests/ContextTest.php @@ -9,7 +9,6 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ - namespace HyperfTest\Utils; use Hyperf\Utils\Context; diff --git a/src/utils/tests/Coordinator/CoordinatorTest.php b/src/utils/tests/Coordinator/CoordinatorTest.php index f1bcb8ab9..aa927736f 100644 --- a/src/utils/tests/Coordinator/CoordinatorTest.php +++ b/src/utils/tests/Coordinator/CoordinatorTest.php @@ -9,7 +9,6 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ - namespace HyperfTest\Utils\Coordinator; use Hyperf\Utils\Coordinator\Coordinator; diff --git a/src/utils/tests/Coroutine/ConcurrentTest.php b/src/utils/tests/Coroutine/ConcurrentTest.php index 5766e4ea2..bb2fd8cdb 100644 --- a/src/utils/tests/Coroutine/ConcurrentTest.php +++ b/src/utils/tests/Coroutine/ConcurrentTest.php @@ -9,7 +9,6 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ - namespace HyperfTest\Utils\Coroutine; use Hyperf\Utils\ApplicationContext; diff --git a/src/utils/tests/Exception/RetryException.php b/src/utils/tests/Exception/RetryException.php index ff2405b0c..786a0fe09 100644 --- a/src/utils/tests/Exception/RetryException.php +++ b/src/utils/tests/Exception/RetryException.php @@ -9,7 +9,6 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ - namespace HyperfTest\Utils\Exception; class RetryException extends \Exception diff --git a/src/utils/tests/FunctionTest.php b/src/utils/tests/FunctionTest.php index 6b33fbe7c..97339a7b5 100644 --- a/src/utils/tests/FunctionTest.php +++ b/src/utils/tests/FunctionTest.php @@ -9,7 +9,6 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ - namespace HyperfTest\Utils; use HyperfTest\Utils\Exception\RetryException; diff --git a/src/utils/tests/ParallelTest.php b/src/utils/tests/ParallelTest.php index a1e1b66fc..bf964906a 100644 --- a/src/utils/tests/ParallelTest.php +++ b/src/utils/tests/ParallelTest.php @@ -9,7 +9,6 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ - namespace HyperfTest\Utils; use Hyperf\Utils\Coroutine; diff --git a/src/utils/tests/Serializer/ExceptionNormalizerTest.php b/src/utils/tests/Serializer/ExceptionNormalizerTest.php index e0f586226..9bec215a6 100644 --- a/src/utils/tests/Serializer/ExceptionNormalizerTest.php +++ b/src/utils/tests/Serializer/ExceptionNormalizerTest.php @@ -9,7 +9,6 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ - namespace HyperfTest\Utils\Serializer; use Hyperf\Utils\Serializer\ExceptionNormalizer; diff --git a/src/utils/tests/Serializer/SymfonySerializerTest.php b/src/utils/tests/Serializer/SymfonySerializerTest.php index b55736ff1..281219bd1 100644 --- a/src/utils/tests/Serializer/SymfonySerializerTest.php +++ b/src/utils/tests/Serializer/SymfonySerializerTest.php @@ -9,7 +9,6 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ - namespace HyperfTest\Utils\Serializer; use Hyperf\Utils\Serializer\SerializerFactory; diff --git a/src/utils/tests/Stub/Foo.php b/src/utils/tests/Stub/Foo.php index a22a64e9c..fe6410586 100644 --- a/src/utils/tests/Stub/Foo.php +++ b/src/utils/tests/Stub/Foo.php @@ -9,7 +9,6 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ - namespace HyperfTest\Utils\Stub; class Foo diff --git a/src/utils/tests/Stub/FooException.php b/src/utils/tests/Stub/FooException.php index e1b2799ab..87fb712dd 100644 --- a/src/utils/tests/Stub/FooException.php +++ b/src/utils/tests/Stub/FooException.php @@ -9,7 +9,6 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ - namespace HyperfTest\Utils\Stub; class FooException extends \Exception diff --git a/src/utils/tests/Stub/SerializableException.php b/src/utils/tests/Stub/SerializableException.php index f402d76ab..5194359e6 100644 --- a/src/utils/tests/Stub/SerializableException.php +++ b/src/utils/tests/Stub/SerializableException.php @@ -9,7 +9,6 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ - namespace HyperfTest\Utils\Stub; class SerializableException extends \RuntimeException implements \Serializable diff --git a/src/utils/tests/Traits/ContainerTest.php b/src/utils/tests/Traits/ContainerTest.php index b538520e1..2208afd19 100644 --- a/src/utils/tests/Traits/ContainerTest.php +++ b/src/utils/tests/Traits/ContainerTest.php @@ -9,7 +9,6 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ - namespace HyperfTest\Utils\Traits; use Hyperf\Utils\Traits\Container; diff --git a/src/utils/tests/WaitGroupTest.php b/src/utils/tests/WaitGroupTest.php index 25f25f9c8..2f822fee7 100644 --- a/src/utils/tests/WaitGroupTest.php +++ b/src/utils/tests/WaitGroupTest.php @@ -9,7 +9,6 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ - namespace HyperfTest\Utils; use Hyperf\Utils\WaitGroup;