vomebook commited on
Commit ·
a4e9ebc
1
Parent(s): c6dfdc2
Unify PDF network recovery eligibility and throttle state
Browse files
scripts/complexity-budget.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
"version": 1,
|
| 3 |
"scope": "site",
|
| 4 |
"limit": 12,
|
| 5 |
-
"branches":
|
| 6 |
"files": {
|
| 7 |
"backend/app.py": {
|
| 8 |
"peak": 19,
|
|
@@ -270,7 +270,7 @@
|
|
| 270 |
},
|
| 271 |
"static/reader/pdf/reader-pdf-network.mjs": {
|
| 272 |
"peak": 48,
|
| 273 |
-
"excess":
|
| 274 |
},
|
| 275 |
"static/reader/pdf/reader-pdf-page-window.mjs": {
|
| 276 |
"peak": 64,
|
|
|
|
| 2 |
"version": 1,
|
| 3 |
"scope": "site",
|
| 4 |
"limit": 12,
|
| 5 |
+
"branches": 17550,
|
| 6 |
"files": {
|
| 7 |
"backend/app.py": {
|
| 8 |
"peak": 19,
|
|
|
|
| 270 |
},
|
| 271 |
"static/reader/pdf/reader-pdf-network.mjs": {
|
| 272 |
"peak": 48,
|
| 273 |
+
"excess": 153
|
| 274 |
},
|
| 275 |
"static/reader/pdf/reader-pdf-page-window.mjs": {
|
| 276 |
"peak": 64,
|
static/reader/pdf/reader-pdf-network.mjs
CHANGED
|
@@ -632,7 +632,7 @@ export function createPdfFetchPolicy({idleMs = 15000, attemptMs = 45000,
|
|
| 632 |
// Shared per-document admission activates only after HTTP 429. The 100ms
|
| 633 |
// recovery spacing is a conservative retry policy, not a claimed origin
|
| 634 |
// limit; normal traffic is unaffected and all waiters remain cancellable.
|
| 635 |
-
let throttled = false, throttleUntil = 0, nextRateStart = 0,
|
| 636 |
/** @type {Map<string, number>} */
|
| 637 |
const blockedUntil = new Map();
|
| 638 |
/** @type {(Bounds & {url: string | null})[]} */
|
|
@@ -685,17 +685,16 @@ export function createPdfFetchPolicy({idleMs = 15000, attemptMs = 45000,
|
|
| 685 |
return false;
|
| 686 |
}
|
| 687 |
throttled = true;
|
| 688 |
-
recovering = true;
|
| 689 |
throttleUntil = Math.max(throttleUntil, now() + Math.max(delay,rateLimitDelayMs));
|
| 690 |
return true;
|
| 691 |
};
|
| 692 |
/** @param {number} generation */
|
| 693 |
const noteRateSuccess = generation => {
|
| 694 |
-
if (!
|
| 695 |
clearTimeout(recoveryIdle);
|
| 696 |
recoveryIdle = setTimeout(() => {
|
| 697 |
if (now() >= throttleUntil && normalRecoveryAdmissions > 0 && !rateAdmissions.length) {
|
| 698 |
-
throttled = false;
|
| 699 |
}
|
| 700 |
},rateLimitRecoveryMs);
|
| 701 |
};
|
|
@@ -780,6 +779,7 @@ export function createPdfFetchPolicy({idleMs = 15000, attemptMs = 45000,
|
|
| 780 |
}
|
| 781 |
};
|
| 782 |
const initialRecovery = method === "GET" && !headers.has("Range") && !conditional;
|
|
|
|
| 783 |
const initialAt = performance.now();
|
| 784 |
const admissionRemaining = () => Math.max(0, (initialRecovery ? initialTotalMs : attemptMs * 2) -
|
| 785 |
(performance.now()-initialAt));
|
|
@@ -838,7 +838,7 @@ export function createPdfFetchPolicy({idleMs = 15000, attemptMs = 45000,
|
|
| 838 |
}
|
| 839 |
async function open() {
|
| 840 |
signal.throwIfAborted();
|
| 841 |
-
if (
|
| 842 |
const remaining=admissionRemaining();
|
| 843 |
if (!remaining) throw new DOMException("PDF recovery deadline elapsed", "TimeoutError");
|
| 844 |
const admissionSignal=AbortSignal.any([signal,AbortSignal.timeout(Math.ceil(remaining))]);
|
|
@@ -851,7 +851,7 @@ export function createPdfFetchPolicy({idleMs = 15000, attemptMs = 45000,
|
|
| 851 |
const owner = new AbortController();
|
| 852 |
record = {owner, active:AbortSignal.any([signal,owner.signal]), generation:rateGeneration,
|
| 853 |
row:{at:performance.now(),attempt,range:match?[start,end]:null,headersMs:0,bodyMs:0,bytes:0}};
|
| 854 |
-
if (
|
| 855 |
const received = await abortable(Promise.resolve(nativeFetch.call(globalThis,input,{...init,cache:"no-store",signal:record.active}))
|
| 856 |
.then(value => {
|
| 857 |
if (owner.signal.aborted || signal.aborted) {value.body?.cancel().catch(() => {});throw record.active.reason;}
|
|
@@ -861,17 +861,19 @@ export function createPdfFetchPolicy({idleMs = 15000, attemptMs = 45000,
|
|
| 861 |
record.row.headersMs = performance.now()-record.row.at;
|
| 862 |
record.row.status = received.status;
|
| 863 |
record.row.serverTiming = (received.headers.get("Server-Timing") || "").slice(0,512);
|
| 864 |
-
if (
|
| 865 |
-
|
| 866 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 867 |
}
|
| 868 |
-
if ((eligible || initialRecovery) && [408,500,502,503,504].includes(received.status))
|
| 869 |
-
throw Object.assign(new Error("PDF temporary upstream failure"),{retryable:true});
|
| 870 |
// PDF.js needs headers immediately to cancel the full GET and demand ranges.
|
| 871 |
// Initial recovery must not read or wait for this potentially large body.
|
| 872 |
if (received.ok) {
|
| 873 |
remember(received,url);
|
| 874 |
-
if (
|
| 875 |
}
|
| 876 |
if (!eligible || received.status !== 206) {
|
| 877 |
if (delivered) throw new Error("PDF_RANGE_INVALID");
|
|
|
|
| 632 |
// Shared per-document admission activates only after HTTP 429. The 100ms
|
| 633 |
// recovery spacing is a conservative retry policy, not a claimed origin
|
| 634 |
// limit; normal traffic is unaffected and all waiters remain cancellable.
|
| 635 |
+
let throttled = false, throttleUntil = 0, nextRateStart = 0, rateGeneration = 0;
|
| 636 |
/** @type {Map<string, number>} */
|
| 637 |
const blockedUntil = new Map();
|
| 638 |
/** @type {(Bounds & {url: string | null})[]} */
|
|
|
|
| 685 |
return false;
|
| 686 |
}
|
| 687 |
throttled = true;
|
|
|
|
| 688 |
throttleUntil = Math.max(throttleUntil, now() + Math.max(delay,rateLimitDelayMs));
|
| 689 |
return true;
|
| 690 |
};
|
| 691 |
/** @param {number} generation */
|
| 692 |
const noteRateSuccess = generation => {
|
| 693 |
+
if (!throttled || generation !== rateGeneration) return;
|
| 694 |
clearTimeout(recoveryIdle);
|
| 695 |
recoveryIdle = setTimeout(() => {
|
| 696 |
if (now() >= throttleUntil && normalRecoveryAdmissions > 0 && !rateAdmissions.length) {
|
| 697 |
+
throttled = false;clearTimeout(rateTimer);rateTimer=0;
|
| 698 |
}
|
| 699 |
},rateLimitRecoveryMs);
|
| 700 |
};
|
|
|
|
| 779 |
}
|
| 780 |
};
|
| 781 |
const initialRecovery = method === "GET" && !headers.has("Range") && !conditional;
|
| 782 |
+
const recoveryEligible = eligible || initialRecovery;
|
| 783 |
const initialAt = performance.now();
|
| 784 |
const admissionRemaining = () => Math.max(0, (initialRecovery ? initialTotalMs : attemptMs * 2) -
|
| 785 |
(performance.now()-initialAt));
|
|
|
|
| 838 |
}
|
| 839 |
async function open() {
|
| 840 |
signal.throwIfAborted();
|
| 841 |
+
if (recoveryEligible) {
|
| 842 |
const remaining=admissionRemaining();
|
| 843 |
if (!remaining) throw new DOMException("PDF recovery deadline elapsed", "TimeoutError");
|
| 844 |
const admissionSignal=AbortSignal.any([signal,AbortSignal.timeout(Math.ceil(remaining))]);
|
|
|
|
| 851 |
const owner = new AbortController();
|
| 852 |
record = {owner, active:AbortSignal.any([signal,owner.signal]), generation:rateGeneration,
|
| 853 |
row:{at:performance.now(),attempt,range:match?[start,end]:null,headersMs:0,bodyMs:0,bytes:0}};
|
| 854 |
+
if (recoveryEligible) arm();
|
| 855 |
const received = await abortable(Promise.resolve(nativeFetch.call(globalThis,input,{...init,cache:"no-store",signal:record.active}))
|
| 856 |
.then(value => {
|
| 857 |
if (owner.signal.aborted || signal.aborted) {value.body?.cancel().catch(() => {});throw record.active.reason;}
|
|
|
|
| 861 |
record.row.headersMs = performance.now()-record.row.at;
|
| 862 |
record.row.status = received.status;
|
| 863 |
record.row.serverTiming = (received.headers.get("Server-Timing") || "").slice(0,512);
|
| 864 |
+
if (recoveryEligible) {
|
| 865 |
+
if (received.status === 429) {
|
| 866 |
+
const retryable = noteRateLimit(received,url);
|
| 867 |
+
throw Object.assign(new Error("PDF rate limited"),{retryable,status:429,code:"READER_PDF_RATE_LIMIT"});
|
| 868 |
+
}
|
| 869 |
+
if ([408,500,502,503,504].includes(received.status))
|
| 870 |
+
throw Object.assign(new Error("PDF temporary upstream failure"),{retryable:true});
|
| 871 |
}
|
|
|
|
|
|
|
| 872 |
// PDF.js needs headers immediately to cancel the full GET and demand ranges.
|
| 873 |
// Initial recovery must not read or wait for this potentially large body.
|
| 874 |
if (received.ok) {
|
| 875 |
remember(received,url);
|
| 876 |
+
if (recoveryEligible) noteRateSuccess(record.generation);
|
| 877 |
}
|
| 878 |
if (!eligible || received.status !== 206) {
|
| 879 |
if (delivered) throw new Error("PDF_RANGE_INVALID");
|
tests/reader/pdf-network/test_reader_pdf_backpressure.mjs
CHANGED
|
@@ -336,6 +336,28 @@ try {
|
|
| 336 |
assert.equal(response.status,429);assert.equal(calls.length,1);assert.equal(calls[0].method,'HEAD');
|
| 337 |
assert.equal(calls[0].headers.get('If-None-Match'),'"stable"');policy.dispose();
|
| 338 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
// Three callers rejected in the same burst resume one by one rather than
|
| 340 |
// immediately reissuing all their requests together.
|
| 341 |
{
|
|
|
|
| 336 |
assert.equal(response.status,429);assert.equal(calls.length,1);assert.equal(calls[0].method,'HEAD');
|
| 337 |
assert.equal(calls[0].headers.get('If-None-Match'),'"stable"');policy.dispose();
|
| 338 |
}
|
| 339 |
+
// Temporary-status recovery applies to a full GET or one bounded Range,
|
| 340 |
+
// not a conditional or open-ended Range. Each attempt retains its headers.
|
| 341 |
+
for (const [name, requestHeaders, status, expectedCalls] of [
|
| 342 |
+
['full GET', {}, 200, 2],
|
| 343 |
+
['bounded Range', {Range:'bytes=0-3'}, 206, 2],
|
| 344 |
+
['conditional Range', {Range:'bytes=0-3','If-Range':'"stable"'}, 503, 1],
|
| 345 |
+
['open-ended Range', {Range:'bytes=0-'}, 503, 1]
|
| 346 |
+
]) {
|
| 347 |
+
const requests=[];
|
| 348 |
+
globalThis.fetch=async (_input,init)=>{
|
| 349 |
+
requests.push([...new Headers(init.headers)]);
|
| 350 |
+
if(requests.length===1)return new Response(null,{status:503});
|
| 351 |
+
return new Response(bytes,{status,headers:status===206 ? headers : {ETag:'"stable"','Content-Length':'4'}});
|
| 352 |
+
};
|
| 353 |
+
const policy=createPdfFetchPolicy(opts);policy.add(['/a.pdf']);
|
| 354 |
+
try {
|
| 355 |
+
const response=await fetch('/a.pdf',{headers:requestHeaders});
|
| 356 |
+
assert.equal(response.status,status,name);
|
| 357 |
+
if(status!==503)assert.deepEqual(new Uint8Array(await response.arrayBuffer()),bytes,name);
|
| 358 |
+
assert.deepEqual(requests,Array.from({length:expectedCalls},()=>[...new Headers(requestHeaders)]),name);
|
| 359 |
+
} finally {policy.dispose();}
|
| 360 |
+
}
|
| 361 |
// Three callers rejected in the same burst resume one by one rather than
|
| 362 |
// immediately reissuing all their requests together.
|
| 363 |
{
|