/** * Returns the current interval. If none is set, a new one is randomly created. * @return int */ protected function getCurrentInterval() { if ($this->currentInterval === null) { $this->currentInterval = $this->randomizer->randNum($this->from, $this->to); } return $this->currentInterval; }
/** * @param RequestInterface $request * @return RotatingProxyInterface */ protected function getWorkingProxy(RequestInterface $request) { $waitingProxies = []; $waitingProxyTimes = []; while ($this->hasEnoughWorkingProxies()) { $randKey = $this->randomizer->randKey($this->workingProxies); // $randKey = array_rand($this->workingProxies); $proxy = $this->workingProxies[$randKey]; if (!$proxy->isUsable()) { unset($this->workingProxies[$randKey]); continue; } if ($proxy->hasToWait()) { $waitingProxyTimes[$randKey] = $proxy->getWaitingTime(); $waitingProxies[$randKey] = $proxy; unset($this->workingProxies[$randKey]); continue; } $this->workingProxies += $waitingProxies; return $proxy; } if (count($waitingProxies) > 0) { asort($waitingProxyTimes); reset($waitingProxyTimes); $minKey = key($waitingProxies); /** @var RotatingProxyInterface $minWaitingProxy */ $minWaitingProxy = $waitingProxies[$minKey]; // $minimumWaitingTime = ceil(reset($waitingProxyTimes)); $event = new WaitingEvent($minWaitingProxy); $this->getEmitter()->emit(self::EVENT_ON_WAIT, $event); $minimumWaitingTime = $minWaitingProxy->getWaitingTime(); // the WaitingTime might have been changed by a listener to the WaitingEvent if ($minimumWaitingTime > 0) { sleep($minimumWaitingTime); } $this->workingProxies += $waitingProxies; return $this->getWorkingProxy($request); } if ($this->useOwnIp) { $event = new UseOwnIpEvent(); $this->getEmitter()->emit(self::EVENT_ON_USE_OWN_IP, $event); return new NullProxy(); } $msg = "No proxies left and usage of own IP is forbidden"; throw new NoProxiesLeftException($this, $request, $msg); }
/** * Switches the current identity to a randomly chosen one. */ public function switchIdentity() { $key = $this->randomizer->randKey($this->identities); $this->currentIdentity = $this->identities[$key]; $this->randomCounter->restart(); }