|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Utopia\Swoole\Dispatch; |
| 4 | + |
| 5 | +use Swoole\Server; |
| 6 | + |
| 7 | +abstract class RiskyRequest extends ContextualDispatch |
| 8 | +{ |
| 9 | + /** |
| 10 | + * @param int $riskyWorkersPercent 0 - 100 |
| 11 | + */ |
| 12 | + public function __construct( |
| 13 | + readonly int $dispatcherMapSize, |
| 14 | + private readonly int $riskyWorkersPercent |
| 15 | + ) |
| 16 | + { |
| 17 | + parent::__construct($dispatcherMapSize); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * @param string $request |
| 22 | + * @param string $domain |
| 23 | + * @return bool |
| 24 | + */ |
| 25 | + abstract protected function isRisky(string $request, string $domain): bool; |
| 26 | + |
| 27 | + protected function randomRiskyWorker(int $riskyWorkers, int $totalWorkers): int |
| 28 | + { |
| 29 | + return rand($riskyWorkers, $totalWorkers - 1); |
| 30 | + } |
| 31 | + |
| 32 | + protected function randomSafeWorker(int $riskyWorkers): int |
| 33 | + { |
| 34 | + return rand(0, $riskyWorkers - 1); |
| 35 | + } |
| 36 | + |
| 37 | + protected function resolveWorkerId(Server $server, ?string $data): int |
| 38 | + { |
| 39 | + $totalWorkers = $server->setting['worker_num']; |
| 40 | + |
| 41 | + // If data is not set, we can send the request to any worker. |
| 42 | + // First we try to pick an idle worker, otherwise we randomly pick a worker. |
| 43 | + if (empty($data)) { |
| 44 | + for ($i = 0; $i < $totalWorkers; $i++) { |
| 45 | + if ($server->getWorkerStatus($i) === SWOOLE_WORKER_IDLE) { |
| 46 | + return $i; |
| 47 | + } |
| 48 | + } |
| 49 | + return rand(0, $totalWorkers - 1); |
| 50 | + } |
| 51 | + |
| 52 | + // Each worker has a numeric ID, starting from 0 and incrementing |
| 53 | + // From 0 to $riskyWorkers, we consider safe workers |
| 54 | + // From $riskyWorkers to $totalWorkers, we consider risky workers |
| 55 | + $riskyWorkers = (int) floor($totalWorkers * $this->riskyWorkersPercent); // Absolute number of risky workers |
| 56 | + |
| 57 | + $headers = explode("\n", strstr($data, "\r\n", true)); |
| 58 | + $request = $headers[0]; |
| 59 | + $domain = ''; |
| 60 | + if (count($headers) > 1) { |
| 61 | + $domain = trim(explode('Host: ', $headers[1])[1]); |
| 62 | + } |
| 63 | + |
| 64 | + $risky = $this->isRisky($request, $domain); |
| 65 | + |
| 66 | + if ($risky) { |
| 67 | + // If risky request, only consider risky workers |
| 68 | + for ($j = $riskyWorkers; $j < $totalWorkers; $j++) { |
| 69 | + /** Reference https://openswoole.com/docs/modules/swoole-server-getWorkerStatus#description */ |
| 70 | + if ($server->getWorkerStatus($j) === SWOOLE_WORKER_IDLE) { |
| 71 | + // If idle worker found, give to him |
| 72 | + return $j; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // If no idle workers, give to random risky worker |
| 77 | + return $this->randomRiskyWorker($riskyWorkers, $totalWorkers); |
| 78 | + } |
| 79 | + |
| 80 | + // If safe request, give to any idle worker |
| 81 | + // It's fine to pick a risky worker here because it's idle. Idle is never actually risky |
| 82 | + for ($i = 0; $i < $totalWorkers; $i++) { |
| 83 | + if ($server->getWorkerStatus($i) === SWOOLE_WORKER_IDLE) { |
| 84 | + return $i; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // If no idle worker found, give to a random safe worker |
| 89 | + // We avoid risky workers here, as it could be in work - not idle. That's exactly when they are risky. |
| 90 | + return $this->randomSafeWorker($riskyWorkers); |
| 91 | + } |
| 92 | +} |
0 commit comments