Ejemplo n.º 1
0
 public function __invoke(RequestInterface $request, array $options)
 {
     $fn = $this->nextHandler;
     $throttleId = isset($options['throttle_id']) ? $options['throttle_id'] : null;
     $limit = isset($options['throttle_limit']) ? $options['throttle_limit'] : null;
     if (!$throttleId || !$limit) {
         // Request is not throttled; just ignore it.
         return $fn($request, $options);
     }
     if (!isset($this->running[$throttleId])) {
         $this->running[$throttleId] = 0;
         $this->queue[$throttleId] = [];
     }
     $promise = new Promise([\GuzzleHttp\Promise\queue(), 'run']);
     if ($this->running[$throttleId] + 1 <= $limit) {
         // Queue has enough space; run this request and watch for queue size.
         ++$this->running[$throttleId];
         return $fn($request, $options)->then($this->getQueuePopper($throttleId, true), $this->getQueuePopper($throttleId, false));
     }
     // The queue is full; delay the request, and don't forget to also pop the queue.
     $this->queue[$throttleId][] = function () use($request, $options, $fn, $throttleId, $promise) {
         $promise->resolve($fn($request, $options)->then($this->getQueuePopper($throttleId, true), $this->getQueuePopper($throttleId, false)));
     };
     return $promise;
 }
 public function testAsynchronouslyInvokesOnFulfilled()
 {
     $p = new FulfilledPromise('a');
     $r = null;
     $f = function ($d) use(&$r) {
         $r = $d;
     };
     $p2 = $p->then($f);
     $this->assertNotSame($p, $p2);
     $this->assertNull($r);
     \GuzzleHttp\Promise\queue()->run();
     $this->assertEquals('a', $r);
 }
Ejemplo n.º 3
0
 public function listen($port, $host = '127.0.0.1')
 {
     self::$loop = Factory::create();
     $socket = new SocketServer(self::$loop);
     $http = new HttpServer($socket);
     $queue = \GuzzleHttp\Promise\queue();
     $queue->run();
     self::$loop->addPeriodicTimer(0, [$queue, 'run']);
     //Add Intervals
     $intervals = Interval::getIntervals();
     if ($intervals instanceof \ArrayIterator) {
         foreach ($intervals as $interval) {
             self::$loop->addPeriodicTimer($interval->getTime(), $interval->getCallback());
         }
     }
     $http->on('request', $this->app);
     echo "Server running on {$host}:{$port}\n";
     $socket->listen($port, $host);
     self::$loop->run();
 }
 /**
  * @test
  */
 public function it_rejects_deferred_with_runtime_exception_asynchronous_when_json_response_is_invalid()
 {
     $producerInAsyncMode = new HttpMessageProducer($this->guzzleClient, new NoOpMessageConverter(), null, true);
     $fetchDataQuery = new FetchSomething(['filter' => 'foo']);
     $psrResponse = $this->prophesize(ResponseInterface::class);
     $queryDeferred = new Deferred();
     $producerInAsyncMode($fetchDataQuery, $queryDeferred);
     //Return invalid json
     $psrResponse->getBody()->willReturn('[{"data" => "bar"');
     $this->responsePromise->resolve($psrResponse->reveal());
     //Perform next tick, required to resolve the promise when we are in async mode
     $queue = \GuzzleHttp\Promise\queue();
     $queue->run();
     $rejectionReason = null;
     $queryDeferred->promise()->otherwise(function ($reason) use(&$rejectionReason) {
         $rejectionReason = $reason;
     });
     $this->assertInstanceOf(RuntimeException::class, $rejectionReason);
 }
Ejemplo n.º 5
0
 public function testCreatesPromiseWhenFulfilledBeforeThen()
 {
     $p = new Promise();
     $p->resolve('foo');
     $carry = null;
     $p2 = $p->then(function ($v) use(&$carry) {
         $carry = $v;
     });
     $this->assertNotSame($p, $p2);
     $this->assertNull($carry);
     \GuzzleHttp\Promise\queue()->run();
     $this->assertEquals('foo', $carry);
 }
 public function testCanResolveThenWithSuccess()
 {
     $actual = null;
     $p = new RejectedPromise('foo');
     $p->otherwise(function ($v) {
         return $v . ' bar';
     })->then(function ($v) use(&$actual) {
         $actual = $v;
     });
     \GuzzleHttp\Promise\queue()->run();
     $this->assertEquals('foo bar', $actual);
 }
 protected function invokeQueue()
 {
     $this->loop->addTimer(static::QUEUE_TIMER_INTERVAL, function () {
         \GuzzleHttp\Promise\queue()->run();
     });
 }
Ejemplo n.º 8
0
 private function processProcessMessages()
 {
     $clean = true;
     reset($this->processHandles);
     $now = time();
     $checkTimeout = $this->processTimeoutChecker !== $now;
     $this->processTimeoutChecker = $now;
     while ($entry = current($this->processHandles)) {
         next($this->processHandles);
         /** @var LoopHandle $easy */
         $easy = $entry['easy'];
         if ($checkTimeout) {
             try {
                 $easy->handle->checkTimeout();
             } catch (ProcessFailedException $timeoutException) {
             }
         }
         if ($easy->handle->isRunning()) {
             continue;
         }
         /** @var Promise $deferred */
         $deferred = $entry['promise'];
         unset($this->processHandles[$entry['id']]);
         if ($easy->handle->isSuccessful()) {
             $deferred->resolve(isset($timeoutException) ? new RejectedPromise($timeoutException) : $easy->handle);
         } else {
             $deferred->reject(new ProcessFailedException($easy->handle));
         }
         $clean = false;
     }
     if (!$clean) {
         \GuzzleHttp\Promise\queue()->run();
         return true;
     }
     return false;
 }