mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 01:00:34 +08:00
Perf: wait longer before fetch fallback
Some checks failed
Build / Build (push) Has been cancelled
Build / Diff output (push) Has been cancelled
Build / Deploy to Cloudflare Pages (3.114.12) (push) Has been cancelled
Build / Deploy to GitHub and GitLab (push) Has been cancelled
Build / Remove Artifacts after Deployment (push) Has been cancelled
Some checks failed
Build / Build (push) Has been cancelled
Build / Diff output (push) Has been cancelled
Build / Deploy to Cloudflare Pages (3.114.12) (push) Has been cancelled
Build / Deploy to GitHub and GitLab (push) Has been cancelled
Build / Remove Artifacts after Deployment (push) Has been cancelled
This commit is contained in:
parent
9e9ee36292
commit
19f4b485b3
@ -7,6 +7,8 @@ import { ProcessLineStream } from './process-line';
|
|||||||
import { AdGuardFilterIgnoreUnsupportedLinesStream } from './parse-filter/filters';
|
import { AdGuardFilterIgnoreUnsupportedLinesStream } from './parse-filter/filters';
|
||||||
import { appendArrayInPlace } from 'foxts/append-array-in-place';
|
import { appendArrayInPlace } from 'foxts/append-array-in-place';
|
||||||
|
|
||||||
|
import { newQueue } from '@henrygd/queue';
|
||||||
|
|
||||||
// eslint-disable-next-line sukka/unicorn/custom-error-definition -- typescript is better
|
// eslint-disable-next-line sukka/unicorn/custom-error-definition -- typescript is better
|
||||||
class CustomAbortError extends Error {
|
class CustomAbortError extends Error {
|
||||||
public readonly name = 'AbortError';
|
public readonly name = 'AbortError';
|
||||||
@ -15,6 +17,8 @@ class CustomAbortError extends Error {
|
|||||||
|
|
||||||
const reusedCustomAbortError = new CustomAbortError();
|
const reusedCustomAbortError = new CustomAbortError();
|
||||||
|
|
||||||
|
const queue = newQueue(16);
|
||||||
|
|
||||||
export async function fetchAssets(
|
export async function fetchAssets(
|
||||||
url: string, fallbackUrls: null | undefined | string[] | readonly string[],
|
url: string, fallbackUrls: null | undefined | string[] | readonly string[],
|
||||||
processLine = false, allowEmpty = false, filterAdGuardUnsupportedLines = false
|
processLine = false, allowEmpty = false, filterAdGuardUnsupportedLines = false
|
||||||
@ -25,7 +29,7 @@ export async function fetchAssets(
|
|||||||
if (index >= 0) {
|
if (index >= 0) {
|
||||||
// To avoid wasting bandwidth, we will wait for a few time before downloading from the fallback URL.
|
// To avoid wasting bandwidth, we will wait for a few time before downloading from the fallback URL.
|
||||||
try {
|
try {
|
||||||
await waitWithAbort(200 + (index + 1) * 400, controller.signal);
|
await waitWithAbort(1800 + (index + 1) * 1200, controller.signal);
|
||||||
} catch {
|
} catch {
|
||||||
console.log(picocolors.gray('[fetch cancelled early]'), picocolors.gray(url));
|
console.log(picocolors.gray('[fetch cancelled early]'), picocolors.gray(url));
|
||||||
throw reusedCustomAbortError;
|
throw reusedCustomAbortError;
|
||||||
@ -38,6 +42,8 @@ export async function fetchAssets(
|
|||||||
if (index >= 0) {
|
if (index >= 0) {
|
||||||
console.log(picocolors.yellowBright('[fetch fallback begin]'), picocolors.gray(url));
|
console.log(picocolors.yellowBright('[fetch fallback begin]'), picocolors.gray(url));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// we don't queue add here
|
||||||
const res = await $$fetch(url, { signal: controller.signal, ...defaultRequestInit });
|
const res = await $$fetch(url, { signal: controller.signal, ...defaultRequestInit });
|
||||||
|
|
||||||
let stream = nullthrow(res.body, url + ' has an empty body')
|
let stream = nullthrow(res.body, url + ' has an empty body')
|
||||||
@ -49,7 +55,9 @@ export async function fetchAssets(
|
|||||||
if (filterAdGuardUnsupportedLines) {
|
if (filterAdGuardUnsupportedLines) {
|
||||||
stream = stream.pipeThrough(new AdGuardFilterIgnoreUnsupportedLinesStream());
|
stream = stream.pipeThrough(new AdGuardFilterIgnoreUnsupportedLinesStream());
|
||||||
}
|
}
|
||||||
const arr = await Array.fromAsync(stream);
|
|
||||||
|
// we does queue during downloading
|
||||||
|
const arr = await queue.add(() => Array.fromAsync(stream));
|
||||||
|
|
||||||
if (arr.length < 1 && !allowEmpty) {
|
if (arr.length < 1 && !allowEmpty) {
|
||||||
throw new ResponseError(res, url, 'empty response w/o 304');
|
throw new ResponseError(res, url, 'empty response w/o 304');
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user