mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-04 04:37:46 +08:00
use non-blocking locks.
This commit is contained in:
parent
6739970444
commit
bbd1bcea20
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user