Skip to content

Commit 0827c2b

Browse files
authored
Add env variable to control rate limiting (#655)
* feat: add env var to control rate limiting Signed-off-by: Ivo Yankov <[email protected]> * chore: update helm chart values Signed-off-by: Ivo Yankov <[email protected]> * docs: add new env var to readme Signed-off-by: Ivo Yankov <[email protected]> Signed-off-by: Ivo Yankov <[email protected]>
1 parent 7863259 commit 0827c2b

File tree

8 files changed

+15
-1
lines changed

8 files changed

+15
-1
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ TIER_3_RATE_LIMIT =
1515
LIMIT_DURATION =
1616
HBAR_RATE_LIMIT_TINYBAR =
1717
HBAR_RATE_LIMIT_DURATION =
18+
RATE_LIMIT_DISABLED =
1819
ETH_GET_LOGS_BLOCK_RANGE_LIMIT =

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ TIER_3_RATE_LIMIT = 400
7979
LIMIT_DURATION = 60000
8080
HBAR_RATE_LIMIT_TINYBAR = 6000000000
8181
HBAR_RATE_LIMIT_DURATION = 60000
82+
RATE_LIMIT_DISABLED = false
8283
```
8384

8485
The following table highlights some initial configuration values to consider

docs/rate-limiting.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ TIER_1_RATE_LIMIT = 100;
1313
TIER_2_RATE_LIMIT = 800;
1414
TIER_3_RATE_LIMIT = 1600;
1515
LIMIT_DURATION = 60000;
16+
RATE_LIMIT_DISABLED = false;
1617
```
1718

1819
- **DEFAULT_RATE_LIMIT**: - default fallback rate limit, if no other is configured. Default is to `200` (200 request per IP).
1920
- **TIER_1_RATE_LIMIT**: - restrictive limiting tier, for expensive endpoints. Default is to `100` (100 request per IP).
2021
- **TIER_2_RATE_LIMIT**: - moderate limiting tier, for non expensive endpoints. Default is to `800` (800 request per IP).
2122
- **TIER_3_RATE_LIMIT**: - relaxed limiting tier. Default is to `1600` (1600 request per IP).
2223
- **LIMIT_DURATION**: - reset limit duration. This creates a timestamp, which resets all limits, when it's reached. Default is to `60000` (1 minute).
24+
- **RATE_LIMIT_DISABLED**: - if set to `true` no rate limiting will be performed.

helm-chart/templates/configmap.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ data:
2626
HBAR_RATE_LIMIT_TINYBAR: {{ .Values.config.HBAR_RATE_LIMIT_TINYBAR }}
2727
HBAR_RATE_LIMIT_DURATION: {{ .Values.config.HBAR_RATE_LIMIT_DURATION }}
2828
ETH_GET_LOGS_BLOCK_RANGE_LIMIT: {{ .Values.config.ETH_GET_LOGS_BLOCK_RANGE_LIMIT }}
29+
RATE_LIMIT_DISABLED: {{ .Values.config.RATE_LIMIT_DISABLED }}

helm-chart/templates/deployment.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ spec:
138138
name: {{ include "json-rpc-relay.fullname" . }}
139139
key: ETH_GET_LOGS_BLOCK_RANGE_LIMIT
140140
optional: true
141+
- name: RATE_LIMIT_DISABLED
142+
valueFrom:
143+
configMapKeyRef:
144+
name: {{ include "json-rpc-relay.fullname" . }}
145+
key: RATE_LIMIT_DISABLED
146+
optional: true
141147
ports:
142148
- containerPort: {{ .Values.ports.containerPort }}
143149
name: {{ .Values.ports.name }}

helm-chart/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ config:
110110
HBAR_RATE_LIMIT_TINYBAR: 5000000000
111111
HBAR_RATE_LIMIT_DURATION: 60000
112112
ETH_GET_LOGS_BLOCK_RANGE_LIMIT: 1000
113+
RATE_LIMIT_DISABLED: false
113114

114115
# Enable rolling_restarts if SDK calls fail this is usually due to stale connections that get cycled on restart
115116
rolling_restart:

packages/server/src/ratelimit/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export default class RateLimit {
4343
}
4444

4545
shouldRateLimit(ip: string, methodName: string, total: number): boolean {
46+
if (process.env.RATE_LIMIT_DISABLED && process.env.RATE_LIMIT_DISABLED === 'true') return false;
4647
this.precheck(ip, methodName, total);
4748
if (!this.shouldReset(ip)) {
4849
if (this.checkRemaining(ip, methodName)) {

packages/server/tests/localAcceptance.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ TIER_2_RATE_LIMIT = 800
2020
TIER_3_RATE_LIMIT = 1600
2121
LIMIT_DURATION = 60000
2222
HBAR_RATE_LIMIT_TINYBAR = 8000000000
23-
HBAR_RATE_LIMIT_DURATION = 60000
23+
HBAR_RATE_LIMIT_DURATION = 60000
24+
RATE_LIMIT_DISABLED=true

0 commit comments

Comments
 (0)