Skip to content

Commit

Permalink
fix: not prevent ipfs io requests (#1230)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored Feb 9, 2022
1 parent 7a5a72a commit 769a465
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
12 changes: 10 additions & 2 deletions packages/gateway/src/durable-objects/gateway-rate-limits.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Durable Object to keep track of gateway rating limits.
* State: number[]
*/
export class GatewayRateLimits0 {
export class GatewayRateLimits1 {
constructor(state) {
this.state = state
this.id = this.state.id.name
Expand All @@ -32,6 +32,14 @@ export class GatewayRateLimits0 {
let url = new URL(request.url)
switch (url.pathname) {
case '/request':
if (this.rateLimitCharacteristics.RATE_LIMIT_REQUESTS === Infinity) {
return new Response(
JSON.stringify({
shouldBlock: false,
})
)
}

// Filter out outdated requests
const now = Date.now()
this.rateLimitUsage = this.rateLimitUsage.filter(
Expand Down Expand Up @@ -78,7 +86,7 @@ function getRateLimitingCharacteristics(gatewayUrl) {
switch (gatewayUrl) {
case 'https://ipfs.io':
return {
RATE_LIMIT_REQUESTS: 800,
RATE_LIMIT_REQUESTS: Infinity,
RATE_LIMIT_TIMEFRAME: MINUTE,
}
case 'https://cf-ipfs.com':
Expand Down
2 changes: 1 addition & 1 deletion packages/gateway/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { metricsGet } from './metrics.js'
export { GatewayMetrics0 } from './durable-objects/gateway-metrics.js'
export { SummaryMetrics0 } from './durable-objects/summary-metrics.js'
export { CidsTracker0 } from './durable-objects/cids.js'
export { GatewayRateLimits0 } from './durable-objects/gateway-rate-limits.js'
export { GatewayRateLimits1 } from './durable-objects/gateway-rate-limits.js'
export { GatewayRedirectCounter0 } from './durable-objects/gateway-redirect-counter.js'

import { addCorsHeaders, withCorsHeaders } from './cors.js'
Expand Down
15 changes: 10 additions & 5 deletions packages/gateway/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ bindings = [
{name = "GATEWAYMETRICS", class_name = "GatewayMetrics0"},
{name = "SUMMARYMETRICS", class_name = "SummaryMetrics0"},
{name = "CIDSTRACKER", class_name = "CidsTracker0"},
{name = "GATEWAYRATELIMITS", class_name = "GatewayRateLimits0"},
{name = "GATEWAYRATELIMITS", class_name = "GatewayRateLimits1"},
{name = "GATEWAYREDIRECTCOUNTER", class_name = "GatewayRedirectCounter0"}
]

Expand All @@ -42,7 +42,7 @@ bindings = [
{name = "GATEWAYMETRICS", class_name = "GatewayMetrics0"},
{name = "SUMMARYMETRICS", class_name = "SummaryMetrics0"},
{name = "CIDSTRACKER", class_name = "CidsTracker0"},
{name = "GATEWAYRATELIMITS", class_name = "GatewayRateLimits0"},
{name = "GATEWAYRATELIMITS", class_name = "GatewayRateLimits1"},
{name = "GATEWAYREDIRECTCOUNTER", class_name = "GatewayRedirectCounter0"}
]

Expand All @@ -63,7 +63,7 @@ bindings = [
{name = "GATEWAYMETRICS", class_name = "GatewayMetrics0"},
{name = "SUMMARYMETRICS", class_name = "SummaryMetrics0"},
{name = "CIDSTRACKER", class_name = "CidsTracker0"},
{name = "GATEWAYRATELIMITS", class_name = "GatewayRateLimits0"},
{name = "GATEWAYRATELIMITS", class_name = "GatewayRateLimits1"},
{name = "GATEWAYREDIRECTCOUNTER", class_name = "GatewayRedirectCounter0"}
]

Expand All @@ -81,7 +81,7 @@ bindings = [
{name = "GATEWAYMETRICS", class_name = "GatewayMetrics0"},
{name = "SUMMARYMETRICS", class_name = "SummaryMetrics0"},
{name = "CIDSTRACKER", class_name = "CidsTracker0"},
{name = "GATEWAYRATELIMITS", class_name = "GatewayRateLimits0"},
{name = "GATEWAYRATELIMITS", class_name = "GatewayRateLimits1"},
{name = "GATEWAYREDIRECTCOUNTER", class_name = "GatewayRedirectCounter0"}
]

Expand All @@ -98,10 +98,15 @@ bindings = [
{name = "GATEWAYMETRICS", class_name = "GatewayMetrics0"},
{name = "SUMMARYMETRICS", class_name = "SummaryMetrics0"},
{name = "CIDSTRACKER", class_name = "CidsTracker0"},
{name = "GATEWAYRATELIMITS", class_name = "GatewayRateLimits0"},
{name = "GATEWAYRATELIMITS", class_name = "GatewayRateLimits1"},
{name = "GATEWAYREDIRECTCOUNTER", class_name = "GatewayRedirectCounter0"}
]

[[migrations]]
tag = "v1" # Should be unique for each entry
new_classes = ["GatewayMetrics0", "SummaryMetrics0", "CidsTracker0", "GatewayRateLimits0", "GatewayRedirectCounter0"]
[[migrations]]
tag = "v2" # Should be unique for each entry
renamed_classes = [
{from="GatewayRateLimits0", to="GatewayRateLimits1"}
]

0 comments on commit 769a465

Please sign in to comment.