Skip to content

Commit

Permalink
feat: 读写分离 clusterNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
linkunyuan committed Feb 3, 2024
1 parent f0f8fc6 commit 1b694ed
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/Consumer/BaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ abstract protected function consume($data = [], Redis $redis = null);
public function getListenQueues()
{
// 在集群模式中,将队列数据均匀分布在不同分片的槽位中
// 读一定要比写大!!!不然redis会爆!!!
$clusterNumber = config('QUEUE.clusterNumber');
$clusterNumberWrite = config('QUEUE.clusterNumberWrite');
$queue = $this->args['queue'];

// 读一定要比写大!!!不然redis会爆!!!
$cn = max($clusterNumber ?: 0, $clusterNumberWrite ?: 0);
$list[] = $queue;
if ($clusterNumber > 0) {
for ($i = 0; $i <= $clusterNumber; ++$i) {
if ($cn > 0) {
for ($i = 0; $i <= $cn; ++$i) {
$list[] = "$queue.$i";
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/function.php
Original file line number Diff line number Diff line change
Expand Up @@ -994,9 +994,12 @@ function redis_list_push(Redis $redis, string $key, $data, bool $left = false)
$data = json_encode($data);
}
$clusterNumber = config('QUEUE.clusterNumber');
if ($clusterNumber > 0) {
$clusterNumberWrite = config('QUEUE.clusterNumberWrite');
// 写一定要比读小!!!不然redis会爆!!!
$cn = min($clusterNumber ?: 0, $clusterNumberWrite ?: 0);
if ($cn > 0) {
mt_rand();
$index = mt_rand(-1, $clusterNumber);
$index = mt_rand(-1, $cn);
$index > 0 && $key .= ".$index";
}

Expand Down

0 comments on commit 1b694ed

Please sign in to comment.