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: gateway get durable object request function #1264

Merged
merged 3 commits into from
Feb 8, 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
2 changes: 1 addition & 1 deletion packages/gateway/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export const REDIRECT_COUNTER_METRICS_ID = 'redirect-counter-metrics'
export const HTTP_STATUS_RATE_LIMITED = 429
export const HTTP_STATUS_SUCCESS = 200
export const REQUEST_PREVENTED_RATE_LIMIT_CODE = 'RATE_LIMIT'
export const ABORT_ERR_CODE = 'ABORT_ERR'
export const TIMEOUT_CODE = 'TIMEOUT'
34 changes: 25 additions & 9 deletions packages/gateway/src/gateway.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
/* eslint-env serviceworker, browser */
/* global Response caches */

import pAny from 'p-any'
import pAny, { AggregateError } from 'p-any'
import { FilterError } from 'p-some'
import pSettle from 'p-settle'

import { TimeoutError } from './errors.js'
import { getCidFromSubdomainUrl } from './utils/cid.js'
import {
ABORT_ERR_CODE,
CIDS_TRACKER_ID,
SUMMARY_METRICS_ID,
REDIRECT_COUNTER_METRICS_ID,
CF_CACHE_MAX_OBJECT_SIZE,
HTTP_STATUS_RATE_LIMITED,
REQUEST_PREVENTED_RATE_LIMIT_CODE,
TIMEOUT_CODE,
} from './constants.js'

/**
* @typedef {Object} GatewayResponse
* @property {Response} [response]
* @property {string} url
* @property {number} [responseTime]
* @property {string} [requestPreventedCode]
* @property {string} [reason]
* @property {boolean} [aborted]
*
* @typedef {import('./env').Env} Env
*/
Expand Down Expand Up @@ -105,7 +106,7 @@ export async function gatewayGet(request, env, ctx) {
const wasRateLimited = responses.every(
(r) =>
r.value?.response?.status === HTTP_STATUS_RATE_LIMITED ||
r.value?.requestPreventedCode === REQUEST_PREVENTED_RATE_LIMIT_CODE
r.value?.reason === REQUEST_PREVENTED_RATE_LIMIT_CODE
)

ctx.waitUntil(
Expand All @@ -126,7 +127,7 @@ export async function gatewayGet(request, env, ctx) {
}

// Return the error response from gateway, error is not from nft.storage Gateway
if (err instanceof FilterError) {
if (err instanceof FilterError || err instanceof AggregateError) {
const candidateResponse = responses.find((r) => r.value?.response)

// Return first response with upstream error
Expand All @@ -135,7 +136,10 @@ export async function gatewayGet(request, env, ctx) {
}

// Gateway timeout
if (responses[0].reason?.code === ABORT_ERR_CODE) {
if (
responses[0].value?.aborted &&
responses[0].value?.reason == TIMEOUT_CODE
) {
throw new TimeoutError()
}
}
Expand Down Expand Up @@ -183,7 +187,8 @@ async function gatewayFetch(
/** @type {GatewayResponse} */
return {
url: gwUrl,
requestPreventedCode: REQUEST_PREVENTED_RATE_LIMIT_CODE,
aborted: true,
reason: REQUEST_PREVENTED_RATE_LIMIT_CODE,
}
}

Expand All @@ -198,6 +203,15 @@ async function gatewayFetch(
signal: controller.signal,
headers: getHeaders(request),
})
} catch (error) {
if (controller.signal.aborted) {
return {
url: gwUrl,
aborted: true,
Copy link
Contributor

@alanshaw alanshaw Feb 8, 2022

Choose a reason for hiding this comment

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

Can we please be specific that this is a timeout (timeout: true), or have aborted + reason - where reason is timeout.

We could maybe roll requestPreventedCode into this:

{
  url: gwUrl,
  aborted: true,
  reason: REQUEST_PREVENTED_RATE_LIMIT_CODE
}

And for timeout:

{
  url: gwUrl,
  aborted: true,
  reason: 'TIMEOUT'
}

...for all other successful you set aborted: false.

Just trying to get a standard return type out of this.

reason: TIMEOUT_CODE,
}
}
throw error
} finally {
clearTimeout(timer)
}
Expand Down Expand Up @@ -317,7 +331,7 @@ async function updateGatewayMetrics(
status: gwResponse.response?.status,
winner: isWinner,
responseTime: gwResponse.responseTime,
requestPreventedCode: gwResponse.requestPreventedCode,
requestPreventedCode: gwResponse.reason,
}

await stub.fetch(getDurableRequestUrl(request, 'update', fetchStats))
Expand Down Expand Up @@ -352,7 +366,9 @@ async function updateCidsTracker(request, env, responses, cid) {
function getDurableRequestUrl(request, route, data) {
const reqUrl = new URL(
route,
request.url.startsWith('http') ? request.url : `http://${request.url}`
request.url.startsWith('http') || request.url.startsWith('https')
? request.url
: `https://${request.url}`
)
const headers = new Headers()
headers.append('Content-Type', 'application/json')
Expand Down