8000 [Lock] Create a lock component by jderusse · Pull Request #21093 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Lock] Create a lock component #21093

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

Closed
wants to merge 4 commits into from
Closed
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
Prev Previous commit
Fix typo
  • Loading branch information
jderusse committed Feb 28, 2017
commit 48c998a097e8c71d90c5e8b508d21c8b866f2db1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\Lock\Exception;

/**
* NotSupportedException is thrown when a unsupported method is called.
* NotSupportedException is thrown when an unsupported method is called.
*
* @author Jérémy Derussé <jeremy@derusse.com>
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Lock/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Psr\Log\NullLogger;

/**
* Factory provide method to create locks.
* Factory provides method to create locks.
*
* @author Jérémy Derussé <jeremy@derusse.com>
*/
Expand All @@ -34,12 +34,12 @@ public function __construct(StoreInterface $store)
}

/**
* Create a lock for the given resource.
* Creates a lock for the given resource.
*
* @param string $resource The resource to lock
* @param float $ttl maximum expected lock duration
*
* @return LockInterface
* @return Lock
*/
public function createLock($resource, $ttl = 300.0)
{
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Lock/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public function removeState($stateKey)
unset($this->state[$stateKey]);
}

/**
* @param $stateKey
*
* @return mixed
Copy link
Member

Choose a reason for hiding this comment

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

Can be removed as it does not say anything useful

*/
public function getState($stateKey)
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing phpdoc.

{
return $this->state[$stateKey];
Expand Down
16 changes: 8 additions & 8 deletions src/Symfony/Component/Lock/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,23 @@ public function acquire($blocking = false)
$this->store->waitAndSave($this->key);
}

$this->logger->info('Lock successfully acquired for "{resource}"', array('resource' => $this->key));
$this->logger->info('Lock successfully acquired for "{resource}".', array('resource' => $this->key));

if ($this->ttl) {
$this->refresh();
}

return true;
} catch (LockConflictedException $e) {
$this->logger->warning('Failed to lock the "{resource}". Someone else already acquired the lock', array('resource' => $this->key));
$this->logger->warning('Failed to lock the "{resource}". Someone else already acquired the lock.', array('resource' => $this->key));

if ($blocking) {
throw $e;
}

return false;
} catch (\Exception $e) {
$this->logger->warning('Failed to lock the "{resource}"', array('resource' => $this->key, 'exception' => $e));
$this->logger->warning('Failed to lock the "{resource}".', array('resource' => $this->key, 'exception' => $e));
throw new LockAcquiringException('', 0, $e);
Copy link
Member

Choose a reason for hiding this comment

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

@jderusse I suggest using a non-empty exception message for better DX

Copy link
Member Author

Choose a reason for hiding this comment

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

indeed..

}
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't you be catching \Throwable as well?

Copy link
Member
@nicolas-grekas nicolas-grekas Dec 29, 2016

Choose a reason for hiding this comment

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

