下号比率恢复 + 触线降权 + 同步裁决
This commit is contained in:
@@ -22,15 +22,19 @@ class SplitRoundRobinStore
|
||||
|
||||
/**
|
||||
* 获取本次访问应使用的号码下标(0 .. count-1)
|
||||
*
|
||||
* @param string $poolKey 选号池标识(如 preferred / deferred),用于双层选号独立轮转
|
||||
*/
|
||||
public function nextIndex(int $splitLinkId, int $numberCount): int
|
||||
public function nextIndex(int $splitLinkId, int $numberCount, string $poolKey = 'main'): int
|
||||
{
|
||||
if ($splitLinkId <= 0 || $numberCount <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$poolKey = $this->sanitizePoolKey($poolKey);
|
||||
|
||||
if ($this->ensureRedis()) {
|
||||
$key = self::KEY_PREFIX . $splitLinkId;
|
||||
$key = self::KEY_PREFIX . $splitLinkId . ':' . $poolKey;
|
||||
$seq = (int) $this->redis->incr($key);
|
||||
if ($seq <= 0) {
|
||||
$seq = 1;
|
||||
@@ -39,7 +43,16 @@ class SplitRoundRobinStore
|
||||
return ($seq - 1) % $numberCount;
|
||||
}
|
||||
|
||||
return $this->nextIndexFallback($splitLinkId, $numberCount);
|
||||
return $this->nextIndexFallback($splitLinkId, $numberCount, $poolKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 选号池 key 仅允许字母数字与下划线,避免 Redis key 注入
|
||||
*/
|
||||
private function sanitizePoolKey(string $poolKey): string
|
||||
{
|
||||
$poolKey = preg_replace('/[^a-zA-Z0-9_]/', '', $poolKey) ?? '';
|
||||
return $poolKey !== '' ? $poolKey : 'main';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,14 +115,14 @@ class SplitRoundRobinStore
|
||||
/**
|
||||
* 无 Redis 时使用 runtime 文件锁递增(开发/单机可用;生产请启用 Redis)
|
||||
*/
|
||||
private function nextIndexFallback(int $splitLinkId, int $numberCount): int
|
||||
private function nextIndexFallback(int $splitLinkId, int $numberCount, string $poolKey = 'main'): int
|
||||
{
|
||||
$runtime = defined('RUNTIME_PATH') ? RUNTIME_PATH : (dirname(__DIR__, 3) . '/runtime/');
|
||||
$dir = $runtime . 'split_rr/';
|
||||
if (!is_dir($dir) && !@mkdir($dir, 0755, true)) {
|
||||
return 0;
|
||||
}
|
||||
$file = $dir . $splitLinkId . '.cnt';
|
||||
$file = $dir . $splitLinkId . '_' . $poolKey . '.cnt';
|
||||
$fp = @fopen($file, 'c+');
|
||||
if ($fp === false) {
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user