public function consume($tokens, &$seconds = 0)
    {
        try {
            if ($tokens > $this->capacity) {
                throw new \LengthException("Token amount ($tokens) is larger than the capacity ($this->capacity).");
            }
            if ($tokens <= 0) {
                throw new \InvalidArgumentException("Token amount ($tokens) should be greater than 0.");
            }

            return $this->storage->getMutex()->synchronized(
                function () use ($tokens, &$seconds) {
                    $tokensAndMicrotime = $this->loadTokensAndTimestamp();
                    $microtime = $tokensAndMicrotime["microtime"];
                    $availableTokens = $tokensAndMicrotime["tokens"];

                    $delta = $availableTokens - $tokens;
                    if ($delta < 0) {
                        $this->storage->letMicrotimeUnchanged();
                        $passed  = microtime(true) - $microtime;
                        $seconds = max(0, $this->tokenConverter->convertTokensToSeconds($tokens) - $passed);
                        return false;
                    } else {
                        $microtime += $this->tokenConverter->convertTokensToSeconds($tokens);
                        $this->storage->setMicrotime($microtime);
                        $seconds = 0;
                        return true;
                    }
                }
            );
        } catch (MutexException $e) {
            throw new StorageException("Could not lock token consumption.", 0, $e);
        }
    }
public function consume($tokens, &$seconds = 0)
    {
        try {
            if ($tokens > $this->capacity) {
                throw new \LengthException("Token amount ($tokens) is larger than the capacity ($this->capacity).");
            }
            if ($tokens <= 0) {
                throw new \InvalidArgumentException("Token amount ($tokens) should be greater than 0.");
            }

            return $this->storage->getMutex()->synchronized(
                function () use ($tokens, &$seconds) {
                    $tokensAndMicrotime = $this->loadTokensAndTimestamp();
                    $microtime = $tokensAndMicrotime["microtime"];
                    $availableTokens = $tokensAndMicrotime["tokens"];

                    $delta = $availableTokens - $tokens;
                    if ($delta < 0) {
                        $this->storage->letMicrotimeUnchanged();
                        $passed  = microtime(true) - $microtime;
                        $seconds = max(0, $this->tokenConverter->convertTokensToSeconds($tokens) - $passed);
                        return false;
                    } else {
                        $microtime += $this->tokenConverter->convertTokensToSeconds($tokens);
                        $this->storage->setMicrotime($microtime);
                        $seconds = 0;
                        return true;
                    }
                }
            );
        } catch (MutexException $e) {
            throw new StorageException("Could not lock token consumption.", 0, $e);
        }
    }
public function consume ( $ tokens , & $ seconds = 0 ) { try { if ( $ tokens > $ this -> capacity ) { throw new \ LengthException ( "Token amount ($tokens) is larger than the capacity ($this->capacity)." ) ; } if ( $ tokens <= 0 ) { throw new \ InvalidArgumentException ( "Token amount ($tokens) should be greater than 0." ) ; } return $ this -> storage -> getMutex ( ) -> synchronized ( function ( ) use ( $ tokens , & $ seconds ) { $ tokensAndMicrotime = $ this -> loadTokensAndTimestamp ( ) ; $ microtime = $ tokensAndMicrotime [ "microtime" ] ; $ availableTokens = $ tokensAndMicrotime [ "tokens" ] ; $ delta = $ availableTokens - $ tokens ; if ( $ delta < 0 ) { $ this -> storage -> letMicrotimeUnchanged ( ) ; $ passed = microtime ( true ) - $ microtime ; $ seconds = max ( 0 , $ this -> tokenConverter -> convertTokensToSeconds ( $ tokens ) - $ passed ) ; return false ; } else { $ microtime += $ this -> tokenConverter -> convertTokensToSeconds ( $ tokens ) ; $ this -> storage -> setMicrotime ( $ microtime ) ; $ seconds = 0 ; return true ; } } ) ; } catch ( MutexException $ e ) { throw new StorageException ( "Could not lock token consumption." , 0 , $ e ) ; } }
Consumes tokens from the bucket.

This method consumes only tokens if there are sufficient tokens available.
If there aren't sufficient tokens, no tokens will be removed and the
remaining seconds to wait are written to $seconds.

This method is threadsafe.

@param int    $tokens   The token amount.
@param double &$seconds The seconds to wait.

@return bool If tokens were consumed.
@SuppressWarnings(PHPMD)

@throws \LengthException The token amount is larger than the capacity.
@throws StorageException The stored microtime could not be accessed.
Consumes tokens from the bucket .