Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lock function #34

Open
wants to merge 5 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
12 changes: 11 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"require": {
"amphp/amp": "^2",
"amphp/byte-stream": "^1",
"amphp/parallel": "^1"
"amphp/parallel": "^1",
"amphp/sync": "^1.0"
},
"require-dev": {
"amphp/phpunit-util": "^1",
Expand All @@ -56,5 +57,14 @@
"platform": {
"php": "7.0.0"
}
},
"scripts": {
"check": [
"@cs",
"@test"
],
"cs": "PHP_CS_FIXER_IGNORE_ENV=1 php-cs-fixer fix -v --diff --dry-run",
"cs-fix": "PHP_CS_FIXER_IGNORE_ENV=1 php-cs-fixer fix -v --diff",
"test": "@php -dzend.assertions=1 -dassert.exception=1 ./vendor/bin/phpunit --coverage-text"
}
}
80 changes: 80 additions & 0 deletions lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

namespace Amp\File;

use Amp\CancellationToken;
use Amp\Delayed;
use Amp\Loop;
use Amp\NullCancellationToken;
use Amp\Promise;
use Amp\Sync\Lock;

use function Amp\call;

const LOOP_STATE_IDENTIFIER = Driver::class;

Expand Down Expand Up @@ -350,3 +356,77 @@ function put(string $path, string $contents): Promise
{
return filesystem()->put($path, $contents);
}

/**
* Asynchronously lock a file
* Resolves with a callable that MUST eventually be called in order to release the lock.
*
* @param string $file File to lock
* @param bool $shared Whether to acquire a shared or exclusive lock (\LOCK_SH or \LOCK_EX, see PHP flock docs)
* @param integer $polling Polling interval for lock in milliseconds
* @param CancellationToken $token Cancellation token
*
* @return \Amp\Promise Resolves with a \Amp\Sync\Lock that MUST eventually be released
danog marked this conversation as resolved.
Show resolved Hide resolved
*/
function lock(string $file, bool $shared, int $polling = 100, CancellationToken $token = null): Promise
{
return call(static function () use ($file, $shared, $polling, $token) {
$operation = $shared ? \LOCK_SH : \LOCK_EX;
$token = $token ?? new NullCancellationToken;
if (!yield exists($file)) {
yield \touch($file);
StatCache::clear($file);
}
$operation |= LOCK_NB;
$res = \fopen($file, 'c');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we just open the file here instead of the additional touch above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well technically yes, but then the touching wouldn't be async.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the fopen isn't async anyway? I guess we'd have to add the function to Handle to have it really async?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, fopen isn't async, but afaik there is no way to obtain a lock using any of the native async libs supported by amphp.
I thought it'd be nice if we could make at least the touch part of the fopen async.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trowski What's your opinion here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm rather uncomfortable introducing anything that can block when it could be rolled into a simple Task executed using parallel.


while (!\flock($res, $operation, $wouldblock)) {
if (!$wouldblock) {
throw new FilesystemException("Failed acquiring lock on file.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check for the file being deleted here and recreate it? I guess it could make sense to delete after unlocking, but not in every case (i.e. only if it's a separate lock file).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't think this is the place for such logic

}
yield new Delayed($polling);
$token->throwIfRequested();
}

return new Lock(
0,
static function () use (&$res) {
if ($res) {
\flock($res, LOCK_UN);
\fclose($res);
$res = null;
}
}
);
});
}

/**
* Asynchronously lock a file (shared lock)
* Resolves with a callable that MUST eventually be called in order to release the lock.
*
* @param string $file File to lock
* @param integer $polling Polling interval for lock in milliseconds
* @param CancellationToken $token Cancellation token
*
* @return \Amp\Promise Resolves with a callable that MUST eventually be called in order to release the lock.
*/
function lockShared(string $file, int $polling = 100, CancellationToken $token = null): Promise
{
return lock($file, true, $polling, $token ?? new NullCancellationToken);
}

/**
* Asynchronously lock a file (exclusive lock)
* Resolves with a callable that MUST eventually be called in order to release the lock.
*
* @param string $file File to lock
* @param integer $polling Polling interval for lock in milliseconds
* @param CancellationToken $token Cancellation token
*
* @return \Amp\Promise Resolves with a callable that MUST eventually be called in order to release the lock.
*/
function lockExclusive(string $file, int $polling = 100, CancellationToken $token = null): Promise
{
return lock($file, false, $polling, $token ?? new NullCancellationToken);
}
89 changes: 88 additions & 1 deletion test/HandleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
namespace Amp\File\Test;

use Amp\ByteStream\ClosedException;
use Amp\Delayed;
use Amp\File;
use Amp\PHPUnit\TestCase;
use Amp\Sync\Lock;
use Amp\TimeoutCancellationToken;

use function Amp\Promise\timeout;

abstract class HandleTest extends TestCase
{
Expand Down Expand Up @@ -232,7 +237,89 @@ public function testMode()
yield $handle->close();
});
}

/**
* Try locking file exclusively.
*
* @param string $file File
* @param int $polling Polling interval
* @param int $timeout Lock timeout
* @return void
*/
private function tryLockExclusive(string $file, int $polling, int $timeout)
{
return File\lockExclusive($file, $polling, new TimeoutCancellationToken($timeout));
}
/**
* Try locking file in shared mode.
*
* @param string $file File
* @param int $polling Polling interval
* @param int $timeout Lock timeout
* @return void
*/
private function tryLockShared(string $file, int $polling, int $timeout)
{
return File\lockShared($file, $polling, new TimeoutCancellationToken($timeout));
}
public function testExclusiveLock()
{
$this->execute(function () {
$primary = null;
$secondary = null;
try {
try {
$primary = yield $this->tryLockExclusive(__FILE__, 100, 100);
$this->assertInstanceOf(Lock::class, $primary);

$unlocked = false;
$try = $this->tryLockShared(__FILE__, 100, 10000);
$try->onResolve(static function ($e, $secondaryUnlock) use (&$unlocked, &$secondary) {
if ($e) {
throw $e;
}
$unlocked = true;
$secondary = $secondaryUnlock;
});

$this->assertFalse($unlocked, "The lock wasn't acquired");
} finally {
if ($primary) {
$primary->release();
}
}

yield new Delayed(100 * 2);
$this->assertTrue($unlocked, "The lock wasn't released");

yield $try;
$this->assertInstanceOf(Lock::class, $secondary);
} finally {
if ($secondary) {
$secondary->release();
}
}
});
}
public function testSharedLock()
{
$this->execute(function () {
$primary = null;
$secondary = null;
try {
$primary = yield $this->tryLockShared(__FILE__, 100, 100);
$this->assertInstanceOf(Lock::class, $primary);
$secondary = yield $this->tryLockShared(__FILE__, 100, 100);
$this->assertInstanceOf(Lock::class, $secondary);
} finally {
if ($primary) {
$primary->release();
}
if ($secondary) {
$secondary->release();
}
}
});
}
public function testClose()
{
$this->execute(function () {
Expand Down