Skip to content

Commit 5bd2c3a

Browse files
author
desperado
committed
changed lock interfaces
1 parent 696cd8a commit 5bd2c3a

12 files changed

+314
-33
lines changed

src/AmpLock.php

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,21 @@
1212

1313
namespace ServiceBus\Mutex;
1414

15-
use function Amp\asyncCall;
15+
use function Amp\call;
16+
use Amp\Promise;
1617

1718
/**
18-
*
19+
* @internal
1920
*/
2021
final class AmpLock implements Lock
2122
{
23+
/**
24+
* Lock identifier.
25+
*
26+
* @var string
27+
*/
28+
private $id;
29+
2230
/**
2331
* The function to be called on release or null if the lock has been released.
2432
*
@@ -27,10 +35,12 @@ final class AmpLock implements Lock
2735
private $releaser;
2836

2937
/**
38+
* @param string $id
3039
* @param callable|null $releaser
3140
*/
32-
public function __construct(?callable $releaser)
41+
public function __construct(string $id, ?callable $releaser)
3342
{
43+
$this->id = $id;
3444
$this->releaser = $releaser;
3545
}
3646

@@ -45,17 +55,29 @@ public function released(): bool
4555
/**
4656
* {@inheritdoc}
4757
*/
48-
public function release(): void
58+
public function id(): string
4959
{
50-
if (null === $this->releaser)
51-
{
52-
return;
53-
}
60+
return $this->id;
61+
}
5462

55-
$releaser = $this->releaser;
56-
$this->releaser = null;
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
public function release(): Promise
67+
{
68+
/** @psalm-suppress MixedTypeCoercion */
69+
return call(
70+
function(): \Generator
71+
{
72+
if (null !== $this->releaser)
73+
{
74+
$releaser = $this->releaser;
75+
$this->releaser = null;
5776

58-
asyncCall($releaser);
77+
yield call($releaser);
78+
}
79+
}
80+
);
5981
}
6082

6183
public function __destruct()

src/FileMutex.php renamed to src/FilesystemMutex.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,33 @@
1515
use function Amp\asyncCall;
1616
use function Amp\call;
1717
use function Amp\File\get;
18-
use function Amp\File\touch;
18+
use function Amp\File\put;
1919
use function Amp\File\unlink;
2020
use Amp\Delayed;
2121
use Amp\Promise;
2222
use ServiceBus\Mutex\Exceptions\SyncException;
2323

2424
/**
2525
* It can be used when several processes are running within the same host.
26+
*
27+
* @internal
28+
*
29+
* @see FilesystemMutexFactory
2630
*/
27-
final class FileMutex implements Mutex
31+
final class FilesystemMutex implements Mutex
2832
{
2933
const LATENCY_TIMEOUT = 50;
3034

3135
/**
36+
* Mutex identifier.
37+
*
38+
* @var string
39+
*/
40+
private $id;
41+
42+
/**
43+
* Barrier file path.
44+
*
3245
* @var string
3346
*/
3447
private $filePath;
@@ -41,21 +54,25 @@ final class FileMutex implements Mutex
4154
private $release;
4255

4356
/**
57+
* @param string $id
4458
* @param string $filePath
4559
*/
46-
public function __construct(string $filePath)
60+
public function __construct(string $id, string $filePath)
4761
{
62+
$this->id = $id;
4863
$this->filePath = $filePath;
4964
$this->release = function(): \Generator
5065
{
5166
try
5267
{
5368
yield unlink($this->filePath);
5469
}
70+
// @codeCoverageIgnoreStart
5571
catch (\Throwable $throwable)
5672
{
5773
/** Not interests */
5874
}
75+
// @codeCoverageIgnoreEnd
5976
};
6077
}
6178

@@ -81,9 +98,9 @@ function(): \Generator
8198
yield new Delayed(self::LATENCY_TIMEOUT);
8299
}
83100

84-
yield touch($this->filePath);
101+
yield put($this->filePath, '');
85102

86-
return new AmpLock($this->release);
103+
return new AmpLock($this->id, $this->release);
87104
}
88105
catch (\Throwable $throwable)
89106
{

src/FilesystemMutexFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public function __construct(string $storageDirectory)
3333
/**
3434
* {@inheritdoc}
3535
*/
36-
public function create(string $key): Mutex
36+
public function create(string $id): Mutex
3737
{
38-
return new FileMutex(\sprintf('%s/%s', $this->storageDirectory, \sha1($key)));
38+
return new FilesystemMutex($id, \sprintf('%s/%s', $this->storageDirectory, \sha1($id)));
3939
}
4040
}

