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 1 commit
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
3 changes: 2 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 Down
21 changes: 13 additions & 8 deletions lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
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 @@ -364,7 +366,7 @@ function put(string $path, string $contents): Promise
* @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.
* @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
{
Expand All @@ -377,7 +379,7 @@ function lock(string $file, bool $shared, int $polling = 100, CancellationToken
}
$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

Expand All @@ -386,13 +388,16 @@ function lock(string $file, bool $shared, int $polling = 100, CancellationToken
$token->throwIfRequested();
}

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

Expand Down
17 changes: 9 additions & 8 deletions test/HandleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Amp\Delayed;
use Amp\File;
use Amp\PHPUnit\TestCase;
use Amp\Sync\Lock;
use Amp\TimeoutCancellationToken;

use function Amp\Promise\timeout;
Expand Down Expand Up @@ -268,7 +269,7 @@ public function testExclusiveLock()
try {
try {
$primary = yield $this->tryLockExclusive(__FILE__, 100, 100);
$this->assertInstanceOf(\Closure::class, $primary);
$this->assertInstanceOf(Lock::class, $primary);

$unlocked = false;
$try = $this->tryLockShared(__FILE__, 100, 10000);
Expand All @@ -283,18 +284,18 @@ public function testExclusiveLock()
$this->assertFalse($unlocked, "The lock wasn't acquired");
} finally {
if ($primary) {
$primary();
$primary->release();
}
}

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

yield $try;
$this->assertInstanceOf(\Closure::class, $secondary);
$this->assertInstanceOf(Lock::class, $secondary);
} finally {
if ($secondary) {
$secondary();
$secondary->release();
}
}
});
Expand All @@ -306,15 +307,15 @@ public function testSharedLock()
$secondary = null;
try {
$primary = yield $this->tryLockShared(__FILE__, 100, 100);
$this->assertInstanceOf(\Closure::class, $primary);
$this->assertInstanceOf(Lock::class, $primary);
$secondary = yield $this->tryLockShared(__FILE__, 100, 100);
$this->assertInstanceOf(\Closure::class, $secondary);
$this->assertInstanceOf(Lock::class, $secondary);
} finally {
if ($primary) {
$primary();
$primary->release();
}
if ($secondary) {
$secondary();
$secondary->release();
}
}
});
Expand Down