use non-blocking locks.

This commit is contained in:
Reasno 2020-04-21 14:05:42 +08:00
parent 6739970444
commit bbd1bcea20
20 changed files with 24 additions and 25 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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;

View File

@ -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;