Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Annotation/RateLimit.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class RateLimit extends ConfigurationAnnotation
*/
protected $payload;

/**
* @var bool allow the ratelimiter to fail open on any request where an exception is thrown
*/
protected $failOpen = false;

/**
* Returns the alias name for an annotated configuration.
*
Expand Down Expand Up @@ -115,4 +120,20 @@ public function setPayload($payload)
$this->payload = $payload;
}

/**
* @return bool
*/
public function getFailOpen()
{
return $this->failOpen;
}

/**
* @param bool $failOpen
*/
public function setFailOpen($failOpen)
{
$this->failOpen = $failOpen;
}

}
13 changes: 11 additions & 2 deletions EventListener/RateLimitAnnotationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,17 @@ public function onKernelController(FilterControllerEvent $event)

$key = $this->getKey($event, $rateLimit, $annotations);

// Ratelimit the call
$rateLimitInfo = $this->rateLimitService->limitRate($key);
$rateLimitInfo = null;
// rate limit fails for any reason we can allow the endpoint to respond normally
try {
// Ratelimit the call
$rateLimitInfo = $this->rateLimitService->limitRate($key);
} catch (\Exception $exception) {
if ($rateLimit->getFailOpen()) {
return;
}
}

if (! $rateLimitInfo) {
// Create new rate limit entry for this call
$rateLimitInfo = $this->rateLimitService->createRate($key, $rateLimit->getLimit(), $rateLimit->getPeriod());
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
/**
* @Route(...)
*
* @RateLimit(limit=1000, period=3600)
* @RateLimit(limit=1000, period=3600, failOpen=true)
*/
public function someApiAction()
{
Expand Down