-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RuntimeConfigurableRateLimiter (#57)
- Loading branch information
1 parent
fa5d32f
commit 84bc08a
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RateLimit; | ||
|
||
use LogicException; | ||
|
||
final class RuntimeConfigurableRateLimiter extends ConfigurableRateLimiter implements RateLimiter, SilentRateLimiter | ||
{ | ||
public function __construct(private RateLimiter|SilentRateLimiter $rateLimiter) | ||
{ | ||
parent::__construct(Rate::perSecond(1)); | ||
} | ||
|
||
public function limit(string $identifier, Rate $rate = null): void | ||
{ | ||
if (!$this->rateLimiter instanceof RateLimiter) { | ||
throw new LogicException('Decorated Rate Limiter must implement RateLimiter interface'); | ||
} | ||
|
||
if ($this->rateLimiter instanceof ConfigurableRateLimiter) { | ||
$this->rateLimiter->rate = $rate; | ||
} | ||
|
||
$this->rateLimiter->limit($identifier); | ||
} | ||
|
||
public function limitSilently(string $identifier, Rate $rate = null): Status | ||
{ | ||
if (!$this->rateLimiter instanceof SilentRateLimiter) { | ||
throw new LogicException('Decorated Rate Limiter must implement SilentRateLimiter interface'); | ||
} | ||
|
||
if ($this->rateLimiter instanceof ConfigurableRateLimiter) { | ||
$this->rateLimiter->rate = $rate; | ||
} | ||
|
||
return $this->rateLimiter->limitSilently($identifier); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RateLimit\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use RateLimit\Exception\LimitExceeded; | ||
use RateLimit\InMemoryRateLimiter; | ||
use RateLimit\Rate; | ||
use RateLimit\RuntimeConfigurableRateLimiter; | ||
|
||
class RuntimeConfigurableRateLimiterTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function it_allows_rate_to_be_configured_at_runtime(): void | ||
{ | ||
$rate = Rate::perHour(1); | ||
$rateLimiter = new RuntimeConfigurableRateLimiter(new InMemoryRateLimiter(Rate::perMinute(100))); | ||
$identifier = 'test'; | ||
|
||
$rateLimiter->limitSilently($identifier, $rate); | ||
|
||
try { | ||
$rateLimiter->limit($identifier, $rate); | ||
|
||
$this->fail('Limit should have been reached'); | ||
} catch (LimitExceeded $exception) { | ||
$this->assertSame($identifier, $exception->getIdentifier()); | ||
$this->assertSame($rate, $exception->getRate()); | ||
} | ||
} | ||
} |