Skip to content

Commit

Permalink
Merge branch 'master' into predis-timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
antriver committed Nov 10, 2017
2 parents adeafe5 + 916c36b commit 8653c53
Show file tree
Hide file tree
Showing 16 changed files with 250 additions and 92 deletions.
3 changes: 0 additions & 3 deletions .travis.php.ini

This file was deleted.

1 change: 1 addition & 0 deletions .travis.php.memcache.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extension=memcache.so
1 change: 1 addition & 0 deletions .travis.php.memcached.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extension=memcached.so
1 change: 1 addition & 0 deletions .travis.php.redis.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extension=redis.so
25 changes: 22 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,22 @@ php:
- 7.1
- hhvm
matrix:
include:
# Memcache extension is not supported in php 7
# https://github.com/websupport-sk/pecl-memcache/issues/11
- php: 7.0
env: MEMCACHE_EXT_UNSUPPORTED=1
- php: 7.1
env: MEMCACHE_EXT_UNSUPPORTED=1
allow_failures:
# PHP 5.3 is no longer supported on travis
# https://docs.travis-ci.com/user/languages/php#PHP-5.2(.x)-and-5.3(.x)-support-is-available-on-Precise-only
- php: 5.3
env:
- php: 7.0
env:
- php: 7.1
env:

addons:
code_climate:
Expand All @@ -23,12 +36,18 @@ services:
- redis-server

before_script:
- if [[ "$TRAVIS_PHP_VERSION" != "hhvm" ]]; then cat .travis.php.ini >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini ; echo "Loading additional config for version $TRAVIS_PHP_VERSION" ; fi
# If Memcache is supported then enable it.
- if [[ "$MEMCACHE_EXT_UNSUPPORTED" != "1" ]]; then phpenv config-add .travis.php.memcache.ini ; fi
- phpenv config-add .travis.php.memcached.ini
- phpenv config-add .travis.php.redis.ini
- composer self-update || true
# If Memcache extension is not supported we need to fake it,
# otherwise composer will complain about missing dev dependencies.
- if [[ "$MEMCACHE_EXT_UNSUPPORTED" == "1" ]]; then composer config platform.ext-memcache 0.0.1 ; fi
- composer install
- ./tests/travis/mysql-setup.sh
- vendor/bin/phpunit --version

script: phpunit
script: vendor/bin/phpunit

