mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-28 17:41:54 +08:00
Perf: run reverse chn cidr in worker_threads
This commit is contained in:
@@ -16,7 +16,7 @@ const PROBE_CHN_CIDR_V4 = [
|
||||
'120.78.92.171'
|
||||
];
|
||||
|
||||
export const getChnCidrPromise = createMemoizedPromise(cachedOnlyFail(
|
||||
export const getChnCidrPromise = createMemoizedPromise(cachedOnlyFail<[], [string[], string[]]>(
|
||||
async function getChnCidr() {
|
||||
const [_cidr4, cidr6] = await Promise.all([
|
||||
fetchRemoteTextByLine('https://raw.githubusercontent.com/misakaio/chnroutes2/master/chnroutes.txt', true).then(Array.fromAsync<string>),
|
||||
|
||||
@@ -1,31 +1,61 @@
|
||||
import path from 'node:path';
|
||||
import { task } from './trace';
|
||||
|
||||
import { exclude, merge } from 'fast-cidr-tools';
|
||||
import { getChnCidrPromise } from './build-chn-cidr';
|
||||
import { NON_CN_CIDR_INCLUDED_IN_CHNROUTE, RESERVED_IPV4_CIDR } from './constants/cidr';
|
||||
// import { RESERVED_IPV4_CIDR, NON_CN_CIDR_INCLUDED_IN_CHNROUTE } from './constants/cidr';
|
||||
|
||||
import fs from 'node:fs';
|
||||
import { OUTPUT_INTERNAL_DIR } from './constants/dir';
|
||||
import { asyncWriteToStream } from 'foxts/async-write-to-stream';
|
||||
import { mkdirp } from './lib/misc';
|
||||
import { appendArrayInPlace } from './lib/append-array-in-place';
|
||||
// import { appendArrayInPlace } from './lib/append-array-in-place';
|
||||
import Worktank from 'worktank';
|
||||
|
||||
export const buildInternalReverseChnCIDR = task(require.main === module, __filename)(async () => {
|
||||
const [cidr] = await getChnCidrPromise();
|
||||
const pool = new Worktank({
|
||||
name: 'build-internal-reverse-chn-cidr',
|
||||
size: 1,
|
||||
timeout: 10000, // The maximum number of milliseconds to wait for the result from the worker, if exceeded the worker is terminated and the execution promise rejects
|
||||
warmup: true,
|
||||
autoterminate: 30000, // The interval of milliseconds at which to check if the pool can be automatically terminated, to free up resources, workers will be spawned up again if needed
|
||||
env: {},
|
||||
methods: { // An object mapping function names to functions objects to serialize and deserialize into each worker thread, only functions that don't depend on their closure can be serialized
|
||||
// eslint-disable-next-line object-shorthand -- workertank
|
||||
getreversedCidr: async function (cidr: string[], importMetaUrl: string): Promise<string[]> {
|
||||
// TODO: createRequire is a temporary workaround for https://github.com/nodejs/node/issues/51956
|
||||
const { default: module } = await import('node:module');
|
||||
const __require = module.createRequire(importMetaUrl);
|
||||
const { exclude, merge } = __require('fast-cidr-tools');
|
||||
const { RESERVED_IPV4_CIDR, NON_CN_CIDR_INCLUDED_IN_CHNROUTE } = __require('./constants/cidr');
|
||||
const { appendArrayInPlace } = __require('./lib/append-array-in-place');
|
||||
|
||||
const reversedCidr = merge(
|
||||
appendArrayInPlace(
|
||||
exclude(
|
||||
['0.0.0.0/0'],
|
||||
RESERVED_IPV4_CIDR.concat(cidr),
|
||||
return merge(
|
||||
appendArrayInPlace(
|
||||
exclude(
|
||||
['0.0.0.0/0'],
|
||||
RESERVED_IPV4_CIDR.concat(cidr),
|
||||
true
|
||||
),
|
||||
// https://github.com/misakaio/chnroutes2/issues/25
|
||||
NON_CN_CIDR_INCLUDED_IN_CHNROUTE
|
||||
),
|
||||
true
|
||||
),
|
||||
// https://github.com/misakaio/chnroutes2/issues/25
|
||||
NON_CN_CIDR_INCLUDED_IN_CHNROUTE
|
||||
),
|
||||
true
|
||||
);
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const buildInternalReverseChnCIDR = task(require.main === module, __filename)(async (span) => {
|
||||
const [cidr] = await span.traceChildPromise('download chnroutes2', getChnCidrPromise());
|
||||
|
||||
const reversedCidr = await span.traceChildAsync('build reversed chn cidr', async () => {
|
||||
const reversedCidr = await pool.exec(
|
||||
'getreversedCidr',
|
||||
[cidr, import.meta.url]
|
||||
);
|
||||
pool.terminate();
|
||||
|
||||
return reversedCidr;
|
||||
});
|
||||
|
||||
const outputFile = path.join(OUTPUT_INTERNAL_DIR, 'reversed-chn-cidr.txt');
|
||||
await mkdirp(OUTPUT_INTERNAL_DIR);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { noop } from 'foxts/noop';
|
||||
import { basename, extname } from 'node:path';
|
||||
import process from 'node:process';
|
||||
import picocolors from 'picocolors';
|
||||
@@ -98,8 +99,12 @@ export function createSpan(name: string, parentTraceResult?: TraceResult): Span
|
||||
export const dummySpan = createSpan('');
|
||||
|
||||
export function task(importMetaMain: boolean, importMetaPath: string) {
|
||||
return <T>(fn: (span: Span) => Promise<T>, customName?: string) => {
|
||||
return <T>(fn: (span: Span, onCleanup: (cb: () => Promise<void> | void) => void) => Promise<T>, customName?: string) => {
|
||||
const taskName = customName ?? basename(importMetaPath, extname(importMetaPath));
|
||||
let cleanup: () => Promise<void> | void = noop;
|
||||
const onCleanup = (cb: () => void) => {
|
||||
cleanup = cb;
|
||||
};
|
||||
|
||||
const dummySpan = createSpan(taskName);
|
||||
if (importMetaMain) {
|
||||
@@ -112,7 +117,7 @@ export function task(importMetaMain: boolean, importMetaPath: string) {
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
dummySpan.traceChildAsync('dummy', fn).finally(() => {
|
||||
dummySpan.traceChildAsync('dummy', (childSpan) => fn(childSpan, onCleanup)).finally(() => {
|
||||
dummySpan.stop();
|
||||
printTraceResult(dummySpan.traceResult);
|
||||
whyIsNodeRunning();
|
||||
@@ -121,9 +126,9 @@ export function task(importMetaMain: boolean, importMetaPath: string) {
|
||||
|
||||
return async (span?: Span) => {
|
||||
if (span) {
|
||||
return span.traceChildAsync(taskName, fn);
|
||||
return span.traceChildAsync(taskName, (childSpan) => fn(childSpan, onCleanup).finally(() => cleanup()));
|
||||
}
|
||||
return fn(dummySpan);
|
||||
return fn(dummySpan, onCleanup).finally(() => cleanup());
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user