-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Teknasyon-Teknoloji/master
AWS SQS Job Locker to Prevent Duplication
- Loading branch information
Showing
14 changed files
with
386 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
extension = "memcached.so" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Simpleue\Locker; | ||
|
||
abstract class BaseLocker implements Locker | ||
{ | ||
protected $uniqIdFunction; | ||
protected $keyPrefix = 'sqslocker-'; | ||
|
||
public function __construct() | ||
{ | ||
$this->uniqIdFunction = function ($job) { | ||
return md5(strtolower($job)); | ||
}; | ||
} | ||
|
||
public function setJobUniqIdFunction(\Closure $function) | ||
{ | ||
$this->uniqIdFunction = $function; | ||
} | ||
|
||
public function getJobUniqId($job) | ||
{ | ||
if ($this->uniqIdFunction) { | ||
$func = $this->uniqIdFunction; | ||
return $this->keyPrefix . $func($job); | ||
} else { | ||
throw new \InvalidArgumentException('Locker::uniqIdFunction not defined!'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Simpleue\Locker; | ||
|
||
interface Locker | ||
{ | ||
public function getLockerInfo(); | ||
public function lock($job, $timeout = 40); | ||
public function disconnect(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Simpleue\Locker; | ||
|
||
class MemcachedLocker extends BaseLocker | ||
{ | ||
/** | ||
* @var \Memcached; | ||
*/ | ||
private $memcached; | ||
|
||
public function __construct(\Memcached $memcached) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->memcached = $memcached; | ||
$this->memcached->setOptions([ | ||
\Memcached::OPT_TCP_NODELAY => true, | ||
\Memcached::OPT_NO_BLOCK => true, | ||
\Memcached::OPT_CONNECT_TIMEOUT => 60 | ||
]); | ||
} | ||
|
||
public function getLockerInfo() | ||
{ | ||
return 'Memcached ( ' . json_encode($this->memcached->getServerList()) . ' )'; | ||
} | ||
|
||
public function lock($job, $timeout = 30) | ||
{ | ||
if (!$job) { | ||
throw new \RuntimeException('Job for lock is invalid!'); | ||
} | ||
return $this->memcached->add( | ||
$this->getJobUniqId($job), | ||
time() + $timeout + 1, | ||
$timeout | ||
); | ||
} | ||
|
||
public function disconnect() | ||
{ | ||
$this->memcached->quit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Simpleue\Locker; | ||
|
||
class RedisLocker extends BaseLocker | ||
{ | ||
/** | ||
* @var \Redis; | ||
*/ | ||
private $redis; | ||
|
||
public function __construct(\Redis $redisClient) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->redis = $redisClient; | ||
if ($this->redis->isConnected()===false) { | ||
throw new \RuntimeException('Redis Client not connected!'); | ||
} | ||
} | ||
|
||
public function getLockerInfo() | ||
{ | ||
return 'Redis ( ' | ||
. $this->redis->getHost() | ||
. ':' . $this->redis->getPort() | ||
. ' -> ' . $this->redis->getDbNum() | ||
. ' )'; | ||
} | ||
|
||
public function lock($job, $timeout = 40) | ||
{ | ||
if (!$job) { | ||
throw new \RuntimeException('Job for lock is invalid!'); | ||
} | ||
$key = $this->getJobUniqId($job); | ||
$status = $this->redis->set( | ||
$key, | ||
time() + $timeout + 1, | ||
array('nx', 'ex' => $timeout) | ||
); | ||
if ($status) { | ||
return true; | ||
} | ||
|
||
$currentLockTimestamp = $this->redis->get($key); | ||
if ($currentLockTimestamp > time()) { | ||
return false; | ||
} | ||
$oldLockTimestamp = $this->redis->getSet($key, (time() + $timeout + 1)); | ||
if ($oldLockTimestamp > time()) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public function disconnect() | ||
{ | ||
$this->redis->close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Simpleue\Unitary\Locker; | ||
|
||
use Simpleue\Locker\MemcachedLocker; | ||
|
||
class MemcachedLockerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $memcachedClientMock; | ||
/** | ||
* @var MemcachedLocker | ||
*/ | ||
private $memcachedLocker; | ||
|
||
protected function setUp() | ||
{ | ||
$this->memcachedClientMock = $this->getMock( | ||
'\Memcached', | ||
array('addServer', 'setOptions', 'getServerList', 'add', 'quit') | ||
); | ||
$this->memcachedClientMock->expects($this->any())->method('getServerList')->willReturn( | ||
[['host' => 'localhost', 'port' => 11211, 'weight'=>10]] | ||
); | ||
|
||
$this->memcachedLocker = new MemcachedLocker($this->memcachedClientMock); | ||
} | ||
|
||
public function testGetJobUniqId() | ||
{ | ||
$job = '{"string": "example", "uniqid":"123"}'; | ||
$this->assertEquals( | ||
'sqslocker-'.md5(strtolower($job)), | ||
$this->memcachedLocker->getJobUniqId($job) | ||
); | ||
$this->memcachedLocker->setJobUniqIdFunction(function ($job) { | ||
return json_decode($job, true)['uniqid']; | ||
}); | ||
$this->assertEquals( | ||
'sqslocker-123', | ||
$this->memcachedLocker->getJobUniqId($job) | ||
); | ||
} | ||
|
||
public function testGetLockerInfo() | ||
{ | ||
$this->assertEquals( | ||
'Memcached ( ' . json_encode([['host' => 'localhost', 'port' => 11211, 'weight'=>10]]) . ' )', | ||
$this->memcachedLocker->getLockerInfo() | ||
); | ||
} | ||
|
||
public function testLock() | ||
{ | ||
$job = '{"string": "example", "uniqid":"123"}'; | ||
$this->memcachedClientMock->expects($this->at(0))->method('add')->willReturn(true); | ||
$this->memcachedClientMock->expects($this->at(1))->method('add')->willReturn(false); | ||
$this->assertTrue($this->memcachedLocker->lock($job, 60)); | ||
$this->assertFalse($this->memcachedLocker->lock($job, 60)); | ||
|
||
$this->setExpectedException('RuntimeException', 'Job for lock is invalid!'); | ||
$this->memcachedLocker->lock(false, 60); | ||
} | ||
} |
Oops, something went wrong.