Skip to content

Commit 2045548

Browse files
committed
AC-1271: Add rate limiting for payment information endpoint and mutation
1 parent f1f4cc4 commit 2045548

10 files changed

+43
-44
lines changed

lib/internal/Magento/Framework/App/Backpressure/BackpressureExceededException.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,8 @@
99
namespace Magento\Framework\App\Backpressure;
1010

1111
/**
12-
* Thrown when backpressure is exceeded.
12+
* Thrown when backpressure is exceeded
1313
*/
1414
class BackpressureExceededException extends \RuntimeException
1515
{
16-
/**
17-
* @param \Throwable|null $prev
18-
* @param string $message
19-
* @param int $code
20-
*/
21-
public function __construct(?\Throwable $prev = null, string $message = 'Backpressure exceeded', int $code = 0)
22-
{
23-
parent::__construct($message, $code, $prev);
24-
}
2516
}

lib/internal/Magento/Framework/App/Backpressure/ContextInterface.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Magento\Framework\App\RequestInterface;
1212

1313
/**
14-
* Request context.
14+
* Request context
1515
*/
1616
interface ContextInterface
1717
{
@@ -22,30 +22,30 @@ interface ContextInterface
2222
public const IDENTITY_TYPE_ADMIN = 2;
2323

2424
/**
25-
* Current request.
25+
* Current request
2626
*
2727
* @return RequestInterface
2828
*/
2929
public function getRequest(): RequestInterface;
3030

3131
/**
32-
* Unique ID for request issuer.
32+
* Unique ID for request issuer
3333
*
3434
* @return string
3535
*/
3636
public function getIdentity(): string;
3737

3838
/**
39-
* Type of identity detected.
39+
* Type of identity detected
4040
*
4141
* @return int
4242
*/
4343
public function getIdentityType(): int;
4444

4545
/**
46-
* Request type ID.
46+
* Request type ID
4747
*
48-
* String ID of the functionality that requires backpressure enforcement.
48+
* String ID of the functionality that requires backpressure enforcement
4949
*
5050
* @return string
5151
*/

lib/internal/Magento/Framework/App/Backpressure/IdentityProviderInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
interface IdentityProviderInterface
1515
{
1616
/**
17-
* One of ContextInterface constants.
17+
* One of ContextInterface constants
1818
*
1919
* @return int
2020
*/
2121
public function fetchIdentityType(): int;
2222

2323
/**
24-
* Identity string representation.
24+
* Identity string representation
2525
*
2626
* @return string
2727
*/

lib/internal/Magento/Framework/App/Backpressure/SlidingWindow/CacheRequestLogger.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Magento\Framework\Cache\FrontendInterface;
1414

1515
/**
16-
* Logging requests to cache.
16+
* Logging requests to cache
1717
*/
1818
class CacheRequestLogger implements RequestLoggerInterface
1919
{
@@ -43,13 +43,13 @@ public function incrAndGetFor(ContextInterface $context, int $timeSlot, int $dis
4343
$cache = $this->cache->getBackend();
4444

4545
return $cache->updateByAndGet($id, 1, time() + $discardAfter);
46-
} else {
47-
//Non-atomic way
48-
$n = (int) ($this->cache->load($id) ?? 0);
49-
$this->cache->save((string) ++$n, $id, [], $discardAfter);
50-
51-
return $n;
5246
}
47+
48+
//Non-atomic way
49+
$n = (int) ($this->cache->load($id) ?? 0);
50+
$this->cache->save((string) ++$n, $id, [], $discardAfter);
51+
52+
return $n;
5353
}
5454

5555
/**

lib/internal/Magento/Framework/App/Backpressure/SlidingWindow/CompositeLimitConfigManager.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
namespace Magento\Framework\App\Backpressure\SlidingWindow;
1010

1111
use Magento\Framework\App\Backpressure\ContextInterface;
12+
use Magento\Framework\Exception\RuntimeException;
1213

1314
/**
14-
* Delegates finding configs for different requests types to other instances.
15+
* Delegates finding configs for different requests types to other instances
1516
*/
1617
class CompositeLimitConfigManager implements LimitConfigManagerInterface
1718
{
@@ -29,14 +30,21 @@ public function __construct(array $configs)
2930
}
3031

3132
/**
32-
* @inheritDoc
33+
* {@inheritDoc}
34+
*
35+
* @throws RuntimeException
3336
*/
3437
public function readLimit(ContextInterface $context): LimitConfig
3538
{
36-
if (!array_key_exists($context->getTypeId(), $this->configs)) {
37-
throw new \RuntimeException(sprintf('Failed to find config manager for "%s".', $context->getTypeId()));
39+
if (isset($this->configs[$context->getTypeId()])) {
40+
return $this->configs[$context->getTypeId()]->readLimit($context);
3841
}
3942

40-
return $this->configs[$context->getTypeId()]->readLimit($context);
43+
throw new RuntimeException(
44+
__(
45+
'Failed to find config manager for "%typeId".',
46+
[ 'typeId' => $context->getTypeId()]
47+
)
48+
);
4149
}
4250
}

lib/internal/Magento/Framework/App/Backpressure/SlidingWindow/LimitConfig.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Magento\Framework\App\Backpressure\SlidingWindow;
1010

1111
/**
12-
* Limit configuration.
12+
* Limit configuration
1313
*/
1414
class LimitConfig
1515
{
@@ -34,7 +34,7 @@ public function __construct(int $limit, int $period)
3434
}
3535

3636
/**
37-
* Requests per period.
37+
* Requests per period
3838
*
3939
* @return int
4040
*/
@@ -44,7 +44,7 @@ public function getLimit(): int
4444
}
4545

4646
/**
47-
* Period in seconds.
47+
* Period in seconds
4848
*
4949
* @return int
5050
*/

lib/internal/Magento/Framework/App/Backpressure/SlidingWindow/LimitConfigManagerInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
use Magento\Framework\App\Backpressure\ContextInterface;
1212

1313
/**
14-
* Provides limit configuration for request contexts.
14+
* Provides limit configuration for request contexts
1515
*/
1616
interface LimitConfigManagerInterface
1717
{
1818
/**
19-
* Find limits for given context.
19+
* Find limits for given context
2020
*
2121
* @param ContextInterface $context
2222
* @return LimitConfig

lib/internal/Magento/Framework/App/Backpressure/SlidingWindow/RequestLoggerInterface.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@
1111
use Magento\Framework\App\Backpressure\ContextInterface;
1212

1313
/**
14-
* Logs requests.
14+
* Logs requests
1515
*/
1616
interface RequestLoggerInterface
1717
{
1818
/**
19-
* Increase counter for requests coming inside given timeslot from given identity.
19+
* Increase counter for requests coming inside given timeslot from given identity
2020
*
2121
* @param ContextInterface $context
22-
* @param int $timeSlot Time slot to increase the counter for (timestamp).
23-
* @param int $discardAfter Counter for the time slot can be discarded after given number of seconds.
24-
* @return int Requests logged for the identity and the time slot.
22+
* @param int $timeSlot Time slot to increase the counter for (timestamp)
23+
* @param int $discardAfter Counter for the time slot can be discarded after given number of seconds
24+
* @return int Requests logged for the identity and the time slot
2525
*/
2626
public function incrAndGetFor(ContextInterface $context, int $timeSlot, int $discardAfter): int;
2727

2828
/**
29-
* Get counter for specific identity and time slot.
29+
* Get counter for specific identity and time slot
3030
*
3131
* @param ContextInterface $context
3232
* @param int $timeSlot

lib/internal/Magento/Framework/App/Backpressure/SlidingWindow/SlidingWindowEnforcer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Magento\Framework\Stdlib\DateTime\DateTime;
1515

1616
/**
17-
* Uses Sliding Window approach to record request times and enforce limits.
17+
* Uses Sliding Window approach to record request times and enforce limits
1818
*/
1919
class SlidingWindowEnforcer implements BackpressureEnforcerInterface
2020
{

lib/internal/Magento/Framework/App/BackpressureEnforcerInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
use Magento\Framework\App\Backpressure\ContextInterface;
1313

1414
/**
15-
* Enforces certain backpressure.
15+
* Enforces certain backpressure
1616
*/
1717
interface BackpressureEnforcerInterface
1818
{
1919
/**
20-
* Enforce the backpressure by throwing the exception when limit exceeded.
20+
* Enforce the backpressure by throwing the exception when limit exceeded
2121
*
2222
* @param ContextInterface $context
2323
* @throws BackpressureExceededException

0 commit comments

Comments
 (0)