Merge pull request #11676 from overleaf/jpa-faster-access-check

[web] run all the user-content-domain-access-check probes in parallel

GitOrigin-RevId: 1b5a4756064053639e0dbc899d75ba8c0a5c5557
This commit is contained in:
Jakob Ackermann 2023-02-07 14:58:39 +00:00 committed by Copybot
parent 663aace586
commit ffc9b63ab1

View file

@ -21,7 +21,6 @@ const INITIAL_DELAY_MS = parseIntFromSplitTest(
'user-content-domain-access-check-delay',
30_000
)
const DELAY_BETWEEN_PROBES_MS = 1_000
const TIMEOUT_MS = 30_000
const FULL_SIZE = 739
const FULL_HASH =
@ -43,9 +42,6 @@ const CHUNKS = [
hash: '8278914487a3a099c9af5aa22ed836d6587ca0beb7bf9a059fb0409667b3eb3d',
},
]
async function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms))
}
function pickZone() {
const x = Math.random()
@ -198,30 +194,36 @@ export async function checkUserContentDomainAccess() {
}
let failed = 0
let ignoreResult = false
const epochBeforeCheck = networkEpoch
for (const { url, init, estimatedSize, hash, chunks } of cases) {
await sleep(DELAY_BETWEEN_PROBES_MS)
await Promise.all(
cases.map(async ({ url, init, estimatedSize, hash, chunks }) => {
try {
await singleCheck(url, init, estimatedSize, hash, chunks)
} catch (err: any) {
if (!navigator.onLine || epochBeforeCheck !== networkEpoch) {
// It is very likely that the request failed because we are offline or
// the network connection changed just now.
ignoreResult = true
}
if (ignoreResult) return
try {
await singleCheck(url, init, estimatedSize, hash, chunks)
} catch (err: any) {
if (!navigator.onLine || epochBeforeCheck !== networkEpoch) {
// It is very likely that the request failed because we are offline or
// the network connection changed just now.
return false
failed++
OError.tag(err, 'user-content-domain-access-check failed', {
url,
init,
})
if (
isSplitTestEnabled('report-user-content-domain-access-check-error')
) {
captureException(err)
} else {
console.error(OError.getFullStack(err), OError.getFullInfo(err))
}
}
failed++
OError.tag(err, 'user-content-domain-access-check failed', {
url,
init,
})
if (isSplitTestEnabled('report-user-content-domain-access-check-error')) {
captureException(err)
} else {
console.error(OError.getFullStack(err), OError.getFullInfo(err))
}
}
}
})
)
if (ignoreResult) return false
try {
await postJSON('/record-user-content-domain-access-check-result', {