Skip to content

[Semaphore] Added the component #35780

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

Merged
merged 1 commit into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@
"symfony/property-info": "self.version",
"symfony/proxy-manager-bridge": "self.version",
"symfony/routing": "self.version",
"symfony/security-bundle": "self.version",
"symfony/security-core": "self.version",
"symfony/security-csrf": "self.version",
"symfony/security-guard": "self.version",
"symfony/security-http": "self.version",
"symfony/security-bundle": "self.version",
"symfony/semaphore": "self.version",
"symfony/sendgrid-mailer": "self.version",
"symfony/serializer": "self.version",
"symfony/stopwatch": "self.version",
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Semaphore/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.gitignore export-ignore
/phpunit.xml.dist export-ignore
/Tests export-ignore
3 changes: 3 additions & 0 deletions src/Symfony/Component/Semaphore/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
phpunit.xml
vendor/
7 changes: 7 additions & 0 deletions src/Symfony/Component/Semaphore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

5.2.0
-----

* Introduced the component as experimental
23 changes: 23 additions & 0 deletions src/Symfony/Component/Semaphore/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Semaphore\Exception;

/**
* Base ExceptionInterface for the Semaphore Component.
*
* @experimental in 5.2
*
* @author Jérémy Derussé <jeremy@derusse.com>
*/
interface ExceptionInterface extends \Throwable
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Semaphore\Exception;

/**
* @experimental in 5.2
*
* @author Jérémy Derussé <jeremy@derusse.com>
*/
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
{
}
21 changes: 21 additions & 0 deletions src/Symfony/Component/Semaphore/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Semaphore\Exception;

/**
* @experimental in 5.2
*
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
class RuntimeException extends \RuntimeException implements ExceptionInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Semaphore\Exception;

use Symfony\Component\Semaphore\Key;

/**
* SemaphoreAcquiringException is thrown when an issue happens during the acquisition of a semaphore.
*
* @experimental in 5.2
*
* @author Jérémy Derussé <jeremy@derusse.com>
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
class SemaphoreAcquiringException extends \RuntimeException implements ExceptionInterface
{
public function __construct(Key $key, string $message)
{
parent::__construct(sprintf('The semaphore "%s" could not be acquired: %s.', $key, $message));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Semaphore\Exception;

use Symfony\Component\Semaphore\Key;

/**
* SemaphoreExpiredException is thrown when a semaphore may conflict due to a TTL expiration.
*
* @experimental in 5.2
*
* @author Jérémy Derussé <jeremy@derusse.com>
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
class SemaphoreExpiredException extends \RuntimeException implements ExceptionInterface
{
public function __construct(Key $key, string $message)
{
parent::__construct(sprintf('The semaphore "%s" has expired: %s.', $key, $message));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Semaphore\Exception;

use Symfony\Component\Semaphore\Key;

/**
* SemaphoreReleasingException is thrown when an issue happens during the release of a semaphore.
*
* @experimental in 5.2
*
* @author Jérémy Derussé <jeremy@derusse.com>
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
class SemaphoreReleasingException extends \RuntimeException implements ExceptionInterface
{
public function __construct(Key $key, string $message)
{
parent::__construct(sprintf('The semaphore "%s" could not be released: %s.', $key, $message));
}
}
104 changes: 104 additions & 0 deletions src/Symfony/Component/Semaphore/Key.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Semaphore;

use Symfony\Component\Semaphore\Exception\InvalidArgumentException;

/**
* Key is a container for the state of the semaphores in stores.
*
* @experimental in 5.2
*
* @author Grégoire Pineau <lyrixx@lyrixx.info>
* @author Jérémy Derussé <jeremy@derusse.com>
*/
final class Key
{
private $resource;
private $limit;
private $weight;
private $expiringTime;
private $state = [];

public function __construct(string $resource, int $limit, int $weight = 1)
{
if (1 > $limit) {
throw new InvalidArgumentException("The limit ($limit) should be greater than 0.");
}
if (1 > $weight) {
throw new InvalidArgumentException("The weight ($weight) should be greater than 0.");
}
if ($weight > $limit) {
throw new InvalidArgumentException("The weight ($weight) should be lower or equals to the limit ($limit).");
}
$this->resource = $resource;
$this->limit = $limit;
$this->weight = $weight;
}

public function __toString(): string
{
return $this->resource;
}

public function getLimit(): int
{
return $this->limit;
}

public function getWeight(): int
{
return $this->weight;
}

public function hasState(string $stateKey): bool
{
return isset($this->state[$stateKey]);
}

public function setState(string $stateKey, $state): void
{
$this->state[$stateKey] = $state;
}

public function removeState(string $stateKey): void
{
unset($this->state[$stateKey]);
}

public function getState(string $stateKey)
{
return $this->state[$stateKey];
}

public function reduceLifetime(float $ttlInSeconds)
{
$newTime = microtime(true) + $ttlInSeconds;

if (null === $this->expiringTime || $this->expiringTime > $newTime) {
$this->expiringTime = $newTime;
}
}

/**
* @return float|null Remaining lifetime in seconds. Null when the key won't expire.
*/
public function getRemainingLifetime(): ?float
{
return null === $this->expiringTime ? null : $this->expiringTime - microtime(true);
}

public function isExpired(): bool
{
return null !== $this->expiringTime && $this->expiringTime <= microtime(true);
}
}
19 changes: 19 additions & 0 deletions src/Symfony/Component/Semaphore/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2016-2020 Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
51 changes: 51 additions & 0 deletions src/Symfony/Component/Semaphore/PersistingStoreInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Semaphore;

use Symfony\Component\Semaphore\Exception\SemaphoreAcquiringException;
use Symfony\Component\Semaphore\Exception\SemaphoreExpiredException;
use Symfony\Component\Semaphore\Exception\SemaphoreReleasingException;

/**
* @experimental in 5.2
*
* @author Grégoire Pineau <lyrixx@lyrixx.info>
* @author Jérémy Derussé <jeremy@derusse.com>
*/
interface PersistingStoreInterface
{
/**
* Stores the resource if the semaphore is not full.
*
* @throws SemaphoreAcquiringException
*/
public function save(Key $key, float $ttlInSecond);

/**
* Removes a resource from the storage.
*
* @throws SemaphoreReleasingException
*/
public function delete(Key $key);

/**
* Returns whether or not the resource exists in the storage.
*/
public function exists(Key $key): bool;

/**
* Extends the TTL of a resource.
*
* @throws SemaphoreExpiredException
*/
public function putOffExpiration(Key $key, float $ttlInSecond);
}
16 changes: 16 additions & 0 deletions src/Symfony/Component/Semaphore/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Semaphore Component
===================

**This Component is experimental**.
[Experimental features](https://symfony.com/doc/current/contributing/code/experimental.html)
are not covered by Symfony's
[Backward Compatibility Promise](https://symfony.com/doc/current/contributing/code/bc.html).

Resources
---------

* [Documentation](https://symfony.com/doc/master/components/semaphore.html)
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)
Loading