Skip to content

Commit

Permalink
fix: error handling on race
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Oct 7, 2022
1 parent ff44d07 commit 9889753
Showing 1 changed file with 36 additions and 29 deletions.
65 changes: 36 additions & 29 deletions packages/edge-gateway/src/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,39 +214,46 @@ async function getFromDotstorage (request, cid, { pathname = '' } = {}) {
async function getFromGatewayRacer (cid, pathname, headers, env, ctx) {
const winnerUrlPromise = pDefer()
let response
let winnerResponseFetched = false

try {
// Trigger first tier resolution
response = await env.gwRacerL1.get(cid, {
pathname,
headers,
noAbortRequestsOnWinner: true,
onRaceEnd: async (gatewayResponsePromises, winnerGwResponse) => {
if (winnerGwResponse) {
winnerResponseFetched = true
winnerUrlPromise.resolve(winnerGwResponse.url)
ctx.waitUntil(
reportRaceResults(env, gatewayResponsePromises, winnerGwResponse.url)
)
} else {
ctx.waitUntil(
reportRaceResults(env, gatewayResponsePromises, undefined)
)
}
}
})
if (!winnerResponseFetched) {
throw new Error('no winner in the first race')
}
} catch (err) {
// Trigger second tier resolution
response = await env.gwRacerL2.get(cid, {
pathname,
headers,
noAbortRequestsOnWinner: true,
onRaceEnd: async (gatewayResponsePromises, winnerGwResponse) => {
winnerUrlPromise.resolve(winnerGwResponse?.url)

// Trigger first tier resolution
response = await env.gwRacerL1.get(cid, {
pathname,
headers,
noAbortRequestsOnWinner: true,
onRaceEnd: async (gatewayResponsePromises, winnerGwResponse) => {
if (winnerGwResponse) {
winnerUrlPromise.resolve(winnerGwResponse.url)
ctx.waitUntil(
reportRaceResults(env, gatewayResponsePromises, winnerGwResponse.url)
)
} else {
ctx.waitUntil(
reportRaceResults(env, gatewayResponsePromises, undefined)
reportRaceResults(env, gatewayResponsePromises, winnerGwResponse?.url)
)

// Trigger second tier resolution
response = await env.gwRacerL2.get(cid, {
pathname,
headers,
noAbortRequestsOnWinner: true,
onRaceEnd: async (gatewayResponsePromises, winnerGwResponse) => {
winnerUrlPromise.resolve(winnerGwResponse?.url)

ctx.waitUntil(
reportRaceResults(env, gatewayResponsePromises, winnerGwResponse?.url)
)
}
})
}
}
})
})
}

const url = await winnerUrlPromise.promise

Expand Down

0 comments on commit 9889753

Please sign in to comment.