Skip to content

Add custom period of blocking time #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4dcf52d
Add block period into annotation
sane4ek-2 Jun 27, 2018
e858246
Add an ability to get block period from config file
sane4ek-2 Jun 27, 2018
f70c74d
Add predis/predis package for development
sane4ek-2 Jun 27, 2018
b52562d
Add block status into RateLimitInfo
sane4ek-2 Jun 27, 2018
c39d49f
Add the key of rate limit into RateLimitInfo
sane4ek-2 Jun 27, 2018
8a7b7b2
Add setting of block into all available storages
sane4ek-2 Jun 27, 2018
ec310dd
Fix test
sane4ek-2 Jun 27, 2018
ed27380
Change set block method of mock storage
sane4ek-2 Jun 27, 2018
86d4a20
Add calling of setBlock method of storages into rate limit service
sane4ek-2 Jun 27, 2018
28de195
Add passing blocked value of rate limit from a storage into the object
sane4ek-2 Jun 27, 2018
6d8a4d9
Add an ability of setting custom period of time of blocking a call
sane4ek-2 Jun 28, 2018
a00ca57
Add a new event is dispatched after a block happened
sane4ek-2 Jun 28, 2018
4ba74d2
Add comment for generate key event
sane4ek-2 Jun 28, 2018
52b0549
Add firing block event when block happens
sane4ek-2 Jun 28, 2018
f589268
Add dispatching an event before sending a response
sane4ek-2 Jun 28, 2018
8896661
Add information about new feature block_period
sane4ek-2 Jun 28, 2018
dbea0dc
Rename constant of an event ratelimit.block.after
sane4ek-2 Jun 28, 2018
65dbbf5
Add documentation and examples about events
sane4ek-2 Jun 28, 2018
067dac3
Fix missing of setting key value into RateLimitInfo
sane4ek-2 Jun 28, 2018
a957440
Resolve conflicts between master and jivosite/custom-period-of-lock
sane4ek-2 Jul 3, 2018
bc6e1bb
Add block method into PsrCache storage
sane4ek-2 Jul 3, 2018
02b460d
Add block method into SimpleCache storage
sane4ek-2 Jul 3, 2018
59a7c85
Fix new tests
sane4ek-2 Jul 3, 2018
630b1d1
Fix tests
sane4ek-2 Jul 3, 2018
4b73007
Fix the last test
sane4ek-2 Jul 3, 2018
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 @@ -26,6 +26,11 @@ class RateLimit extends ConfigurationAnnotation
*/
protected $period = 3600;

/**
* @var int Amount of seconds when the calls aren't available
*/
protected $blockPeriod = 0;

/**
* Returns the alias name for an annotated configuration.
*
Expand Down Expand Up @@ -93,4 +98,20 @@ public function setPeriod($period)
{
$this->period = $period;
}

/**
* @return int
*/
public function getBlockPeriod()
{
return $this->blockPeriod;
}

/**
* @param int $blockPeriod
*/
public function setBlockPeriod($blockPeriod)
{
$this->blockPeriod = $blockPeriod;
}
}
4 changes: 4 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ public function getConfigTreeBuilder()
->isRequired()
->min(0)
->end()
->integerNode('block_period')
->defaultValue(0)
->min(0)
->end()
->end()
->end()
->end()
Expand Down
31 changes: 25 additions & 6 deletions EventListener/RateLimitAnnotationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Noxlogic\RateLimitBundle\EventListener;

