Search by

hryvinskyi / magento2-rate-limit

hryvinskyi

Fixed-window rate limits for Magento 2: an allowance per bucket and subject kept in the cache, with the subject hashed, and a client key that groups an IPv6 /64 network.

Package info

github.com/hryvinskyi/magento2-rate-limit

Type:magento2-module

pkg:composer/hryvinskyi/magento2-rate-limit

Statistics

Installs: 35

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-11-30 18:52 UTC

This package is auto-updated.

Last update: 2026-11-30 20:06:37 UTC


README

Bounds how much of something a subject may do in a fixed window of time. A module asks the limiter for an allowance per bucket and subject, and the counters live in the platform's cache, so no table is needed. The subject (a client address, an order number) is hashed before it reaches the cache and is never stored, logged or shown.

What it does for a store

  • An allowance per bucket and subject. take() grants as many of a request as the subject has left in the current window and counts what it granted. hit() counts one and says whether it was within the limit. A limit of 0 is no limit.
  • Fixed windows. A window is the stretch of windowSeconds seconds the clock is in, counted from the epoch. A new window starts every subject of every bucket at zero.
  • Fails open. When the cache cannot be read or written, or the clock cannot tell the time, everything asked for is granted, and one warning for each bucket in the process names the bucket (never the subject). A limiter that failed closed would turn a cache outage into an outage of whatever it guards.
  • A client key for addresses. An IPv4 address keys as itself, an IPv4 address mapped into IPv6 as that IPv4 address, and an IPv6 address as its /64 network (2001:db8:1:2::/64), because one IPv6 subscriber is usually given a whole /64. Anything that is not an IP address keys as it is.
  • The clock comes from hryvinskyi/magento2-clock. The window is read from the UTC instant (UtcClockInterface), not from a store's clock, so it needs no store. The module keeps no clock of its own, so a test that sets the time of that package sets the window here too.

Requirements

  • Magento Open Source or Adobe Commerce, PHP 8.1 to 8.5 (the range composer.json allows).
  • hryvinskyi/magento2-clock ^1.0 and psr/log.

Installation

Run from the Magento root.

composer require hryvinskyi/magento2-rate-limit
bin/magento module:enable Hryvinskyi_Clock Hryvinskyi_RateLimit
bin/magento setup:upgrade
bin/magento setup:di:compile  # production mode only
bin/magento cache:flush

The module has no settings and creates no table. Nothing changes in the store until another module asks for a limit.

Using the limiter

Inject the two interfaces. A bucket is a lower-case letter followed by up to 31 lower-case letters, digits or underscores.

use Hryvinskyi\RateLimit\Api\ClientAddressKeyInterface;
use Hryvinskyi\RateLimit\Api\RateLimiterInterface;

public function __construct(
    private readonly RateLimiterInterface $limiter,
    private readonly ClientAddressKeyInterface $addressKeys
) {
}

// Ten reports a minute for each client: refuse the eleventh.
$allowed = $this->limiter->hit('report', $this->addressKeys->of($clientAddress), 10, 60);

// A batch of five against an allowance of four, two of which are used: two are granted.
$granted = $this->limiter->take('report', $subject, 5, 4, 60);

take() and hit() throw \InvalidArgumentException for a bucket outside its grammar, a negative request or limit, and a window shorter than one second.

Stored data

Where What
default cache frontend, key HRYVINSKYI_RATE_LIMIT_… One counter per bucket, window length, window and subject. The key holds a hash of the subject made with the installation's key, and the entry holds only the count. It lives until its window ends.
cache tag HRYVINSKYI_RATE_LIMIT Carried by every counter.

The counters go through the default cache frontend itself, not through a cache type, so switching a cache type off or cleaning one in the admin does not switch the limits off. A full flush (bin/magento cache:flush) removes the counters with everything else.

Limits

  • The count is approximate. Reading a counter, adding to it and saving it is not atomic, so two requests at the same moment can both read the same count, and a cache that evicts an entry forgets its count. That is enough to bound how fast a caller can make the store do work. It is not a ledger.
  • A fixed window lets a subject spend an allowance at the end of one window and again at the start of the next.
  • The limiter trusts the subject it is given. Behind a proxy that does not pass the client address on, every visitor has the proxy's address, and so shares one allowance. Give the caller the real client address.
  • Magento takes the client address from X-Forwarded-For when the header is present and no trusted proxy list is set, so a client can choose its own address and so its own allowance. For per-client limits to hold, the store strips or overwrites that header at its edge (the proxy or the web server).

Registrations

Where What
preference Hryvinskyi\RateLimit\Api\RateLimiterInterface Hryvinskyi\RateLimit\Model\CacheRateLimiter
preference Hryvinskyi\RateLimit\Api\ClientAddressKeyInterface Hryvinskyi\RateLimit\Model\ClientAddressKey

The module registers no route, command, cron job, event or plugin.

Extension point

Both interfaces are @api. A store that wants atomic counters in a shared store replaces the first preference in its own di.xml with a class that implements Hryvinskyi\RateLimit\Api\RateLimiterInterface; every consumer takes the new one.

<preference for="Hryvinskyi\RateLimit\Api\RateLimiterInterface" type="Vendor\Module\Model\AtomicRateLimiter"/>

Tests

  • Unit: vendor/bin/phpunit -c app/code/Hryvinskyi/RateLimit/phpunit.xml.dist, run from the Magento root.
  • Integration: the platform's harness in dev/tests/integration, one run at a time against its database. cd dev/tests/integration && php -d memory_limit=4G ../../../vendor/bin/phpunit -c phpunit.xml ../../../app/code/Hryvinskyi/RateLimit/Test/Integration.
  • Static analysis: PHPStan 2.x at level 10 with the package's phpstan.neon.dist (phpstan analyse -c app/code/Hryvinskyi/RateLimit/phpstan.neon.dist --level=10), and vendor/bin/phpcs --standard=app/code/Hryvinskyi/RateLimit/phpcs.xml.dist app/code/Hryvinskyi/RateLimit.