Skip to content

Commit

Permalink
Merge pull request #30 from piwik/18
Browse files Browse the repository at this point in the history
Use mt_rand if available instead of rand
  • Loading branch information
tsteur committed Jan 13, 2016
2 parents 8aa8c07 + 24d08fb commit 5277167
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
19 changes: 18 additions & 1 deletion Queue/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,23 @@ public function canAcquireMoreLocks()
return $this->lock->getNumberOfAcquiredLocks() < $this->numQueuesAvailable;
}

private function getRandomQueueId()
{
static $useMtRand;

if (!isset($useMtRand)) {
$useMtRand = function_exists('mt_rand');
}

if ($useMtRand) {
$rand = mt_rand(0, $this->numQueuesAvailable - 1);
} else {
$rand = rand(0, $this->numQueuesAvailable - 1);
}

return $rand;
}

/**
* @return Queue
*/
Expand All @@ -232,7 +249,7 @@ public function lockNext()

if ($this->currentQueueId < 0) {
// we just want to avoid to always start looking for the queue at position 0
$this->currentQueueId = rand(0, $this->numQueuesAvailable - 1);
$this->currentQueueId = $this->getRandomQueueId();
}

// here we look for all available queues whether at least one should and can be processed
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ __Are there any known issues?__

## Changelog

0.2.5

- Use a better random number generator if available on the system to more evenly process queues.

0.2.4

- The command `queuedtracking:monitor` will now work even when the queue is disabled
Expand Down
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "QueuedTracking",
"version": "0.2.4",
"version": "0.2.5",
"description": "Scale your large traffic Piwik service by queuing tracking requests in Redis for better performance. ",
"theme": false,
"keywords": ["tracker", "tracking", "queue", "redis"],
Expand Down

0 comments on commit 5277167

Please sign in to comment.