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