Esempio n. 1
0
 public function testRateExceeded()
 {
     $robot = new Robot();
     $robot->addRateLimit(new RateLimit(1, 1));
     $robot->addRateLimit(new RateLimit(5, 10));
     // Add 5 requests one after another every second
     for ($i = 0; $i < 4; $i++) {
         $robot->attach();
         $robot->detach();
         $this->assertTrue($robot->rateExceeded());
         sleep(1);
         $this->assertFalse($robot->rateExceeded());
     }
     // Add 6th request
     $robot->attach();
     $robot->detach();
     sleep(1);
     // Exceeded second rate limit 5 reqs / 10s
     $this->assertTrue($robot->rateExceeded());
 }
Esempio n. 2
0
 /**
  * @param Robot $robot
  */
 protected function refillRobot(Robot $robot)
 {
     if ($robot->rateExceeded()) {
         return;
     }
     while ($robot->queueNotFull() && ($handler = $this->nextRequest())) {
         $request = $handler->getRequest();
         $request->addListener('complete', function (Event $e) use($handler, $robot) {
             foreach ($e->request->getListeners('complete') as $listener) {
                 $e->request->removeListener('complete', $listener);
             }
             $robot->detach();
             $event = new RequestCompletedEvent($this, $robot, $e->response, $handler);
             $this->dispatch('request.completed', $event);
             $robot->dispatch('request.completed', $event);
             if (!$this->isPauseRequested()) {
                 $this->refillRobot($robot);
             }
         });
         $event = new RequestAttachingEvent($this, $robot, $handler);
         $this->dispatch('request.attaching', $event);
         $robot->dispatch('request.attaching', $event);
         $robot->attach();
         $this->queue->attach($request);
     }
 }