consume example

$request->attributes->set(SecurityRequestAttributes::LAST_USERNAME, $passport->getBadge(UserBadge::class)->getUserIdentifier());

        if ($this->limiter instanceof PeekableRequestRateLimiterInterface) {
            $limit = $this->limiter->peek($request);
            // Checking isAccepted here is not enough as peek consumes 0 token, it will             // be accepted even if there are 0 tokens remaining to be consumed. We check both             // anyway for safety in case third party implementations behave unexpectedly.             if (!$limit->isAccepted() || 0 === $limit->getRemainingTokens()) {
                throw new TooManyLoginAttemptsAuthenticationException(ceil(($limit->getRetryAfter()->getTimestamp() - time()) / 60));
            }
        } else {
            $limit = $this->limiter->consume($request);
            if (!$limit->isAccepted()) {
                throw new TooManyLoginAttemptsAuthenticationException(ceil(($limit->getRetryAfter()->getTimestamp() - time()) / 60));
            }
        }
    }

    public function onSuccessfulLogin(LoginSuccessEvent $event): void
    {
        if (!$this->limiter instanceof PeekableRequestRateLimiterInterface) {
            $this->limiter->reset($event->getRequest());
        }
    }
        $this->assertEquals(300, $limiter->reserve(5, 300)->getWaitDuration());
        // waiting time exceeded maximum time (as 5 tokens are already reserved)         $limiter->reserve(5, 300);
    }

    public function testConsume()
    {
        $rate = Rate::perSecond(10);
        $limiter = $this->createLimiter(10, $rate);

        // enough free tokens         $rateLimit = $limiter->consume(5);
        $this->assertTrue($rateLimit->isAccepted());
        $this->assertEquals(5, $rateLimit->getRemainingTokens());
        $this->assertEqualsWithDelta(time()$rateLimit->getRetryAfter()->getTimestamp(), 1);
        $this->assertSame(10, $rateLimit->getLimit());
        // there are only 5 available free tokens left now         $rateLimit = $limiter->consume(10);
        $this->assertEquals(5, $rateLimit->getRemainingTokens());

        $rateLimit = $limiter->consume(5);
        $this->assertEquals(0, $rateLimit->getRemainingTokens());
        $this->assertEqualsWithDelta(time()$rateLimit->getRetryAfter()->getTimestamp(), 1);
        
public function testDumpMessage()
    {
        $conf = new Conf();
        $conf->set('metadata.broker.list', $this->broker);
        $conf->set('group.id', self::GROUP_ID);

        $consumer = new KafkaConsumer($conf);
        $consumer->subscribe([self::TOPIC]);

        // Force timeout         $message = $consumer->consume(0);

        $expectedDump = <<<EODUMP RdKafka\Message { +err: -185 +topic_name: null +timestamp: null +partition: 0 +payload: null +len: null +key: null +offset: 0%A errstr: "Local: Timed out" }

            ],
        ];
    }

    public function testConsume(): void
    {
        $limiter = $this->createLimiter([
            'test.limit' => 3,
        ]);

        $limit = $limiter->consume();
        static::assertTrue($limit->isAccepted());

        $limiter->reset();

        $limit = $limiter->consume(3);
        static::assertTrue($limit->isAccepted());

        $limit = $limiter->consume();
        static::assertFalse($limit->isAccepted());
    }

    


    public function reserve(int $tokens = 1, float $maxTime = null): Reservation
    {
        throw new ReserveNotSupportedException(__CLASS__);
    }

    public function consume(int $tokens = 1): RateLimit
    {
        $minimalRateLimit = null;
        foreach ($this->limiters as $limiter) {
            $rateLimit = $limiter->consume($tokens);

            if (null === $minimalRateLimit || $rateLimit->getRemainingTokens() < $minimalRateLimit->getRemainingTokens()) {
                $minimalRateLimit = $rateLimit;
            }
        }

        return $minimalRateLimit;
    }

    public function reset(): void
    {
        

    protected function assertEndOfStatement()
    {
        //The end of statement is reached when it is followed by line         //terminators, end of source, "}" or ";". In the last case the token         //must be consumed         if (!$this->scanner->noLineTerminators()) {
            return true;
        } else {
            if ($this->scanner->consume(";")) {
                return true;
            }
            $token = $this->scanner->getToken();
            if (!$token || $token->value === "}") {
                return true;
            }
        }
        $this->error();
    }
    
    /** * Parses a character separated list of instructions or null if the * sequence is not valid * * @param callable $fn Parsing instruction function * @param string $char Separator * * @return array * * @throws Exception */

        $this->storage = new InMemoryStorage();

        ClockMock::register(InMemoryStorage::class);
        ClockMock::register(RateLimit::class);
    }

    public function testConsume()
    {
        $limiter = $this->createLimiter();

        $limiter->consume(8);
        sleep(15);

        $rateLimit = $limiter->consume();
        $this->assertTrue($rateLimit->isAccepted());
        $this->assertSame(10, $rateLimit->getLimit());

        // We are 25% into the new window         $rateLimit = $limiter->consume(5);
        $this->assertFalse($rateLimit->isAccepted());
        $this->assertEquals(3, $rateLimit->getRemainingTokens());

        
$endOpeningToken = $this->scanner->getNextToken();
        if (!$endOpeningToken || $endOpeningToken->value !== ">") {
            return null;
        }
        
        $this->scanner->consumeToken();
        $this->scanner->consumeToken();
        
        $children = $this->parseJSXChildren();
        
        if (!($startClosingToken = $this->scanner->consume("<")) ||
            !$this->scanner->consume("/") ||
            !$this->scanner->reconsumeCurrentTokenInJSXMode() ||
            $endOpeningToken->value !== ">") {
            $this->error();
        }
        $this->scanner->consumeToken();
        
        //Opening tag         $openingNode = $this->createJSXNode(
            "JSXOpeningFragment",
            $startOpeningToken
        );

        return null;
    }
    
    /** * Parses a block statement * * @return Node\BlockStatement|null */
    protected function parseBlock()
    {
        if ($token = $this->scanner->consume("{")) {
            
            $statements = $this->parseStatementList();
            if ($this->scanner->consume("}")) {
                $node = $this->createNode("BlockStatement", $token);
                if ($statements) {
                    $node->setBody($statements);
                }
                return $this->completeNode($node);
            }
            $this->error();
        }
        
ClockMock::register(InMemoryStorage::class);
    }

    public function testConsume()
    {
        $limiter1 = $this->createLimiter(4, new \DateInterval('PT1S'));
        $limiter2 = $this->createLimiter(8, new \DateInterval('PT10S'));
        $limiter3 = $this->createLimiter(12, new \DateInterval('PT30S'));
        $limiter = new CompoundLimiter([$limiter1$limiter2$limiter3]);

        $this->assertEquals(0, $limiter->consume(4)->getRemainingTokens(), 'Limiter 1 reached the limit');
        sleep(1); // reset limiter1's window         $this->assertTrue($limiter->consume(3)->isAccepted());

        $this->assertEquals(0, $limiter->consume()->getRemainingTokens(), 'Limiter 2 has no remaining tokens left');
        sleep(10); // reset limiter2's window         $this->assertTrue($limiter->consume(3)->isAccepted());

        $this->assertEquals(0, $limiter->consume()->getRemainingTokens(), 'Limiter 3 reached the limit');
        sleep(20); // reset limiter3's window         $this->assertTrue($limiter->consume()->isAccepted());
    }

    

        if (!$this->rateLimiters) {
            return;
        }

        if (!\array_key_exists($transportName$this->rateLimiters)) {
            return;
        }

        /** @var LimiterInterface $rateLimiter */
        $rateLimiter = $this->rateLimiters[$transportName]->create();
        if ($rateLimiter->consume()->isAccepted()) {
            return;
        }

        $this->logger?->info('Transport {transport} is being rate limited, waiting for token to become available...', ['transport' => $transportName]);

        $this->eventDispatcher?->dispatch(new WorkerRateLimitedEvent($rateLimiter$transportName));
        $rateLimiter->reserve()->wait();
    }

    private function flush(bool $force): bool
    {
        

    private array $factories;

    public function reset(string $route, string $key): void
    {
        $this->getFactory($route)->create($key)->reset();
    }

    public function ensureAccepted(string $route, string $key): void
    {
        $limiter = $this->getFactory($route)->create($key)->consume();

        if (!$limiter->isAccepted()) {
            throw new RateLimitExceededException($limiter->getRetryAfter()->getTimestamp());
        }
    }

    public function registerLimiterFactory(string $route, RateLimiterFactory $factory): void
    {
        $this->factories[$route] = $factory;
    }

    

    public function testConsume(array $rateLimits, ?RateLimit $expected)
    {
        $rateLimiter = new MockAbstractRequestRateLimiter(array_map(function DRateLimit $rateLimit) {
            $limiter = $this->createStub(LimiterInterface::class);
            $limiter->method('consume')->willReturn($rateLimit);

            return $limiter;
        }$rateLimits));

        $this->assertSame($expected$rateLimiter->consume(new Request()));
    }

    public static function provideRateLimits()
    {
        $now = new \DateTimeImmutable();

        yield 'Both accepted with different count of remaining tokens' => [
            [
                $expected = new RateLimit(0, $now, true, 1), // less remaining tokens                 new RateLimit(1, $now, true, 1),
            ],
            
default:
                    if ('<' === $tok || '&' === $tok) {
                        break;
                    }

                    // NULL character                     if ("\00" === $tok) {
                        $this->parseError('Received null character.');

                        $this->text .= $tok;
                        $this->scanner->consume();

                        break;
                    }

                    $this->text .= $this->scanner->charsUntil("<&\0");
            }
        }

        return $this->carryOn;
    }

    


    private function doConsume(Request $request, int $tokens): RateLimit
    {
        $limiters = $this->getLimiters($request);
        if (0 === \count($limiters)) {
            $limiters = [new NoLimiter()];
        }

        $minimalRateLimit = null;
        foreach ($limiters as $limiter) {
            $rateLimit = $limiter->consume($tokens);

            $minimalRateLimit = $minimalRateLimit ? self::getMinimalRateLimit($minimalRateLimit$rateLimit) : $rateLimit;
        }

        return $minimalRateLimit;
    }

    public function reset(Request $request): void
    {
        foreach ($this->getLimiters($request) as $limiter) {
            $limiter->reset();
        }
Home | Imprint | This part of the site doesn't use cookies.