on Error, it should let the error throw imho. throwing a LockAcquiringException would make no sense. Error is a dev mistake, not a runtime one.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair enough, I wanted to make sure this was considered

}
Expand All @@ -85,17 +85,17 @@ public function acquire($blocking = false)
public function refresh()
{
if (!$this->ttl) {
throw new InvalidArgumentException('You have to define an expiration duration');
throw new InvalidArgumentException('You have to define an expiration duration.');
}

try {
$this->store->putOffExpiration($this->key, $this->ttl);
$this->logger->info('Expiration defined for "{resource}" lock for "{ttl}" seconds', array('resource' => $this->key, 'ttl' => $this->ttl));
$this->logger->info('Expiration defined for "{resource}" lock for "{ttl}" seconds.', array('resource' => $this->key, 'ttl' => $this->ttl));
} catch (LockConflictedException $e) {
$this->logger->warning('Failed to define an expiration for the "{resource}" lock, someone else acquired the lock', array('resource' => $this->key));
$this->logger->warning('Failed to define an expiration for the "{resource}" lock, someone else acquired the lock.', array('resource' => $this->key));
throw $e;
} catch (\Exception $e) {
$this->logger->warning('Failed to define an expiration for the "{resource}" lock', array('resource' => $this->key, 'exception' => $e));
$this->logger->warning('Failed to define an expiration for the "{resource}" lock.', array('resource' => $this->key, 'exception' => $e));
throw new LockAcquiringException('', 0, $e);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't you be catching \Throwable as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

I wonder if it make sens to wrap every exception with "LockException" here?
Or I should just let the original exception been thrown.

If yes, you're right, I should catch everything.
If no, I can remove this statement.

WDYT?

}
Expand All @@ -116,7 +116,7 @@ public function release()
$this->store->delete($this->key);

if ($this->store->exists($this->key)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

In a concurrent environment, another process might have created the same lock here. exists will return true even if the previous delete was successful.

Copy link
Member Author

Choose a reason for hiding this comment

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

If the lock had been acquired by an other instance of key, the store will treat them like 2 different keys and will return false.

Copy link
Contributor

Choose a reason for hiding this comment

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

ah, i got it. there is a "token" in between, that is always random, for the same key you have different "tokens"

$this->logger->warning('Failed to release the "{resource}" lock', array('resource' => $this->key));
$this->logger->warning('Failed to release the "{resource}" lock.', array('resource' => $this->key));
throw new LockReleasingException();
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Lock/Store/CombinedStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function save(Key $key)
return;
}

$this->logger->warning('Failed to store the "{resource}" lock. Quorum has not been met', array('resource' => $key, 'success' => $successCount, 'failure' => $failureCount));
$this->logger->warning('Failed to store the "{resource}" lock. Quorum has not been met.', array('resource' => $key, 'success' => $successCount, 'failure' => $failureCount));

// clean up potential locks
$this->delete($key);
Expand All @@ -91,7 +91,7 @@ public function save(Key $key)

public function waitAndSave(Key $key)
{
throw new NotSupportedException(sprintf('The store "%s" does not supports blocking locks', get_class($this)));
throw new NotSupportedException(sprintf('The store "%s" does not supports blocking locks.', get_class($this)));
}

/**
Expand Down Expand Up @@ -121,7 +121,7 @@ public function putOffExpiration(Key $key, $ttl)
return;
}

$this->logger->warning('Failed to define the expiration for the "{resource}" lock. Quorum has not been met', array('resource' => $key, 'success' => $successCount, 'failure' => $failureCount));
$this->logger->warning('Failed to define the expiration for the "{resource}" lock. Quorum has not been met.', array('resource' => $key, 'success' => $successCount, 'failure' => $failureCount));

// clean up potential locks
$this->delete($key);
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Lock/Store/MemcachedStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function save(Key $key)

public function waitAndSave(Key $key)
{
throw new InvalidArgumentException(sprintf('The store "%s" does not supports blocking locks', get_class($this)));
throw new InvalidArgumentException(sprintf('The store "%s" does not supports blocking locks.', get_class($this)));
}

/**
Expand All @@ -80,7 +80,7 @@ public function putOffExpiration(Key $key, $ttl)
throw new InvalidArgumentException(sprintf('%s() expects a TTL greater or equals to 1. Got %s.', __METHOD__, $ttl));
}

// Interface define a float value but Store required an integer.
// Interface defines a float value but Store required an integer.
$ttl = (int) ceil($ttl);

$token = $this->getToken($key);
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Lock/Store/RedisStore.php
F42D
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function save(Key $key)

public function waitAndSave(Key $key)
{
throw new InvalidArgumentException(sprintf('The store "%s" does not supports blocking locks', get_class($this)));
throw new InvalidArgumentException(sprintf('The store "%s" does not supports blocking locks.', get_class($this)));
}

/**
Expand Down Expand Up @@ -112,7 +112,7 @@ public function exists(Key $key)
}

/**
* Evaluate a script in the corresponding redis client.
* Evaluates a script in the corresponding redis client.
*
* @param string $script
* @param string $resource
Expand All @@ -135,7 +135,7 @@ private function evaluate($script, $resource, array $args)
}

/**
* Retrieve an unique token for the given key.
* Retrieves an unique token for the given key.
*
* @param Key $key
*
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Lock/Store/SemaphoreStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private function lock(Key $key, $blocking)

if (PHP_VERSION_ID < 50601) {
if (!$blocking) {
throw new NotSupportedException(sprintf('The store "%s" does not supports non blocking locks', get_class($this)));
throw new NotSupportedException(sprintf('The store "%s" does not supports non blocking locks.', get_class($this)));
}

$acquired = sem_acquire($resource);
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Lock/StoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ interface StoreInterface
public function save(Key $key);

/**
* Wait a key becomes free, then stores the resource.
* Waits a key becomes free, then stores the resource.
*
* If the store does not supports this feature it should thrown an NotSupportedException.
* If the store does not support this feature it should throw a NotSupportedException.
*
* @param Key $key key to lock
*
Expand All @@ -45,7 +45,7 @@ public function waitAndSave(Key $key);
/**
* Extends the ttl of a resource.
*
* If the store does not supports this feature it should thrown an NotSupportedException.
* If the store does not support this feature it should throw a NotSupportedException.
*
* @param Key $key key to lock
* @param float $ttl amount of second to keep the lock in the store
Expand Down
0