Skip to content

Commit

Permalink
fix: address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Feb 3, 2022
1 parent 0f6d5e5 commit 7b0240a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
5 changes: 3 additions & 2 deletions packages/gateway/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export const CF_CACHE_MAX_OBJECT_SIZE = 512 * Math.pow(1024, 2) // 512MB to byte
export const METRICS_CACHE_MAX_AGE = 10 * 60 // in seconds (10 minutes)
export const CIDS_TRACKER_ID = 'cids'
export const SUMMARY_METRICS_ID = 'summary-metrics'
export const RATE_LIMIT_HTTP_ERROR_CODE = 429
export const HTTP_SUCCESS_CODE = 200
export const HTTP_STATUS_RATE_LIMITED = 429
export const HTTP_STATUS_SUCCESS = 200
export const REQUEST_PREVENTED_RATE_LIMIT_CODE = 'RATE_LIMIT'
2 changes: 1 addition & 1 deletion packages/gateway/src/durable-objects/gateway-metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @property {number} status http response status
* @property {number} [responseTime] number of milliseconds to get response
* @property {boolean} [winner] response was from winner gateway
* @property {number} [requestPreventedCode] request not sent to upstream gateway reason code
* @property {string} [requestPreventedCode] request not sent to upstream gateway reason code
*/

const GATEWAY_METRICS_ID = 'gateway_metrics'
Expand Down
25 changes: 13 additions & 12 deletions packages/gateway/src/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import {
CIDS_TRACKER_ID,
SUMMARY_METRICS_ID,
CF_CACHE_MAX_OBJECT_SIZE,
RATE_LIMIT_HTTP_ERROR_CODE,
HTTP_STATUS_RATE_LIMITED,
REQUEST_PREVENTED_RATE_LIMIT_CODE,
} from './constants.js'

