perf: 优化用户流量消费队列(上万用户流量信息数秒即可处理完成

This commit is contained in:
xboard
2023-11-21 15:59:06 +08:00
parent 9d2da393d7
commit 8db622eee4
4 changed files with 51 additions and 56 deletions

View File

@@ -363,7 +363,7 @@ class ServerService
->where('ips',"like", "%\"$nodeIp\"%")
->first();
default:
return false;
return null;
}
}
}

View File

@@ -168,26 +168,11 @@ class UserService
return true;
}
public function trafficFetch(array $server, string $protocol, array $data, array $childServer = null)
public function trafficFetch(array $server, string $protocol, array $data, string $nodeIp = null)
{
$statService = new StatisticalService();
$statService->setStartAt(strtotime(date('Y-m-d')));
$statService->setUserStats();
$statService->setServerStats();
foreach (array_keys($data) as $userId) {
$u = $data[$userId][0];
$d = $data[$userId][1];
// 如果存在子节点则使用过子节点的倍率进行进行流量计算该计算方式依赖服务器IP地址
if(!blank($childServer)){
TrafficFetchJob::dispatch($u, $d, $userId, $childServer, $protocol);
$statService->statUser($childServer['rate'], $userId, $u, $d);
$statService->statServer($childServer['id'], $protocol, $u, $d);
}else{
TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol);
$statService->statUser($server['rate'], $userId, $u, $d);
}
$statService->statServer($server['id'], $protocol, $u, $d);
}
$timestamp = strtotime(date('Y-m-d'));
collect($data)->chunk(1000)->each(function($chunk) use ($timestamp,$server,$protocol, $nodeIp){
TrafficFetchJob::dispatch($server, $chunk->toArray(), $protocol, $timestamp, $nodeIp);
});
}
}