use Noxlogic\RateLimitBundle\Annotation\RateLimit;
use Noxlogic\RateLimitBundle\Events\BlockEvent;
use Noxlogic\RateLimitBundle\Events\GenerateKeyEvent;
use Noxlogic\RateLimitBundle\Events\GetResponseEvent;
use Noxlogic\RateLimitBundle\Events\RateLimitEvents;
use Noxlogic\RateLimitBundle\Service\RateLimitService;
use Noxlogic\RateLimitBundle\Util\PathLimitProcessor;
Expand Down Expand Up @@ -40,6 +42,7 @@ public function __construct(
RateLimitService $rateLimitService,
PathLimitProcessor $pathLimitProcessor
) {
//todo:use an event dispatcher passed into onKernelController
$this->eventDispatcher = $eventDispatcher;
$this->rateLimitService = $rateLimitService;
$this->pathLimitProcessor = $pathLimitProcessor;
Expand Down Expand Up @@ -89,7 +92,7 @@ public function onKernelController(FilterControllerEvent $event)
$request->attributes->set('rate_limit_info', $rateLimitInfo);

// Reset the rate limits
if(time() >= $rateLimitInfo->getResetTimestamp()) {
if(!$rateLimitInfo->isBlocked() && time() >= $rateLimitInfo->getResetTimestamp()) {
$this->rateLimitService->resetRate($key);
$rateLimitInfo = $this->rateLimitService->createRate($key, $rateLimit->getLimit(), $rateLimit->getPeriod());
if (! $rateLimitInfo) {
Expand All @@ -100,19 +103,35 @@ public function onKernelController(FilterControllerEvent $event)
}

// When we exceeded our limit, return a custom error response
if ($rateLimitInfo->getCalls() > $rateLimitInfo->getLimit()) {
if (!$rateLimitInfo->isBlocked() && $rateLimitInfo->getCalls() > $rateLimitInfo->getLimit()) {
$this->rateLimitService->setBlock(
$rateLimitInfo,
$rateLimit->getBlockPeriod() > 0 ? $rateLimit->getBlockPeriod() : $rateLimit->getPeriod()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think If the block period isn't set, there should be no extended block period, as in how system works at the moment.

);
$this->eventDispatcher->dispatch(RateLimitEvents::BLOCK_AFTER, new BlockEvent($rateLimitInfo, $request));
}

if ($rateLimitInfo->isBlocked()) {
// Throw an exception if configured.
if ($this->getParameter('rate_response_exception')) {
$class = $this->getParameter('rate_response_exception');
throw new $class($this->getParameter('rate_response_message'), $this->getParameter('rate_response_code'));
}

$message = $this->getParameter('rate_response_message');
$code = $this->getParameter('rate_response_code');
$event->setController(function () use ($message, $code) {
$response = new Response(
$this->getParameter('rate_response_message'),
$this->getParameter('rate_response_code')
);

$eventResponse = new GetResponseEvent($request, $rateLimitInfo);
$this->eventDispatcher->dispatch(RateLimitEvents::RESPONSE_SENDING_BEFORE, $eventResponse);
if ($eventResponse->hasResponse()) {
$response = $eventResponse->getResponse();
}

$event->setController(function () use ($response) {
// @codeCoverageIgnoreStart
return new Response($message, $code);
return $response;
// @codeCoverageIgnoreEnd
});
$event->stopPropagation();
Expand Down
49 changes: 49 additions & 0 deletions Events/BlockEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Noxlogic\RateLimitBundle\Events;

use Noxlogic\RateLimitBundle\Service\RateLimitInfo;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\Request;

class BlockEvent extends Event
{
/**
* @var RateLimitInfo
*/
private $rateLimitInfo;

/**
* @var Request
*/
private $request;

/**
* BlockEvent constructor.
*
* @param RateLimitInfo $rateLimitInfo
* @param Request $request
*/
public function __construct(RateLimitInfo $rateLimitInfo, Request $request)
{
$this->rateLimitInfo = $rateLimitInfo;
$this->request = $request;
}


/**
* @return RateLimitInfo
*/
public function getRateLimitInfo()
{
return $this->rateLimitInfo;
}

/**
* @return Request
*/
public function getRequest()
{
return $this->request;
}
}
79 changes: 79 additions & 0 deletions Events/GetResponseEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Noxlogic\RateLimitBundle\Events;

use Noxlogic\RateLimitBundle\Service\RateLimitInfo;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class GetResponseEvent extends Event
{
/**
* @var Response
*/
private $response;

/**
* @var Request
*/
private $request;

/**
* @var RateLimitInfo
*/
private $rateLimitInfo;

/**
* GetResponseEvent constructor.
*
* @param Request $request
* @param RateLimitInfo $rateLimitInfo
*/
public function __construct(Request $request, RateLimitInfo $rateLimitInfo)
{
$this->request = $request;
$this->rateLimitInfo = $rateLimitInfo;
}


/**
* @return Response
*/
public function getResponse()
{
return $this->response;
}

/**
* @param Response $response
*/
public function setResponse(Response $response)
{
$this->response = $response;
}

/**
* @return bool
*/
public function hasResponse()
{
return null !== $this->response;
}

/**
* @return Request
*/
public function getRequest()
{
return $this->request;
}

/**
* @return RateLimitInfo
*/
public function getRateLimitInfo()
{
return $this->rateLimitInfo;
}
}
15 changes: 14 additions & 1 deletion Events/RateLimitEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,18 @@

final class RateLimitEvents
{
const GENERATE_KEY = 'ratelimit.generate.key';
/**
* This event is dispatched when generating a key is doing
*/
const GENERATE_KEY = 'ratelimit.generate.key';

/**
* This event is dispatched after a block happened
*/
const BLOCK_AFTER = 'ratelimit.block.after';

/**
* This event is dispatched before response is sent
*/
const RESPONSE_SENDING_BEFORE = 'ratelimit.response.sending.before';
}
Loading