Chore: destroy worker thread

This commit is contained in:
SukkaW 2022-09-23 00:07:38 +08:00
parent 8a7f32e2cd
commit 7cec542320
4 changed files with 37 additions and 29 deletions

View File

@ -12,6 +12,7 @@ const threads = isCI ? cpuCount : cpuCount / 2;
/** @type Set<string> */ /** @type Set<string> */
const domainSets = new Set(); const domainSets = new Set();
console.log('Downloading hosts file...');
console.time('* Download and process Hosts'); console.time('* Download and process Hosts');
// Parse from remote hosts & domain lists // Parse from remote hosts & domain lists
@ -230,7 +231,10 @@ const threads = isCI ? cpuCount : cpuCount / 2;
const piscina = new Piscina({ const piscina = new Piscina({
filename: pathResolve(__dirname, 'worker/build-reject-domainset-worker.js'), filename: pathResolve(__dirname, 'worker/build-reject-domainset-worker.js'),
workerData: [...domainSets] workerData: [...domainSets],
idleTimeout: 50,
minThreads: threads,
maxThreads: threads
}); });
console.log(`Launching ${threads} threads...`) console.log(`Launching ${threads} threads...`)
@ -244,19 +248,18 @@ const threads = isCI ? cpuCount : cpuCount / 2;
return result; return result;
}, []); }, []);
(await Promise.all( (
Array.from(domainSets) await Promise.all(
.reduce((result, element, index) => { Array.from(domainSets)
const chunk = index % threads; .reduce((result, element, index) => {
result[chunk] ??= []; const chunk = index % threads;
result[chunk] ??= [];
result[chunk].push(element); result[chunk].push(element);
return result; return result;
}, []) }, [])
.map(chunk => piscina.run( .map(chunk => piscina.run({ chunk }, { name: 'dedupe' }))
{ chunk } )
)) ).forEach((result, taskIndex) => {
)).forEach((result, taskIndex) => {
const chunk = tasksArray[taskIndex]; const chunk = tasksArray[taskIndex];
result.forEach((value, index) => { result.forEach((value, index) => {
if (value === 1) { if (value === 1) {
@ -268,10 +271,17 @@ const threads = isCI ? cpuCount : cpuCount / 2;
console.log(`* Dedupe from covered subdomain - ${(Date.now() - START_TIME) / 1000}s`); console.log(`* Dedupe from covered subdomain - ${(Date.now() - START_TIME) / 1000}s`);
console.log(`Deduped ${previousSize - domainSets.size} rules!`); console.log(`Deduped ${previousSize - domainSets.size} rules!`);
await fsPromises.writeFile( await Promise.all([
pathResolve(__dirname, '../List/domainset/reject.conf'), fsPromises.writeFile(
`${[...domainSets].join('\n')}\n`, pathResolve(__dirname, '../List/domainset/reject.conf'),
{ encoding: 'utf-8' }); `${[...domainSets].join('\n')}\n`,
{ encoding: 'utf-8' }
),
piscina.destroy()
]);
console.timeEnd('Total Time - build-reject-domain-set'); console.timeEnd('Total Time - build-reject-domain-set');
if (piscina.queueSize === 0) {
process.exit(0);
}
})(); })();

View File

@ -55,6 +55,8 @@ async function processDomainLists (domainListsUrl) {
* @param {string | URL} hostsUrl * @param {string | URL} hostsUrl
*/ */
async function processHosts (hostsUrl, includeAllSubDomain = false) { async function processHosts (hostsUrl, includeAllSubDomain = false) {
console.time(` - processHosts: ${hostsUrl}`);
if (typeof hostsUrl === 'string') { if (typeof hostsUrl === 'string') {
hostsUrl = new URL(hostsUrl); hostsUrl = new URL(hostsUrl);
} }
@ -87,6 +89,8 @@ async function processHosts (hostsUrl, includeAllSubDomain = false) {
} }
}); });
console.timeEnd(` - processHosts: ${hostsUrl}`);
return [...domainSets]; return [...domainSets];
} }
@ -98,10 +102,6 @@ async function processHosts (hostsUrl, includeAllSubDomain = false) {
async function processFilterRules (filterRulesUrl, fallbackUrls) { async function processFilterRules (filterRulesUrl, fallbackUrls) {
console.time(` - processFilterRules: ${filterRulesUrl}`); console.time(` - processFilterRules: ${filterRulesUrl}`);
if (typeof filterRulesUrl === 'string') {
filterRulesUrl = new URL(filterRulesUrl);
}
/** @type Set<string> */ /** @type Set<string> */
const whitelistDomainSets = new Set(); const whitelistDomainSets = new Set();
/** @type Set<string> */ /** @type Set<string> */

View File

@ -1,16 +1,15 @@
const { workerData, move } = require('piscina'); const Piscina = require('piscina');
// pre check if fullset domain is starts with a "." // pre check if fullset domain is starts with a "."
// This avoid calling chatCodeAt repeatedly // This avoid calling chatCodeAt repeatedly
// workerData is an array of string. Sort it by length, short first: // workerData is an array of string. Sort it by length, short first:
const fullsetDomainStartsWithADot = workerData.filter(domain => ( const fullsetDomainStartsWithADot = Piscina.workerData.filter(domain => (
domain.charCodeAt(0) === 46 domain.charCodeAt(0) === 46
&& !canExcludeFromDedupe(domain) && !canExcludeFromDedupe(domain)
)); ));
const totalLen = fullsetDomainStartsWithADot.length; const totalLen = fullsetDomainStartsWithADot.length;
module.exports = ({ chunk }) => { module.exports.dedupe = ({ chunk }) => {
const chunkLength = chunk.length; const chunkLength = chunk.length;
const outputToBeRemoved = new Int8Array(chunkLength); const outputToBeRemoved = new Int8Array(chunkLength);
@ -61,11 +60,11 @@ module.exports = ({ chunk }) => {
} }
} }
return move(outputToBeRemoved); return outputToBeRemoved;
}; };
// duckdns.org domain will not overlap and doesn't need dedupe // duckdns.org domain will not overlap and doesn't need dedupe
function canExcludeFromDedupe(domain) { function canExcludeFromDedupe (domain) {
if (domain.length === 23 && domain.endsWith('.duckdns.org')) { if (domain.length === 23 && domain.endsWith('.duckdns.org')) {
return true; return true;
} }

View File

@ -35,7 +35,6 @@ DOMAIN-KEYWORD,49329d48d6c.
DOMAIN-KEYWORD,vsvevvcca DOMAIN-KEYWORD,vsvevvcca
DOMAIN-KEYWORD,envci. DOMAIN-KEYWORD,envci.
DOMAIN-KEYWORD,aarsenvs. DOMAIN-KEYWORD,aarsenvs.
DOMAIN-SUFFIX,duckdns.org
# --- End of Blacklist Section # --- End of Blacklist Section