Skip to content

Commit

Permalink
fix: not prevent ipfs.io requests
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Feb 3, 2022
1 parent 05c5b96 commit ebc6f59
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
5 changes: 0 additions & 5 deletions packages/gateway/src/durable-objects/gateway-rate-limits.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@ const MINUTE = 1000 * 60
*/
function getRateLimitingCharacteristics(gatewayUrl) {
switch (gatewayUrl) {
case 'https://ipfs.io':
return {
RATE_LIMIT_REQUESTS: 800,
RATE_LIMIT_TIMEFRAME: MINUTE,
}
case 'https://cf-ipfs.com':
return {
RATE_LIMIT_REQUESTS: 100,
Expand Down
22 changes: 17 additions & 5 deletions packages/gateway/src/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,13 @@ async function gatewayFetch(
env,
{ pathname = '', timeout = 20000 } = {}
) {
// Block before hitting rate limit if needed
const { shouldBlock } = await getGatewayRateLimitState(request, env, gwUrl)

if (shouldBlock) {
// Block requests if needed
const requestPreventedCode = await shouldPreventRequest(request, env, gwUrl)
if (requestPreventedCode) {
/** @type {GatewayResponse} */
return {
url: gwUrl,
requestPreventedCode: REQUEST_PREVENTED_RATE_LIMIT_CODE,
requestPreventedCode,
}
}

Expand Down Expand Up @@ -188,6 +187,19 @@ async function gatewayFetch(
return gwResponse
}

async function shouldPreventRequest(request, env, gwUrl) {
// We don't need to prevent requests to ipfs.io gateway
if (gwUrl === 'https://ipfs.io') {
return undefined
}

// Block before hitting rate limit if needed
const { shouldBlock } = await getGatewayRateLimitState(request, env, gwUrl)
if (shouldBlock) {
return REQUEST_PREVENTED_RATE_LIMIT_CODE
}
}

/**
* @param {Request} request
*/
Expand Down

0 comments on commit ebc6f59

Please sign in to comment.