Skip to content

Commit 8cefcf7

Browse files
committed
Perf: compare and write file in worker
1 parent 830ef80 commit 8cefcf7

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

Build/build-cdn-download-conf.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const buildCdnDownloadConf = task(require.main === module, __filename)(as
5959
downloadDomainSet,
6060
steamDomainSet
6161
] = await Promise.all([
62-
getS3OSSDomainsPromise,
62+
span.traceChildPromise('download public suffix list for s3', getS3OSSDomainsPromise),
6363
cdnDomainsListPromise,
6464
downloadDomainSetPromise,
6565
steamDomainSetPromise

Build/lib/create-file.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ export async function fileEqual(linesA: string[], source: AsyncIterable<string>
4848
}
4949

5050
export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) {
51-
const linesALen = linesA.length;
52-
5351
const isEqual = await span.traceChildAsync(`compare ${filePath}`, async () => {
5452
if (fs.existsSync(filePath)) {
5553
return fileEqual(linesA, readFileByLine(filePath));
@@ -65,6 +63,8 @@ export async function compareAndWriteFile(span: Span, linesA: string[], filePath
6563
}
6664

6765
await span.traceChildAsync(`writing ${filePath}`, async () => {
66+
const linesALen = linesA.length;
67+
6868
// The default highwater mark is normally 16384,
6969
// So we make sure direct write to file if the content is
7070
// most likely less than 500 lines

Build/lib/create-file.worker.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import Worktank from 'worktank';
2+
import os from 'node:os';
3+
import process from 'node:process';
4+
import type { Span } from '../trace';
5+
6+
const pool = new Worktank({
7+
name: 'process-phishing-domains',
8+
size: Math.max(2, Math.max(1, ('availableParallelism' in os ? os.availableParallelism() : (os as typeof import('node:os')).cpus().length) - 1)),
9+
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
10+
warmup: true,
11+
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
12+
env: {},
13+
methods: {
14+
// eslint-disable-next-line object-shorthand -- workertank
15+
compareAndWriteFile: async function (
16+
linesA: string[], filePath: string,
17+
importMetaUrl: string
18+
): Promise<void> {
19+
const { default: module } = await import('node:module');
20+
const __require = module.createRequire(importMetaUrl);
21+
22+
const fs = __require('fs') as typeof import('fs');
23+
const { readFileByLine } = __require('./fetch-text-by-line') as typeof import('./fetch-text-by-line');
24+
const { fileEqual } = __require('./create-file') as typeof import('./create-file');
25+
const path = __require('node:path') as typeof import('node:path');
26+
const { fastStringArrayJoin } = __require('foxts/fast-string-array-join') as typeof import('foxts/fast-string-array-join');
27+
const picocolors = __require('picocolors') as typeof import('picocolors');
28+
29+
let isEqual = false;
30+
if (fs.existsSync(filePath)) {
31+
isEqual = await fileEqual(linesA, readFileByLine(filePath));
32+
} else {
33+
console.log(`${filePath} does not exists, writing...`);
34+
isEqual = false;
35+
}
36+
37+
if (isEqual) {
38+
console.log(picocolors.gray(picocolors.dim(`same content, bail out writing: ${filePath}`)));
39+
return;
40+
}
41+
42+
const dir = path.dirname(filePath);
43+
if (!fs.existsSync(dir)) {
44+
fs.mkdirSync(dir, { recursive: true });
45+
}
46+
fs.writeFileSync(filePath, fastStringArrayJoin(linesA, '\n') + '\n', { encoding: 'utf-8' });
47+
}
48+
}
49+
});
50+
51+
export function compareAndWriteFileInWorker(span: Span, linesA: string[], filePath: string) {
52+
return span.traceChildAsync(`compare and write ${filePath}`, () => pool.exec('compareAndWriteFile', [linesA, filePath, import.meta.url]));
53+
}
54+
55+
process.on('beforeExit', () => pool.terminate());

Build/lib/writing-strategy/base.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Span } from '../../trace';
22
import { compareAndWriteFile } from '../create-file';
3+
import { compareAndWriteFileInWorker } from '../create-file.worker';
34

45
/**
56
* The class is not about holding rule data, instead it determines how the
@@ -76,6 +77,19 @@ export abstract class BaseWriteStrategy {
7677
if (!this.result) {
7778
return;
7879
}
80+
81+
if (this.result.length > 1000) {
82+
return compareAndWriteFileInWorker(
83+
span,
84+
this.withPadding(
85+
title,
86+
description,
87+
date,
88+
this.result
89+
),
90+
filePath
91+
);
92+
}
7993
return compareAndWriteFile(
8094
span,
8195
this.withPadding(

0 commit comments

Comments
 (0)