Skip to content

Commit 815b55b

Browse files
Switch to \random_int(). Fixes #7 #9 #10
1 parent 9b14dc6 commit 815b55b

File tree

6 files changed

+5
-66
lines changed

6 files changed

+5
-66
lines changed

classes/mutex/PHPRedisMutex.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
*
1313
* This implementation requires at least phpredis-2.2.4.
1414
*
15-
* Note: If you're going to use this mutex in a forked process, you have to call
16-
* {@link seedRandom()} in each instance.
17-
*
1815
* @author Markus Malkusch <[email protected]>
1916
* @license WTFPL
2017
*

classes/mutex/PredisMutex.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
/**
1111
* Mutex based on the Redlock algorithm using the Predis API.
1212
*
13-
* Note: If you're going to use this mutex in a forked process, you have to call
14-
* {@link seedRandom()} in each instance.
15-
*
1613
* @author Markus Malkusch <[email protected]>
1714
* @license WTFPL
1815
*

classes/mutex/RedisMutex.php

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
/**
1212
* Mutex based on the Redlock algorithm.
1313
*
14-
* Note: If you're going to use this mutex in a forked process, you have to call
15-
* {@link seedRandom()} in each instance.
16-
*
1714
* @author Markus Malkusch <[email protected]>
1815
* @license WTFPL
1916
*
@@ -53,7 +50,6 @@ public function __construct(array $redisAPIs, $name, $timeout = 3)
5350

5451
$this->redisAPIs = $redisAPIs;
5552
$this->logger = new NullLogger();
56-
$this->seedRandom();
5753
}
5854

5955
/**
@@ -71,21 +67,6 @@ public function setLogger(LoggerInterface $logger)
7167
$this->logger = $logger;
7268
}
7369

74-
/**
75-
* Seeds the random number generator.
76-
*
77-
* Normally you don't need to seed, as this happens automatically. But
78-
* if you experience a {@link LockReleaseException} this might come
79-
* from identically created random tokens. In this case you could seed
80-
* from /dev/urandom.
81-
*
82-
* @param int|null $seed The optional seed.
83-
*/
84-
public function seedRandom($seed = null)
85-
{
86-
is_null($seed) ? srand() : srand($seed);
87-
}
88-
8970
/**
9071
* @SuppressWarnings(PHPMD)
9172
* @internal
@@ -98,7 +79,7 @@ protected function acquire($key, $expire)
9879
// 2.
9980
$acquired = 0;
10081
$errored = 0;
101-
$this->token = rand();
82+
$this->token = \random_int(0, 2147483647);
10283
$exception = null;
10384
foreach ($this->redisAPIs as $redis) {
10485
try {

classes/util/Loop.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function execute(callable $code)
7777
}
7878
$min = $minWait * pow(2, $i);
7979
$max = $min * 2;
80-
$usleep = rand($min, $max);
80+
$usleep = \random_int($min, $max);
8181

8282
usleep($usleep);
8383
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
},
1919
"require": {
2020
"php": ">=5.6",
21-
"psr/log": "^1"
21+
"psr/log": "^1",
22+
"paragonie/random_compat": "^1|^2"
2223
},
2324
"require-dev": {
2425
"phpunit/phpunit": "^5",

tests/mutex/RedisMutexTest.php

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -36,45 +36,8 @@ protected function setUp()
3636
$this->registerForTearDown($sleep);
3737
}
3838

39-
40-
/**
41-
* Tests seeding produces different tokens for each process.
42-
*
43-
* @test
44-
*/
45-
public function testSeedRandom()
46-
{
47-
$mutex = $this->buildRedisMutex(1);
48-
$mutex->seedRandom();
49-
50-
$tokens = [];
51-
$processManager = new ProcessManager();
52-
for ($i = 0; $i < 2; $i++) {
53-
$processManager->fork(function () {
54-
$mutex = $this->buildRedisMutex(1);
55-
$mutex->expects($this->any())->method("evalScript")->willReturn(true);
56-
57-
$token = null;
58-
$mutex->expects($this->any())->method("add")->willReturnCallback(
59-
function ($redisAPI, $key, $value, $expire) use (&$token) {
60-
$token = "$value";
61-
return true;
62-
}
63-
);
64-
65-
$mutex->synchronized(function () {
66-
});
67-
68-
return $token;
69-
})->then(function (Fork $fork) use (&$tokens) {
70-
$this->assertArrayNotHasKey($fork->getResult(), $tokens);
71-
$tokens[$fork->getResult()] = $fork->getResult();
72-
});
73-
}
74-
}
75-
7639
/**
77-
* Builds a testaböe RedisMutex mock.
40+
* Builds a testable RedisMutex mock.
7841
*
7942
* @param int $count The amount of redis apis.
8043
* @param int $timeout The timeout.

0 commit comments

Comments
 (0)