after_script:
- vendor/bin/ocular code-coverage:upload --format=php-clover build/logs/clover.xml
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
[![GitHub version](https://badge.fury.io/gh/arvenil%2Fninja-mutex.svg)](http://badge.fury.io/gh/arvenil%2Fninja-mutex)
[![Build Status](https://travis-ci.org/arvenil/ninja-mutex.svg?branch=master)](https://travis-ci.org/arvenil/ninja-mutex)
[![HHVM Status](http://hhvm.h4cc.de/badge/arvenil/ninja-mutex.svg)](http://hhvm.h4cc.de/package/arvenil/ninja-mutex)
[![Tested](https://php-eye.com/badge/arvenil/ninja-mutex/tested.svg)](https://travis-ci.org/arvenil/ninja-mutex)
[![Partial](https://php-eye.com/badge/arvenil/ninja-mutex/partial.svg)](https://travis-ci.org/arvenil/ninja-mutex)
[![Not tested](https://php-eye.com/badge/arvenil/ninja-mutex/not-tested.svg)](https://travis-ci.org/arvenil/ninja-mutex)
[![Code Climate](https://codeclimate.com/github/arvenil/ninja-mutex/badges/gpa.svg)](https://codeclimate.com/github/arvenil/ninja-mutex)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/arvenil/ninja-mutex/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/arvenil/ninja-mutex/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/arvenil/ninja-mutex/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/arvenil/ninja-mutex/?branch=master)
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8 || ^5.5 || ^5.6",
"codeclimate/php-test-reporter": "^0.1 || ^0.3",
"scrutinizer/ocular": "^1.1 || ^1.3",
"phpunit/phpunit": "^4.8 || ^5.5 || ^5.6 || ^5.7",
"codeclimate/php-test-reporter": "^0.1 || ^0.3 || ^0.4",
"scrutinizer/ocular": "^1.1 || ^1.3 || ^1.4",
"mikey179/vfsStream": "^1.4 || ^1.5 || ^1.6",
"predis/predis": "^1.0 || ^1.1",
"ext-memcache": "*",
Expand All @@ -38,6 +38,6 @@
"predis/predis" : "Create mutex using Predis (client library for Redis)",
"ext-memcache": "Create mutex using memcache extension",
"ext-memcached": "Create mutex using memcached extension",
"ext-pdo_mysql": "Create mutex using MySql"
"ext-pdo_mysql": "Create mutex using MySQL PDO"
}
}
30 changes: 30 additions & 0 deletions src/Lock/BasicLockInformationProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* This file is part of ninja-mutex.
*
* (C) Kamil Dziedzic <arvenil@klecza.pl>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NinjaMutex\Lock;


class BasicLockInformationProvider implements LockInformationProviderInterface
{
/**
* @inheritdoc
*/
public function getLockInformation()
{
$pid = getmypid();
$hostname = gethostname();

$params = array();
$params[] = $pid;
$params[] = $hostname;

return $params;
}
}
10 changes: 1 addition & 9 deletions src/Lock/DirectoryLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,7 @@ public function __construct($dirname)
*/
protected function getLock($name, $blocking)
{
while (!@mkdir($this->getDirectoryPath($name))) {
if (!$blocking) {
return false;
}

usleep(rand(5000, 20000));
}

return true;
return @mkdir($this->getDirectoryPath($name));
}

/**
Expand Down
48 changes: 20 additions & 28 deletions src/Lock/LockAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ abstract class LockAbstract implements LockInterface
const USLEEP_TIME = 100;

/**
* Information which allows to track down process which acquired lock
* Provides information which allows to track down process which acquired lock
*
* @var array
* @var LockInformationProviderInterface
*/
protected $lockInformation = array();
protected $lockInformationProvider;

/**
* @var array
*/
protected $locks = array();

public function __construct()
public function __construct(LockInformationProviderInterface $informationProvider = null)
{
$this->lockInformation = $this->generateLockInformation();
$this->lockInformationProvider = $informationProvider ? : new BasicLockInformationProvider();
}

public function __clone()
Expand Down Expand Up @@ -93,43 +93,35 @@ public function acquireLock($name, $timeout = null)

/**
* @param string $name
* @param bool $blocking
* @param bool $blocking If lock provider supports blocking then you can pass this param through,
* otherwise, ignore this variable, default blocking method will be used.
* @return bool
*/
abstract protected function getLock($name, $blocking);

/**
* Information generate by this method allow to track down process which acquired lock
* Information returned by this method allow to track down process which acquired lock
* .
* By default it returns array with:
* 1. pid
* 2. server_ip
* 3. server_name
*
* @return array
*/
protected function generateLockInformation()
protected function getLockInformation()
{
$pid = getmypid();
$hostname = gethostname();
$host = gethostbyname($hostname);

// Compose data to one string
$params = array();
$params[] = $pid;
$params[] = $host;
$params[] = $hostname;
return $this->lockInformationProvider->getLockInformation();
}

return $params;
/**
* @return LockInformationProviderInterface
*/
public function getLockInformationProvider()
{
return $this->lockInformationProvider;
}

/**
* Information returned by this method allow to track down process which acquired lock
* .
* @return array
* @param LockInformationProviderInterface $lockInformationProvider
*/
protected function getLockInformation()
public function setLockInformationProvider($lockInformationProvider)
{
return $this->lockInformation;
$this->lockInformationProvider = $lockInformationProvider;
}
}
24 changes: 24 additions & 0 deletions src/Lock/LockInformationProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* This file is part of ninja-mutex.
*
* (C) Kamil Dziedzic <arvenil@klecza.pl>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NinjaMutex\Lock;

/**
* Provides lock debugging information
* @package NinjaMutex\Lock
*/
interface LockInformationProviderInterface
{
/**
* Gathers lock debug information
* @return array
*/
public function getLockInformation();
}
67 changes: 42 additions & 25 deletions src/Lock/MySqlLock.php → src/Lock/MySQLPDOLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,60 @@
use PDO;

/**
* Lock implementor using MySql
* Lock implementor using MySQL PDO driver
*
* @author Kamil Dziedzic <arvenil@klecza.pl>
*/
class MySqlLock extends LockAbstract
class MySQLPDOLock extends LockAbstract
{
/**
* MySql connections
* MySQL connections
*
* @var PDO[]
*/
protected $pdo = array();

protected $user;
protected $password;
protected $host;
protected $port;
/**
* @var string
*/
protected $dsn;
/**
* @var string
*/
protected $username;
/**
* @var string
*/
protected $passwd;
/**
* @var array
*/
protected $options;
/**
* @var string
*/
protected $classname;

/**
* Provide data for PDO connection
*
* @param string $user
* @param string $password
* @param string $host
* @param int $port
* @param string $classname class name to create as PDO connection
* @link http://php.net/manual/en/pdo.construct.php
* @param string $dsn
* @param string $username [optional]
* @param string $passwd [optional]
* @param array $options [optional]
*
* @param string $classname class name to create as PDO connection,
* by default this is PDO, but in tests we can inject MockPDO
*/
public function __construct($user, $password, $host, $port = 3306, $classname = 'PDO')
public function __construct($dsn, $username = null, $passwd = null, $options = null, $classname = 'PDO')
{
parent::__construct();

$this->user = $user;
$this->password = $password;
$this->host = $host;
$this->port = $port;
$this->dsn = $dsn;
$this->username = $username;
$this->passwd = $passwd;
$this->options = $options;
$this->classname = $classname;
}

Expand Down Expand Up @@ -84,8 +102,8 @@ protected function getLock($name, $blocking)
{
return !$this->isLocked($name) && $this->pdo[$name]->query(
sprintf(
'SELECT GET_LOCK("%s", %d)',
$name,
'SELECT GET_LOCK(%s, %d)',
$this->pdo[$name]->quote($name),
0
),
PDO::FETCH_COLUMN,
Expand All @@ -107,8 +125,8 @@ public function releaseLock($name)

$released = (bool) $this->pdo[$name]->query(
sprintf(
'SELECT RELEASE_LOCK("%s")',
$name
'SELECT RELEASE_LOCK(%s)',
$this->pdo[$name]->quote($name)
),
PDO::FETCH_COLUMN,
0
Expand Down Expand Up @@ -138,8 +156,8 @@ public function isLocked($name)

return !current($this->pdo)->query(
sprintf(
'SELECT IS_FREE_LOCK("%s")',
$name
'SELECT IS_FREE_LOCK(%s)',
current($this->pdo)->quote($name)
),
PDO::FETCH_COLUMN,
0
Expand All @@ -156,8 +174,7 @@ protected function setupPDO($name)
return true;
}

$dsn = sprintf('mysql:host=%s;port=%d', $this->host, $this->port);
$this->pdo[$name] = new $this->classname($dsn, $this->user, $this->password);
$this->pdo[$name] = new $this->classname($this->dsn, $this->username, $this->passwd, $this->options);

return true;
}
Expand Down
29 changes: 29 additions & 0 deletions src/Lock/ResolvedHostnameLockInformationProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* This file is part of ninja-mutex.
*
* (C) Kamil Dziedzic <arvenil@klecza.pl>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace NinjaMutex\Lock;


class ResolvedHostnameLockInformationProvider extends BasicLockInformationProvider
{
/**
* Adds resolved host IP to the provided information
* (WARNING! Using DNS queries at runtime may introduce significant delays to script execution, use with caution!)
* @return array
*/
public function getLockInformation()
{
$params = parent::gatherInformation();
$params[] = gethostbyname(gethostname());

return $params;
}

}
Loading

0 comments on commit 8653c53

Please sign in to comment.