Skip to content

Added lockExists to Store interface, fixed locking bugs, added tests. #5381

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

Conversation

msonnabaum
Copy link
Contributor

While working on Drupal's HttpCache implementation, I discovered that the base HttpCache class does an is_file to check for a lock, which assumes a file-based cache is being used. This seems like a mistake since the rest of the Store interface is easily swappable. I added a lockExists method so that this is properly abstracted.

I also noticed there were no tests for the change I made, so I added some very basic locking tests. While adding those I found that the existing lock method is a bit broken. This line here:

<?php

if (false !== $lock = @fopen($path = $this->getPath($this->getCacheKey($request).'.lck'), 'x')) {

will return false if the file couldn't be written for any reason, but the rest of the method assumes that if $lock == false, the lock exists already. So if the file couldnt be written due to the parent directory not existing, $path will be returned as if it exists, which is clearly not the desired behavior.

I changed this to return false if the file couldnt be written and doesn't exist, $path if it exists, and true if the lock was created. It still doesn't feel great to have bool|string return values, but that's the best I could come up with atm. I also added a check for the parent directory that creates it if it doesn't exist. The new tests fail without it.

I also broke out that code a bit as it was very difficult to read.

@travisbot
Copy link

This pull request passes (merged 4e9caef into ecab04c).

@@ -92,6 +97,11 @@ public function unlock(Request $request)
return @unlink($this->getPath($this->getCacheKey($request).'.lck'));
}

public function lockExists(Request $request)
Copy link
Member

Choose a reason for hiding this comment

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

I would call it isLocked

@henrikbjorn
Copy link
Contributor

Symfony have a editorconfig file which set the correct indentation settings. http://editorconfig.org/

@msonnabaum
Copy link
Contributor Author

Updated based on stof's feedback.

if (false !== $lock = @fopen($path = $this->getPath($this->getCacheKey($request).'.lck'), 'x')) {
$path = $this->getPath($this->getCacheKey($request).'.lck');
if (!is_dir(dirname($path)) && false === @mkdir(dirname($path), 0777, true)) {
return false;
Copy link
Member

Choose a reason for hiding this comment

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

wrong indentation

@msonnabaum
Copy link
Contributor Author

Fixed based on code style feedback.

@travisbot
Copy link

This pull request passes (merged e0eb32c into ecab04c).

@travisbot
Copy link

This pull request passes (merged 4f6406c into ecab04c).

@jonathaningram
Copy link
Contributor

@msonnabaum, this seems to be distantly related to my recent PR too: #5376.

@stof
Copy link
Member

stof commented Oct 13, 2012

@fabpot anything left to merge this ?

@catch56
Copy link

catch56 commented Oct 23, 2012

This looks great to me, Couldn't find anything to complain about.

@fabpot fabpot closed this in da1b635 Oct 27, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants