Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: not prevent ipfs io requests #1230

Merged
merged 4 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just make this Infinity?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but I removed it entirely from calling this function to not have the delay of an async http request to the durable. Also, keeping infinity array size of timestamps might hit Limits of durable object

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense...but...personally I would rather special case "Infinity" as a "limit" than a specific gateway. Maybe in the future we'll have other gateways that do not limit, if that happens we'll have to change code somewhere else than here and I'd rather just have a single place where we define the available gateways and their characteristics rather than scattered.

Unless the additional async request is problematic, I'd keep it there. It also makes the race a bit fairer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough

Copy link
Contributor Author

@vasco-santos vasco-santos Feb 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made 10 seconds the time to track, so that we do not have a really big array to track

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"}
]