Skip to content

Commit

Permalink
Add Rate Limits to batch request itself (#1882)
Browse files Browse the repository at this point in the history
* Added Rate Limits to batch request itself

Signed-off-by: Alfredo Gutierrez <[email protected]>

* Fixing issue with getting total, getting it from config directly instead of method registry. (since is not a real endpoint)

Signed-off-by: Alfredo Gutierrez <[email protected]>

---------

Signed-off-by: Alfredo Gutierrez <[email protected]>
  • Loading branch information
AlfredoG87 authored Dec 14, 2023
1 parent 25965a2 commit 9817960
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/server/src/koaJsonRpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const METHOD_NOT_FOUND = 'METHOD NOT FOUND';
const REQUEST_ID_HEADER_NAME = 'X-Request-Id';

const responseSuccessStatusCode = '200';
const BATCH_REQUEST_METHOD_NAME = 'batch_request';

export default class KoaJsonRpc {
private registry: any;
Expand Down Expand Up @@ -217,8 +218,15 @@ export default class KoaJsonRpc {
return;
}

// verify rate limit for batch request
const batchRequestTotalLimit = this.methodConfig[BATCH_REQUEST_METHOD_NAME].total;
// check rate limit for method and ip
if (this.rateLimit.shouldRateLimit(ctx.ip, BATCH_REQUEST_METHOD_NAME, batchRequestTotalLimit, this.requestId)) {
return jsonResp(null, new IPRateLimitExceeded(BATCH_REQUEST_METHOD_NAME), undefined);
}

const response: any[] = [];
ctx.state.methodName = 'batch_requests';
ctx.state.methodName = BATCH_REQUEST_METHOD_NAME;

// we do the requests in parallel to save time, but we need to keep track of the order of the responses (since the id might be optional)
const promises = body.map((item: any) => this.getRequestResult(item, ctx.ip));
Expand Down
3 changes: 3 additions & 0 deletions packages/server/src/koaJsonRpc/lib/methodConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,7 @@ export const methodConfiguration = {
debug_traceTransaction: {
total: tier1rateLimit,
},
batch_request: {
total: tier1rateLimit,
},
};

0 comments on commit 9817960

Please sign in to comment.