rrnl-request-limiter-middleware
v1.3.0
Published
A RRNL middleware for limit number of request
Downloads
57
Maintainers
Readme
RRNL-request-limiter-middleware
A RRNL middleware for limit number of request
Why need this middleware?
Adding loose limits on the number of requests prevents applications from generating a large number of server requests in a short period of time, thus ultimately avoiding more serious problems
Despite the rigorous testing of our products, bugs that can't be reproduced are always there.
Despite our caching strategy. (with React-Relay
cache policy or cache middleware)
Inexperienced developers can easily make the following mistakes
const res = useLazyLoadQuery(query, {
startDateTime: moment().subtract(7, "day").format("l HH:mm:ss"), // last 7 day
});
Think about this: "If the request time is large then 1 second?"
The precision of the time parameter in variables is small, it is more likely to result in frequent requests.
How to use?
Install:
yarn add rrnl-request-limiter-middleware
// or npm install rrnl-request-limiter-middleware
Usage:
import {
createRequestLimiterMiddleware,
SlidingLogRateLimiter,
} from "rrnl-request-limiter-middleware";
const network = new RelayNetworkLayer([
// your other middleware
// ...
cacheMiddleware(),
createRequestLimiterMiddleware(
new SlidingLogRateLimiter([
{ duration: 60_000, limitTimes: 60 }, // Maximum 60 requests in 60 second for a query id
{ duration: 1_000, limitTimes: 3 }, // Maximum 3 requests in 1 second for a query id
]),
),
// your other middlewares
// ...
]);
Use with TokenBucketRateLimiter
:
import {
createRequestLimiterMiddleware,
TokenBucketRateLimiter,
} from "rrnl-request-limiter-middleware";
const network = new RelayNetworkLayer([
// your other middleware
// ...
cacheMiddleware(),
createRequestLimiterMiddleware(new TokenBucketRateLimiter(20, 1)),
// your other middlewares
// ...
]);
NOTE: Currently, TokenBucketRateLimiter
does not distinguish between queryId limits, in order to avoid overly timed tasks.
With wait
limit policy:
import {
createRequestLimiterMiddleware,
TokenBucketRateLimiter,
} from "rrnl-request-limiter-middleware";
const network = new RelayNetworkLayer([
// your other middleware
// ...
cacheMiddleware(),
createRequestLimiterMiddleware(new TokenBucketRateLimiter(20, 1), "wait"),
// your other middlewares
// ...
]);