Skip to content

fix: retry IPNI queries on 5xx errors #126

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

Merged
merged 5 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ export {
HttpChainClient,
HttpCachingChain
} from 'https://cdn.jsdelivr.net/npm/drand-client@1.2.6/index.js/+esm'

export { assertOkResponse } from 'https://cdn.skypack.dev/assert-ok-response@1.0.0/?dts'
import pRetry from 'https://cdn.skypack.dev/p-retry@6.2.1/?dts'
export { pRetry }
2 changes: 1 addition & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export const MAX_JITTER_BETWEEN_TASKS_IN_MS = 10_000 // 10 seconds
export const RPC_URL = 'https://api.node.glif.io/'
export const RPC_AUTH = 'KZLIUb9ejreYOm-mZFM3UNADE0ux6CrHjxnS2D2Qgb8='
export const MINER_TO_PEERID_CONTRACT_ADDRESS = '0x14183aD016Ddc83D638425D6328009aa390339Ce' // Contract address on the Filecoin EVM
export const MAX_REQUEST_DURATION_MS = 90_000
export const MAX_REQUEST_DURATION_MS = 90_000
17 changes: 1 addition & 16 deletions lib/http-assertions.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import { AssertionError } from 'zinnia:assert'

/**
* @param {Response} res
* @param {string} [errorMsg]
*/
export async function assertOkResponse (res, errorMsg) {
if (res.ok) return

let body
try {
body = await res.text()
} catch {}
const err = new Error(`${errorMsg ?? 'Fetch failed'} (${res.status}): ${body?.trimEnd()}`)
err.statusCode = res.status
err.serverMessage = body
throw err
}
export { assertOkResponse } from '../vendor/deno-deps.js'

/**
* @param {Response} res
Expand Down
34 changes: 22 additions & 12 deletions lib/ipni-client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { decodeBase64, decodeVarint } from '../vendor/deno-deps.js'
import { decodeBase64, decodeVarint, pRetry, assertOkResponse } from '../vendor/deno-deps.js'

/**
*
Expand All @@ -10,22 +10,23 @@ import { decodeBase64, decodeVarint } from '../vendor/deno-deps.js'
* }>}
*/
export async function queryTheIndex (cid, providerId) {
const url = `https://cid.contact/cid/${encodeURIComponent(cid)}`

let providerResults
try {
const res = await fetch(url)
if (!res.ok) {
console.error('IPNI query failed, HTTP response: %s %s', res.status, (await res.text()).trimEnd())
return { indexerResult: `ERROR_${res.status}` }
}

const result = await res.json()
providerResults = result.MultihashResults.flatMap(r => r.ProviderResults)
providerResults = await pRetry(() => getRetrievalProviders(cid), {
retries: 5,
shouldRetry: (error) => {
return error.statusCode && error.statusCode >= 500
},
onFailedAttempt: (error) => {
console.error('IPNI query failed, retrying...\n', error)
}
})
console.log('IPNI returned %s provider results', providerResults.length)
} catch (err) {
console.error('IPNI query failed.', err)
return { indexerResult: 'ERROR_FETCH' }
return {
indexerResult: typeof err.statusCode === 'number' ? `ERROR_${err.statusCode}` : 'ERROR_FETCH'
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
indexerResult: typeof err.statusCode === 'number' ? `ERROR_${err.statusCode}` : 'ERROR_FETCH'
indexerResult: typeof err.status === 'number' ? `ERROR_${err.status}` : 'ERROR_FETCH'

https://developer.mozilla.org/en-US/docs/Web/API/Response/status

Copy link
Member

Choose a reason for hiding this comment

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

See also above

Copy link
Member

Choose a reason for hiding this comment

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

Oh I see, this is the error from assertOkResponse, not fetch. Nevermind

}
}

let graphsyncProvider
Expand Down Expand Up @@ -70,3 +71,12 @@ export async function queryTheIndex (cid, providerId) {
console.log('All advertisements are from other miners or for unsupported protocols.')
return { indexerResult: 'NO_VALID_ADVERTISEMENT' }
}

async function getRetrievalProviders (cid) {
const url = `https://cid.contact/cid/${encodeURIComponent(cid)}`
const res = await fetch(url)
await assertOkResponse(res)

const result = await res.json()
return result.MultihashResults.flatMap((r) => r.ProviderResults)
}
4 changes: 2 additions & 2 deletions lib/spark.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export default class Spark {
const { signal } = controller

let requestIdleTimeout
let maxDurationTimeout

const resetTimeout = () => {
if (requestIdleTimeout) {
Expand All @@ -100,7 +99,8 @@ export default class Spark {
controller.abort()
}, 60_000)
}
maxDurationTimeout = setTimeout(() => {

const maxDurationTimeout = setTimeout(() => {
Copy link
Member

Choose a reason for hiding this comment

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

thanks for this clean up!

Copy link
Member Author

Choose a reason for hiding this comment

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

You are welcome. It was reported by npx standard. I am surprised this passed our CI checks 🤔

Copy link
Member

Choose a reason for hiding this comment

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

stats.timeout = true
controller.abort()
}, maxRequestDurationMs)
Expand Down
Loading