src/InMemoryMutex.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
/**
2121
* Can only be used when working in one process.
22+
*
23+
* @internal
24+
*
25+
* @see InMemoryMutexFactory
2226
*/
2327
final class InMemoryMutex implements Mutex
2428
{
@@ -27,19 +31,19 @@ final class InMemoryMutex implements Mutex
2731
/**
2832
* @var string
2933
*/
30-
private $key;
34+
private $id;
3135

3236
/**
33-
* @param string $key
37+
* @param string $id
3438
*/
35-
public function __construct(string $key)
39+
public function __construct(string $id)
3640
{
37-
$this->key = $key;
41+
$this->id = $id;
3842
}
3943

4044
public function __destruct()
4145
{
42-
InMemoryMutexStorage::instance()->unlock($this->key);
46+
InMemoryMutexStorage::instance()->unlock($this->id);
4347
}
4448

4549
/**
@@ -52,17 +56,18 @@ public function acquire(): Promise
5256
return call(
5357
function(): \Generator
5458
{
55-
while (InMemoryMutexStorage::instance()->has($this->key))
59+
while (InMemoryMutexStorage::instance()->has($this->id))
5660
{
5761
yield new Delayed(self::LATENCY_TIMEOUT);
5862
}
5963

60-
InMemoryMutexStorage::instance()->lock($this->key);
64+
InMemoryMutexStorage::instance()->lock($this->id);
6165

6266
return new AmpLock(
67+
$this->id,
6368
function(): void
6469
{
65-
InMemoryMutexStorage::instance()->unlock($this->key);
70+
InMemoryMutexStorage::instance()->unlock($this->id);
6671
}
6772
);
6873
}

src/InMemoryMutexFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ final class InMemoryMutexFactory implements MutexFactory
2020
/**
2121
* {@inheritdoc}
2222
*/
23-
public function create(string $key): Mutex
23+
public function create(string $id): Mutex
2424
{
25-
return new InMemoryMutex($key);
25+
return new InMemoryMutex($id);
2626
}
2727
}

src/Lock.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,20 @@
1212

1313
namespace ServiceBus\Mutex;
1414

15+
use Amp\Promise;
16+
1517
/**
1618
*
1719
*/
1820
interface Lock
1921
{
22+
/**
23+
* Receive lock identifier.
24+
*
25+
* @return string
26+
*/
27+
public function id(): string;
28+
2029
/**
2130
* Checks if the lock has already been released.
2231
*
@@ -27,7 +36,7 @@ public function released(): bool;
2736
/**
2837
* Releases the lock. No-op if the lock has already been released.
2938
*
30-
* @return void
39+
* @return Promise
3140
*/
32-
public function release(): void;
41+
public function release(): Promise;
3342
}

src/Mutex.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ interface Mutex
2525
* @throws \ServiceBus\Mutex\Exceptions\SyncException An error occurs when attempting to obtain the lock
2626
*
2727
* @return \Amp\Promise<\ServiceBus\Mutex\Lock>
28-
*
2928
*/
3029
public function acquire(): Promise;
3130
}

src/MutexFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ interface MutexFactory
2020
/**
2121
* Create lock for specified operation.
2222
*
23-
* @param string $key
23+
* @param string $id
2424
*
2525
* @return Mutex
2626
*/
27-
public function create(string $key): Mutex;
27+
public function create(string $id): Mutex;
2828
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* PHP Mutex implementation.
5+
*
6+
* @author Maksim Masiukevich <[email protected]>
7+
* @license MIT
8+
* @license https://opensource.org/licenses/MIT
9+
*/
10+
11+
declare(strict_types = 1);
12+
13+
namespace ServiceBus\Mutex\Tests;
14+
15+
use PHPUnit\Framework\TestCase;
16+
use ServiceBus\Mutex\FilesystemMutex;
17+
use ServiceBus\Mutex\FilesystemMutexFactory;
18+
19+
/**
20+
*
21+
*/
22+
final class FilesystemMutexFactoryTest extends TestCase
23+
{
24+
/**
25+
* @test
26+
*
27+
* @throws \Throwable
28+
*
29+
* @return void
30+
*/
31+
public function create(): void
32+
{
33+
$mutex = (new FilesystemMutexFactory(\sys_get_temp_dir()))->create(__CLASS__);
34+
35+
static::assertInstanceOf(FilesystemMutex::class, $mutex);
36+
}
37+
}

0 commit comments

Comments
 (0)