/**
* @typedef {Object} GatewayResponse
* @property {Response} [response]
* @property {string} url
* @property {number} [responseTime]
* @property {number} [requestPreventedCode]
* @property {string} [requestPreventedCode]
*
* @typedef {import('./env').Env} Env
*/
Expand All @@ -44,7 +45,7 @@ export async function gatewayGet(request, env, ctx) {
const pathname = reqUrl.pathname

const gatewayReqs = env.ipfsGateways.map((gwUrl) =>
_gatewayFetch(gwUrl, cid, request, env, {
gatewayFetch(gwUrl, cid, request, env, {
pathname,
timeout: env.REQUEST_TIMEOUT,
})
Expand Down Expand Up @@ -112,7 +113,7 @@ export async function gatewayGet(request, env, ctx) {
// Redirect if all failed and at least one gateway was rate limited
if (responses) {
const wasRateLimited = responses.find(
(r) => r.value?.response?.status === RATE_LIMIT_HTTP_ERROR_CODE
(r) => r.value?.response?.status === HTTP_STATUS_RATE_LIMITED
)

if (wasRateLimited) {
Expand Down Expand Up @@ -150,7 +151,7 @@ async function storeWinnerGwResponse(request, env, winnerGwResponse) {
* @param {string} [options.pathname]
* @param {number} [options.timeout]
*/
async function _gatewayFetch(
async function gatewayFetch(
gwUrl,
cid,
request,
Expand All @@ -164,7 +165,7 @@ async function _gatewayFetch(
/** @type {GatewayResponse} */
return {
url: gwUrl,
requestPreventedCode: RATE_LIMIT_HTTP_ERROR_CODE,
requestPreventedCode: REQUEST_PREVENTED_RATE_LIMIT_CODE,
}
}

Expand Down Expand Up @@ -200,7 +201,7 @@ async function updateSummaryCacheMetrics(request, env) {
const id = env.summaryMetricsDurable.idFromName(SUMMARY_METRICS_ID)
const stub = env.summaryMetricsDurable.get(id)

await stub.fetch(_getDurableRequestUrl(request, 'metrics/cache'))
await stub.fetch(getDurableRequestUrl(request, 'metrics/cache'))
}

/**
Expand All @@ -214,7 +215,7 @@ async function getGatewayRateLimitState(request, env, gwUrl) {
const stub = env.gatewayRateLimitsDurable.get(id)

const stubResponse = await stub.fetch(
_getDurableRequestUrl(request, 'request')
getDurableRequestUrl(request, 'request')
)

/** @type {import('./durable-objects/gateway-rate-limits').RateLimitResponse} */
Expand All @@ -238,7 +239,7 @@ async function updateSummaryWinnerMetrics(request, env, gwResponse) {
responseTime: gwResponse.responseTime,
}

await stub.fetch(_getDurableRequestUrl(request, 'metrics/winner', fetchStats))
await stub.fetch(getDurableRequestUrl(request, 'metrics/winner', fetchStats))
}

/**
Expand All @@ -265,7 +266,7 @@ async function updateGatewayMetrics(
requestPreventedCode: gwResponse.requestPreventedCode,
}

await stub.fetch(_getDurableRequestUrl(request, 'update', fetchStats))
await stub.fetch(getDurableRequestUrl(request, 'update', fetchStats))
}

/**
Expand All @@ -284,7 +285,7 @@ async function updateCidsTracker(request, env, responses, cid) {
urls: responses.filter((r) => r.isFulfilled).map((r) => r?.value?.url),
}

await stub.fetch(_getDurableRequestUrl(request, 'update', updateRequest))
await stub.fetch(getDurableRequestUrl(request, 'update', updateRequest))
}

/**
Expand All @@ -294,7 +295,7 @@ async function updateCidsTracker(request, env, responses, cid) {
* @param {string} route
* @param {any} [data]
*/
function _getDurableRequestUrl(request, route, data) {
function getDurableRequestUrl(request, route, data) {
const reqUrl = new URL(
route,
request.url.startsWith('http') ? request.url : `http://${request.url}`
Expand Down
18 changes: 8 additions & 10 deletions packages/gateway/src/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import pMap from 'p-map'
import {
METRICS_CACHE_MAX_AGE,
SUMMARY_METRICS_ID,
HTTP_SUCCESS_CODE,
HTTP_STATUS_SUCCESS,
} from './constants.js'
import { histogram } from './durable-objects/gateway-metrics.js'

Expand Down Expand Up @@ -112,7 +112,7 @@ export async function metricsGet(request, env, ctx) {
env.ENV
}"} ${
metricsCollected.ipfsGateways[gw].totalResponsesByStatus[
HTTP_SUCCESS_CODE
HTTP_STATUS_SUCCESS
] || 0
}`
),
Expand All @@ -123,25 +123,23 @@ export async function metricsGet(request, env, ctx) {
`nftgateway_failed_requests_total{gateway="${gw}",env="${env.ENV}"} ${
totalResponsesPerGateway[gw] -
(metricsCollected.ipfsGateways[gw].totalResponsesByStatus[
HTTP_SUCCESS_CODE
HTTP_STATUS_SUCCESS
] || 0)
}`
),
`# HELP nftgateway_failed_requests_by_status_total Total failed requests by status code performed to each gateway.`,
`# TYPE nftgateway_failed_requests_by_status_total counter`,
`# HELP nftgateway_requests_by_status_total Total requests by status code performed to each gateway.`,
`# TYPE nftgateway_requests_by_status_total counter`,
...env.ipfsGateways
.map((gw) => {
return Object.keys(
metricsCollected.ipfsGateways[gw].totalResponsesByStatus
)
.filter(
(s) =>
s !== HTTP_SUCCESS_CODE.toString() &&
metricsCollected.ipfsGateways[gw].totalResponsesByStatus[s]
(s) => metricsCollected.ipfsGateways[gw].totalResponsesByStatus[s]
)
.map(
(status) =>
`nftgateway_failed_requests_by_status_total{gateway="${gw}",env="${
`nftgateway_requests_by_status_total{gateway="${gw}",env="${
env.ENV
}",status="${status}"} ${
metricsCollected.ipfsGateways[gw].totalResponsesByStatus[
Expand Down Expand Up @@ -199,7 +197,7 @@ export async function metricsGet(request, env, ctx) {
env.ENV
}"} ${
metricsCollected.ipfsGateways[gw].totalResponsesByStatus[
HTTP_SUCCESS_CODE
HTTP_STATUS_SUCCESS
] || 0
}`
),
Expand Down
6 changes: 3 additions & 3 deletions packages/gateway/test/cache.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ test.beforeEach((t) => {
}
})

test('Caches content', async (t) => {
// Miniflare cache sometimes is not yet setup...
test.skip('Caches content', async (t) => {
const url =
'https://bafkreidyeivj7adnnac6ljvzj2e3rd5xdw3revw4da7mx2ckrstapoupoq.ipfs.localhost:8787/'
const content = 'Hello nft.storage! 😎'
Expand All @@ -21,6 +22,5 @@ test('Caches content', async (t) => {
t.is(await response.text(), content)

const cachedRes = await caches.default.match(url)
// Miniflare cache sometimes is not yet setup...
cachedRes && t.is(await cachedRes.text(), content)
t.is(await cachedRes.text(), content)
})
2 changes: 1 addition & 1 deletion packages/gateway/test/rate-limit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ test('Receives should block when load reaches the RATE_LIMIT_REQUESTS', async (t

t.is(
metricsResponse.includes(
`_prevented_requests_by_reason_total{gateway="${gateways[0]}",env="test",reason="429"} 1`
`_prevented_requests_by_reason_total{gateway="${gateways[0]}",env="test",reason="RATE_LIMIT"} 1`
),
true
)
Expand Down

0 comments on commit 7b0240a

Please sign in to comment.