Skip to content

Commit

Permalink
Swap prefix for directory
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Mar 9, 2024
1 parent 2dc5df8 commit a637005
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
23 changes: 16 additions & 7 deletions src/KeyedFileMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Amp\Sync\KeyedMutex;
use Amp\Sync\Lock;
use Amp\Sync\SyncException;
use ValueError;
use function Amp\delay;

final class KeyedFileMutex implements KeyedMutex
Expand All @@ -13,21 +14,24 @@ final class KeyedFileMutex implements KeyedMutex

private readonly Filesystem $filesystem;

private readonly string $directory;

/**
* @param string $pattern Name of temporary file to use as a mutex, including an %s to splice the key in
* @param string $directory Directory in which to store key files.
*/
public function __construct(private readonly string $pattern, ?Filesystem $filesystem = null)
public function __construct(string $directory, ?Filesystem $filesystem = null)
{
if (!\preg_match("((%%(*SKIP))*%s)", $this->pattern)) {
throw new \Error("Invalid pattern for a mutex, needs to contain an unescaped %s");
}

$this->filesystem = $filesystem ?? filesystem();
$this->directory = \rtrim($directory, "/\\");

if (!$this->filesystem->isDirectory($this->directory)) {
throw new ValueError(\sprintf('Directory "%s" does not exist', $this->directory));
}
}

public function acquire(string $key): Lock
{
$filename = \sprintf($this->pattern, \hash('sha256', $key));
$filename = $this->getFilename($key);

// Try to create the lock file. If the file already exists, someone else
// has the lock, so set an asynchronous timer and try again.
Expand Down Expand Up @@ -63,4 +67,9 @@ private function release(string $filename): void
);
}
}

private function getFilename(string $key): string
{
return $this->directory . '/' . \hash('sha256', $key) . '.lock';
}
}
14 changes: 13 additions & 1 deletion test/KeyedFileMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,20 @@

final class KeyedFileMutexTest extends AbstractKeyedMutexTest
{
protected function setUp(): void
{
parent::setUp();
Fixture::init();
}

protected function tearDown(): void
{
parent::tearDown();
Fixture::clear();
}

public function createMutex(): KeyedMutex
{
return new KeyedFileMutex(\sys_get_temp_dir() . '/testmutex-' . \bin2hex(\random_bytes(5)) . '-%s.lock');
return new KeyedFileMutex(Fixture::path());
}
}

0 comments on commit a637005

Please sign in to comment.