-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,11 @@ public function removeState($stateKey) | |
unset($this->state[$stateKey]); | ||
} | ||
|
||
/** | ||
* @param $stateKey | ||
* | ||
* @return mixed | ||
*/ | ||
public function getState($stateKey) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing phpdoc. |
||
{ | ||
return $this->state[$stateKey]; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jderusse I suggest using a non-empty exception message for better DX There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indeed.. |
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't you be catching There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough, I wanted to make sure this was considered |
||
} | ||
|
@@ -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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't you be catching There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? If yes, you're right, I should catch everything. WDYT? |
||
} | ||
|
@@ -116,7 +116,7 @@ public function release() | |
$this->store->delete($this->key); | ||
|
||
if ($this->store->exists($this->key)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the lock had been acquired by an other instance of There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
$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(); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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