Skip to content

Commit

Permalink
Be more strict
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadyita committed Feb 21, 2024
1 parent f615ff5 commit a26c5c7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"scripts": {
"tests": [
"phpunit -c phpunit.xml --no-output",
"phpstan analyse --memory-limit 512M --no-ansi -c phpstan.neon -q",
"phpstan analyse --memory-limit 512M --no-ansi -cphpstan.neon --error-format=github --no-progress",
"phpcs --no-colors --standard=style/Nadybot/ruleset.xml -q src"
]
}
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 7
level: 9
paths:
- src
parallel:
Expand Down
56 changes: 50 additions & 6 deletions src/LeakyBucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Error;
use InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Revolt\EventLoop;
use Revolt\EventLoop\Suspension;
Expand All @@ -16,14 +17,47 @@ class LeakyBucket {

protected ?string $cancellationToken = null;

/**
* Create a new leaky bucket with the given parameters
*
* @param int $size The total size of the bucket. How many tokens fit in
* @param float $refillDelay The delay in seconds for the bucket to be refilled
* by $refillAmount
* @param int $refillAmount How many tokens to add to the bucket every $refillDelay
* @param null|int $startFill How much is in the bucket initially? NULL means it's full
* @param null|LoggerInterface $logger An optional logger to pas
*
* @phpstan-param positive-int $size
* @phpstan-param positive-int $refillAmount
*
* @throws InvalidArgumentException
*/
final public function __construct(
public int $size,
public float $refillDelay,
public int $refillAmount=1,
?int $startAmount=null,
public readonly int $size,
public readonly float $refillDelay,
public readonly int $refillAmount=1,
?int $startFill=null,
protected ?LoggerInterface $logger=null,
) {
$this->fill = $startAmount ?? $this->size;
/** @phpstan-ignore-next-line */
if ($size < 1) {
throw new InvalidArgumentException(
__CLASS__ ."::" . __FUNCTION__ . "(\$size) needs to be a positive integer."
);
}
if ($refillDelay < 0) {
throw new InvalidArgumentException(
__CLASS__ ."::" . __FUNCTION__ . "(\$refillDelay) needs to be a non-negative float."
);
}

/** @phpstan-ignore-next-line */
if ($refillAmount < 1) {
throw new InvalidArgumentException(
__CLASS__ ."::" . __FUNCTION__ . "(\$refillAmount) needs to be a positive integer."
);
}
$this->fill = $startFill ?? $this->size;
}

/** Get how much is currently in the bucket */
Expand All @@ -48,9 +82,19 @@ public function canTake(int $amount=1): bool {
* @param null|Closure $callback If set, a Closure to call back once there
* is enough in the bucket and it's our turn
*
* @phpstan-param positive-int $amount
* @phpstan-param null|Closure(): mixed $callback
*
* @throws Error
* @throws InvalidArgumentException
*/
public function take(int $amount=1, ?\Closure $callback=null): void {
/** @phpstan-ignore-next-line */
if ($amount < 1) {
throw new InvalidArgumentException(
__CLASS__ ."::take(\$amount) needs to be a positive integer."
);
}
if ($this->fill < $amount) {
$this->logger?->debug("Client wants {amount}, bucket has {fill}/{size}, waiting for refill", [
"amount" => $amount,
Expand Down Expand Up @@ -111,7 +155,7 @@ protected function put(int $refillAmount): void {
"fill" => $this->fill,
"size" => $this->size,
]);
if ($this->size === $this->fill && !count($this->queue)) {
if ($this->size <= $this->fill && !count($this->queue)) {
$this->stop();
return;
}
Expand Down

0 comments on commit a26c5c7

Please sign in to comment.